Drawing lines by dragging the mouse in matlab

二次信任 提交于 2019-12-13 18:22:01

问题


I want to draw a set of disconnected lines on top of an image by dragging the mouse while pressing the left button. However, if I click on a previously drawn line to specify the starting point of the next line, the callback function is not being called and I don't get the point. Here is my code:

function main_test

S.fH = figure('menubar','none');
im = imread( 'image.jpg' );

S.aH = axes;
S.iH = imshow( im ); hold on
axis image;

X = [];
Y = [];

set(S.aH,'ButtonDownFcn',@startDragFcn)
set(S.iH,'ButtonDownFcn',@startDragFcn)
set(S.fH, 'WindowButtonUpFcn', @stopDragFcn);

function startDragFcn(varargin)
    set( S.fH, 'WindowButtonMotionFcn', @draggingFcn );
    pt = get(S.aH, 'CurrentPoint');
    x = pt(1,1);
    y = pt(1,2);
    X = x;
    Y = y;
end

function draggingFcn(varargin)
    pt = get(S.aH, 'CurrentPoint');
    x = pt(1,1);
    y = pt(1,2);
    X = [X x];
    Y = [Y y];

    plot(X, Y, 'r', 'LineWidth', 6);
    hold on
    drawnow 
end

function stopDragFcn(varargin)
    set(S.fH, 'WindowButtonMotionFcn', '');  %eliminate fcn on release
end

end

Would you please help me to identify the problem in this.

Thank you in a advance..

cheers, Sawsan


回答1:


You need to set the ButtonDownFcn property on the plotted line as well, i.e.

plot(X,Y,'r','LineWidth',6,'ButtonDownFcn',@startDragFcn)


来源:https://stackoverflow.com/questions/13258133/drawing-lines-by-dragging-the-mouse-in-matlab

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!