Display plot on image

3 views (last 30 days)
Itai Kadosh
Itai Kadosh on 17 Jan 2016
Edited: Thorsten on 19 Jan 2016
Hi, i want to display a plot of scanning track on the original image but the axes values of the image are different from the values of the plot graph (the image i got is attached, the plot is appears in RED color at the top left of the image and the dimensions of the plot is much smaller than the dimensions of the image). How can I make sync between the axes system of the image and the plot scanning track?
Thanks, Itai

Answers (2)

Image Analyst
Image Analyst on 17 Jan 2016
Nothing is attached. You'd have to display your image and then set hold on and then plot your (x,y) data with x and y scaled to the number of pixels in each direction. Then you'd have to set the YDir property to 'reverse', or else flip your image vertically with flipud() before you call imshow().
  3 Comments
Image Analyst
Image Analyst on 17 Jan 2016
I see a plot and two binary images. I'm not seeing "the display of them on one image" <== which image is that? What's the difference between the two binary images. How did you get the (x,y) data in the range of [-4, +4]? Evidently it did not come from the rows or columns of the image because your image is a lot bigger than 8 pixel tall.
Itai Kadosh
Itai Kadosh on 19 Jan 2016
Hi,
The "original image" is the original image, the "scanning mask" is the track i need to scan and in the "scanning track" there is the presentation of both (the scanning mask image appears in the left corner as a small graph in red color). The code is attached and I want to display Figure 6 ("scanning mask" image) on Figure 5 ("original image") in the same image so Figure 6 will appears in the "white"part of Figure 5.
The X and Y axes in Figure 6, expresses Volts in X Vs. Volts in Y (there are 2 mirrors that perform the scanning).
Thank you very much!

Sign in to comment.


Thorsten
Thorsten on 19 Jan 2016
Edited: Thorsten on 19 Jan 2016
x = cumsum(Volt_and_time(2,:));
y = cumsum(Volt_and_time(1,:));
overlayplot(x,y,image, 'fitbinary')
using my function overlayplot:
function h = overlayplot(x, y, I, rect)
%OVERLAYPLOT Plot an overlay of function x,y on the image I.
%
% H = OVERLAYPLOT(X, Y, I, [RECT])
%
% Plots that do not keep the aspect ratio of the plot:
% OVERLAYPLOT(X, Y, I) Plot function scaled to the size of the image I.
% OVERLAYPLOT(X, Y, I, RECT) Plot function scaled to rectangle. Rectangle
% is given by its coordinates [XMIN XMAX YMIN YMAX].
%
% Function scaled to the size of the image, keeping the aspect ratio of the
% plot:
% OVERLAYPLOT(X, Y, I, []) Plot function witout offset.
% OVERLAYPLOT(X, Y, I, DXY) Plot function with an offset in X and Y
% direction given by DXY = [DX DY].
% OVERLAYPLOT(X, Y, I, OPTION), with OPTION = 'center' or 'c'. Plot
% centered on the image.
% OVERLAYPLOT(X, Y, I, OPTION), with OPTION = 'fitbinary' or 'ft'. Plot
% fitted to the white area of the binary image I.
%
% Thorsten.Hansen@psychol.uni-giessen.de 2016-01-19
%%Process arguments
switch nargin
case 3
plotoption = 1;
case 4
if ischar(rect)
if strcmp(rect, 'center') || strcmp(rect, 'c')
plotoption = 33; % keep ratio, center
elseif strcmp(rect, 'fitbinary') || strcmp(rect, 'fb')
% fit to rectangle given by white area of binary image
if ~islogical(I)
error('Image must be of class logical for option fitbinary.')
end
rect = [find(diff(sum(I)>0)~= 0) find(diff(sum(I,2)'>0)~= 0)];
rect = rect + [1 0 1 0]; % correct off by one error
plotoption = 2;
else
error('String must be ''center'', ''c'', ''fitbinary'' or ''fb''.')
end
else
switch numel(rect)
case 0
plotoption = 31; % keep ratio, no offset
case 2
plotoption = 32; % keep ratio, offset dx, dy given in rect
case 4
plotoption = 2; % scale to fit given rectangle
otherwise
error('Wrong number of elements, must be 0, 2 or 4.')
end
end
otherwise
errro('Wrong number of arguments, must be 3 or 4.')
end
%%Determine scale factors and offset
% first scale xy to [0,1]
wxy = max(x) - min(x); hxy = max(y) - min(y);
x0 = (x - min(x))/wxy;
y0 = (y - min(y))/hxy;
wI = size(I,2); hI= size(I,1); % width and height of image
switch plotoption
case 1 % scale plot to full size of image, ignoring ratio of plot
sx = wI; sy = hI;
dx = 0; dy = 0; % no offset
case 2 % scale plot to fit rectangle rectxy
% rectangle given by [xmin, xmax, ymin, ymax]
xmin = rect(1); xmax = rect(2); ymin = rect(3); ymax = rect(4);
sx = xmax - xmin; sy = ymax - ymin;
dx = xmin;
dy = ymin;
case {31, 32, 33} % scale plot, keeping ratio
% determine ratio of image and plot and scale accordingly
rI = wI/hI; % width/height
rxy = wxy/hxy;
if rI > rxy
s = hI;
else
s = wI;
end
sx = s; sy = s;
% determine offset
switch plotoption
case 31 % empty rect
dx = 0; dy = 0; % default is no offset
case 32 % offset in x and y
dx = rect(1); dy = rect(2);
case 33 % center
dx = (wI-sy)/2;
dy = (hI-sx)/2;
otherwise
error('Internal error: plotoption can be only 31, 32, or 33.')
end
otherwise
error('Internal error, plotoption can only be 1, 2 or 3.');
end
%%Show overlay
imshow(I)
hold on
h = plot(sx*x0+dx, sy*y0+dy);
if nargout == 0, clear h, end

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!