1

我想通过在按住左键的同时拖动鼠标在图像顶部绘制一组断开的线。但是,如果我单击先前绘制的线以指定下一行的起点,则不会调用回调函数并且我不明白这一点。这是我的代码:

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

请您帮我找出其中的问题。

提前谢谢你..

干杯,索桑

4

1 回答 1

5

您还需要ButtonDownFcn在绘图线上设置属性,即

plot(X,Y,'r','LineWidth',6,'ButtonDownFcn',@startDragFcn)
于 2012-11-06T19:42:20.170 回答