You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
overlay plot on imgage
110 views (last 30 days)
Show older comments
I have images of artwork and plots of data from an eye tracker. I want to overlay the data plots on the images. A complication: artwork images come in different sizes. I think the subplot function will do some of this be I don't know how. Any help is greatly appreciated
Accepted Answer
Cedric
on 2 Oct 2017
Edited: Cedric
on 2 Oct 2017
hold on
for overlays, for example:
imshow( myImage ) ;
hold on ;
plot( x, y, 'rx' ) ;
and you will see the plot over the image. You can have more control by creating axes by yourself, but it is more complicated. For this, if you really have the time to learn, look at my answer here.
13 Comments
Cedric
on 4 Oct 2017
Edited: Cedric
on 4 Oct 2017
What part don't you understand? Don't loose too much time, just ask and I can clarify quickly, it's not that complicated.
Also, if you gave a few examples, I could tailor the explanations to your specific case.
Don
on 5 Oct 2017
Edited: Cedric
on 5 Oct 2017
OK.. Here's what I have so far:
Figure(2)
disp (' get image that goes with the data file');
These are all images of different sizes. Are pictures of famous paintings
[filename, filepath] = uigetfile('*.jpg', 'Select ejpg file');
pict = cat(2,filepath,filename);
[X,map] = imread(pict);
figure('position', [1, 10,10,10]);
I doubt this statement is necessary or useful. I think I want to fix the image to standard size
hold on;
figure(3);
positiveXaxisMask = xaxis >0 & xaxis < 1 & yaxis >0 & yaxis <1;
this stuff plots eye fixations from a data set
plot(xaxis(positiveXaxisMask), yaxis(positiveXaxisMask),'color','r','marker','o','LineStyle','--');
axis([0 1 0 1]);
x0 = 300; % seet size of plot
y0 = 75;
width = 675;
height = 675;
set(gcf,'units','points','position',[x0,y0,width,height])
% axis([0,1,0,1]);
xlabel(' Left<--- ---> Right');
ylabel('Bottom <-- --> Top')
TitleText=cat(2,'XY Coordinates of Fixation ',name);
title (TitleText);
TrackerData = cat(2,name,'.png');
eventually save the result
TrackerData = cat(2, PathName,TrackerData);
saveas(gcf,TrackerData); % save plot to subj direcory
Many thanks for any help you can give Don
Cedric
on 5 Oct 2017
Edited: Cedric
on 5 Oct 2017
You have to try little bit by little bit. First with
figure('position', [1, 10,10,10]);
hold on;
figure(3);
you create a tiny figure in the lower-left corner of your screen, then you tell MATALB not to overwrite its content but to overlay whatever will be plotted next, then you create a new figure. What is the purpose of the first?
I'd say, forget about all the code for user management, scaling, etc, at first and work with a static image, e.g. the MonaLisa attached.
So first, are we able to create a empty large figure?
figure() ;
well, it's a new empty figure but not that large. Can we set its position/size relative (normalized) to the screen?
set( gcf, 'Units', 'normalize', 'Position', [0.1,0.1,0.8,0.7] ) ;
where the vector of positions is [x,y,w,h] given as a fraction of the screen size (so x=10%, y=10%, w=80%, h=70%).
Great, first part is working and it is two lines long. Then, are we able to plot something? Let's try a sine wave, just for the test:
x = 1 : size(img, 2) ; % 1 pixel to the width of the image.
y = size( img, 1 ) * (sin(x/50)+1) / 2 ; % Scale a sine to the height of the image.
If we plot it in the current figure, it will clear the previous content first, unless we tell MATLAB that it must "hold" the previous content and overlay the new:
hold( 'on' ) ;
plot( x, y, 'b' ) ;
That seems to work too:
(sorry for the sacrilege by the way ;))
So now we know how to plot, overlay curves, and we can "complexify" the code a little by working on the curves that you need to plot.
I let you check if this works for you and try the next steps, always making small steps.
Don
on 5 Oct 2017
Edited: Cedric
on 5 Oct 2017
figure(3);
disp (' get image that goes with the data file');
[filename, filepath] = uigetfile('.jpg', 'Select .jpg file');
pict = cat(2,filepath,filename);
img=set( gcf, 'Units', 'normalize', 'Position', [0.1,0.1,0.8,0.7] ) ;
imshow(pict)
x = 1 : size(img, 2) ; % 1 pixel to the width of the image.
y = size( img, 1 ) * (sin(x/50)+1) / 2 ; % Scale a sine to the height of the image.
hold( 'on' ) ;
plot( x, y, 'r' ) ;
I got the image to plot and (I guess) the right dimensions on the screen. BUT -- no superimposed plot. Note: Had to add the definition of img had to add imshow
x and y both have value 1×0 empty double row vector
Cedric -- thanks very much for your help on this
Cedric
on 5 Oct 2017
Edited: Cedric
on 5 Oct 2017
No problem, see my example, I never wrote "img=set(...".
Also, using FULLFILE is better for concatenating parts of a path to a file, and you should name variables according to what they store. If you look at the output of UIGETFILE, you will see that the two parameters are strings: the base path and the file name.
I realize that I forgot to copy the part about loading the image in my example, my mistake!
[filename, filepath] = uigetfile( '.jpg', 'Select .jpg file' ) ;
imgLocator = fullfile( filepath, filename ) ;
figure() ;
set( gcf, 'Units', 'normalize', 'Position', [0.1,0.1,0.8,0.7] ) ;
img = imread( imgLocator ) ;
imshow( img ) ;
hold( 'on' ) ;
x = 1 : size(img, 2) ; % Pixel 1 to the width of the image.
y = size(img, 1) * (sin(x/50)+1) / 2 ; % Scale a sine to the height of the image.
plot( x, y, 'r', 'LineWidth', 3 ) ;
Don
on 6 Oct 2017
Edited: Cedric
on 6 Oct 2017
The code you sent works GREAT for plotting sine wave over any of my pictures
figure(3);
set( gcf, 'Units', 'normalize', 'Position', [0.1,0.1,0.8,0.7] ) ;
img = imread(imgLocator) ;
imshow(img) ;
hold( 'on' ) ;
x = 1 : size(img, 2) ; % Pixel 1 to the width of the image.
y = size(img, 1) * (sin(x/50)+1) / 2 ; % Scale a sine to the height of the image.
plot( x, y, 'r', 'LineWidth', 3 ) ;
BUT, getting this data to plot over the picture is non trivial for me. This code gets the plot shown below
figure(2);
positiveXaxisMask = xaxis >0 & xaxis < 1 & yaxis >0 & yaxis <1;
plot(xaxis(positiveXaxisMask), yaxis(positiveXaxisMask),'color','r','marker','o','LineStyle','--');
x0 = 300; % set size of plot
y0 = 75;
width = 675;
height = 675;
set(gcf,'units','points','position',[x0,y0,width,height])
xlabel(' Left<--- ---> Right');
ylabel('Bottom <-- --> Top')
TitleText=cat(2,'XY Coordinates of Fixation ',name);
title (TitleText);
Ive tried substituting xaxis,yxais but to no avail. plot( xaxis, yaxis, 'r', 'LineWidth', 3 ) ;
Sorry to be so dense about this, But were VERY CLOSE to a solution!
Cedric
on 6 Oct 2017
I don't understand why you have these figure(2) and figure(3). In figure #3 you seem to display an image
set( gcf, 'Units', 'normalize', 'Position', [0.1,0.1,0.8,0.7] ) ;
img = imread(imgLocator) ;
imshow(img) ;
then configure the axes switching the hold on:
hold( 'on' ) ;
and then overlay whatever curves you have defined in vectors x and y.
But in figure #2 I don't see where you add the image before plotting your points.
Don
on 6 Oct 2017
well, the figure 2 is a separate plot so I can see that something is happening I hope it is independent of figure 3. Figure 3 works great except for the data points. Once 3 works, I can get rid of 2
As for figure 3, I don't know how to add the extra data points. So figure 2 is presented to you so you can see what I do to the get the data points. But I really want the data points added to figure 3
sorry for any confusion
Cedric
on 6 Oct 2017
Ok, I think that I understand now. You should rescale your x and y variables to the width and height of the image if they are relative and in [0,1], and not try to rescale the image or the axes or the figure.
I don't understand what you are doing by using this mask on your data points if they are already in [0,1]. If not, how to you define them?
Say you have xPoints and yPoints the coordinates of your points relative to the image size (in [0,1]), you can do this:
imgLocator = ... ;
xPoints = ... ;
yPoints = ... ;
% - Create figure.
figure() ;
set( gcf, 'Units', 'normalize', 'Position', [0.1,0.1,0.8,0.7] ) ;
% - Display image.
img = imread( imgLocator ) ;
imshow( img ) ;
% - Rescale x,y in [0,1] to image width,height.
x = size( img, 2 ) * xPoints ;
y = size( img, 1 ) * yPoints ; % or (1-yPoints) depending the direction.
% - Overlay points to image.
hold( 'on' ) ;
plot( x, y, 'ro', 'MarkerSize', 10, 'LineWidth', 3 ) ;
Don
on 7 Oct 2017
sorry to quibble .... imgLocator = ... ; xPoints = ... ; yPoints = ... ;
xPoints gives me the error message Error: The expression to the left of the equals sign is not a valid target for an assignment.
I can't find a definition or explanation of "...", but I think somehow it initiates something in the imgLocator line that is not finished by the xPoints line. I've looked everywhere but can't find an discussion of it
More Answers (0)
See Also
Categories
Find more on Display Image in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom(English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)