Drawing with mouse motion in a GUI-image

8 views (last 30 days)
Hi, I want to draw a line in an image displayed in a GUI. Matlab has a good example which works great for an individual figure:
However, while running it from a callback function in the GUI, I am having issues:
  1. Defining handle for the image-axis is not allowed by WindowButtonDownFcn.
  2. I can use the handle of the GUI-figure (which is an alternative of the point above), but how do I extract the co-ordinates for later use?
Example code (Matlab)
function draw_lines
% Click the left mouse button to define a point
% Drag the mouse to draw a line to the next point and
% left click again
% Right click the mouse to stop drawing
%
figure('WindowButtonDownFcn',@wbdcb)
ah = axes('DrawMode','fast');
axis ([1 10 1 10])
function wbdcb(src,evnt)
if strcmp(get(src,'SelectionType'),'normal')
[x,y,str] = disp_point(ah);
hl = line('XData',x,'YData',y,'Marker','.');
text(x,y,str,'VerticalAlignment','bottom');drawnow
set(src,'WindowButtonMotionFcn',@wbmcb)
elseif strcmp(get(src,'SelectionType'),'alt')
set(src,'WindowButtonMotionFcn','')
[x,y,str] = disp_point(ah);
text(x,y,str,'VerticalAlignment','bottom');drawnow
end
function wbmcb(src,evnt)
[xn,yn,str] = disp_point(ah);
xdat = [x,xn];
ydat = [y,yn];
set(hl,'XData',xdat,'YData',ydat);
end
end
function [x,y,str] = disp_point(ah)
cp = get(ah,'CurrentPoint');
x = cp(1,1);y = cp(1,2);
str = ['(',num2str(x,'%0.3g'),', ',num2str(y,'%0.3g'),')'];
end
end
  2 Comments
Image Analyst
Image Analyst on 18 Jul 2016
Are you using GUIDE? Or are you trying to do it the hard way and define all the properties/settings and callbacks yourself?
And what's in the axes control? If there is something like an image you'll have to set a callback for the image since it lies on top of the axes, or else set the hittest for the image to be off.
ImageProcNaive
ImageProcNaive on 3 Sep 2016
Yes, using GUIDE. Also, there will be an image displayed in the axes. I have to draw a line along an object displayed in the image. I will later measure the length of the line; but it's important to have the visual of a line.

Sign in to comment.

Answers (0)

Categories

Find more on Display Image in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!