overlay plot on imgage
97 次查看(过去 30 天)
显示 更早的评论
Don
2017-10-2
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
采纳的回答
Cedric
2017-10-2
编辑:Cedric
2017-10-2
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 个评论
Don
2017-10-4
Thank you for your help so far. I am struggling with the code you supplied for the other answer, still haven't got it (yet)
Don
2017-10-5
编辑:Cedric
2017-10-5
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
2017-10-5
编辑:Cedric
2017-10-5
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:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/185757/image.jpeg)
(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
2017-10-5
编辑:Cedric
2017-10-5
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
2017-10-5
编辑:Cedric
2017-10-5
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
2017-10-6
编辑:Cedric
2017-10-6
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/185792/image.png)
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!
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/185794/image.png)
Cedric
2017-10-6
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
2017-10-6
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
2017-10-6
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
2017-10-7
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
Don
2017-10-7
YAY! I think I got it -- imgLocator = fullfile(filepath, filename) ;, xPoints = xaxis; yPoints = yaxis;
This seems to work! You are a genius. Thank you Thank you Thank you
更多回答(0 个)
另请参阅
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!发生错误
由于页面发生更改,无法完成操作。请重新加载页面以查看其更新后的状态。
您也可以从以下列表中选择网站:
如何获得最佳网站性能
选择中国网站(中文或英文)以获得最佳网站性能。其他 MathWorks 国家/地区网站并未针对您所在位置的访问进行优化。
美洲
- América Latina (Español)
- Canada (English)
- United States (English)
欧洲
- 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)
亚太
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)