MATLAB Answers


impoly no longer animates the tracing line after placing other animated line objects

Asked by CAM
on 13 Aug 2019
Latest activity Answered by CAM
on 19 Aug 2019
Our users need to trace around a region on an image using impoly (closed = 0). In some cases they would like to start at a point that aligns with a separate feature in another part of the image. So they asked for a movable, vertical "guide line" to help with the alignment.
hFig = figure;
hImg = imshow(imgFileName);
lineColor = [0 1 0.89];
lineWidth = 2;
szImg = size(hImg.CData);
imgCenter = 0.5*szImg(1:2);
origButtonMotionCB = get(hFig, 'WindowButtonMotionFcn');
hLine = xline(ax, imgCenter(1), '--', 'Color', lineColor, 'LineWidth', lineWidth);
set(hLine, 'ButtonDownFcn', @(obj, eventdata)fcnStartDragLine(gcbo, eventdata, hFig));
set(hFig, 'WindowButtonUpFcn', {@fcnStopDragLine, hFig, origButtonMotionCB});
function fcnStartDragLine(hObject, eventdata, hFig)
hLine = hObject;
set(hFig, 'WindowButtonMotionFcn', {@fcnDragLine, hFig, hLine});
function fcnDragLine(hObject, eventdata, hFig, hLine)
ax = findobj(hFig, 'Type', 'axes');
pt = get(ax, 'CurrentPoint');
ptX = pt(1, 1);
set(hLine, 'Value', ptX);
function fcnStopDragLine(hObject, eventdata, hFig, origButtonMotionCB)
set(hFig, 'WindowButtonMotionFcn', origButtonMotionCB);
Problem: After drawing the guide line, I launch impoly, however the animated line that extends from the previous vertex to the mouse pointer no longer appears. Everything else works per spec( i.e., I can still add new vertices, etc).
If I launch impoly before adding the guide line, then the animated line appears during the polygon creation.
If I delete the guide line from the axis.Children, impoly still has no animated line.
What am I missing?
Thank you in advance for any help you can offer.


I realize "impoly" is an old command, but that is in our legacy application.
The "drawpolygon" command behaves the same way as above.
To be complete, I tried using "drawline" to create the guide line. But impoly/drawpolygon will not register a vertex if you try to click on the line-ROI. The users did not like this.

Sign in to comment.




1 Answer

Answer by CAM
on 19 Aug 2019
 Accepted Answer

JK from the Matlab Support Team provided a solution. Before the origButtonMotionCB definition above, he suggested adding the following code:
if isempty(get(hFig, 'WindowButtonMotionFcn'))
set(hFig, 'WindowButtonMotionFcn',@(~,~) deal());
He provided two reasons why the "impoly" function was not showing the animated lines:
1. The "impoly" function uses the figure's "WindowButtonMotionFcn" callback to handle animating the line as you move the mouse around. Right now, the "WindowButtonUpFcn" gets rid of any existing callbacks in "WindoButtonMotionFcn". Although this allows for "hline" to stop moving when the mouse is released, it also removes the "impoly" function's callback as you release the mouse.
2. The "CurrentPoint" property is used to determine where the mouse is located in the axes data space. However, this property is only ever updated on mouse clicks or when the figure's WindowButtonMotionFcn is not empty. Because of this, the animation for "impoly" will work only if "WindowButtonMotionFcn" is not empty. Setting the "WindowButtonMotionFcn" to something that is not empty but still doesn't really do anything (e.g. deal()) could resolve this issue.
To deal with these issues, the following work around will work if using "drawpolygon" as opposed to "impoly".
The modified code avoids setting an empty callback in WindowButtonMotionFcn during drawing.
The solution does work for drawpolygon and drawpolyline. It still does not work for impoly. But I should probably update my application anyway... :-)
Mega thanks to JK at Matlab Support for the solution!


Sign in to comment.