通过在Matlab中拖动鼠标来绘制线条
问题描述:
我想通过在按住鼠标左键的同时拖动鼠标来在图像上方绘制一组断开的线.但是,如果我单击先前绘制的线以指定下一行的起点,则不会调用回调函数,而且我也无法理解该点.这是我的代码:
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.
先谢谢您.
欢呼声, 索山
答
您还需要在标绘线上设置ButtonDownFcn
属性,即
You need to set the ButtonDownFcn
property on the plotted line as well, i.e.
plot(X,Y,'r','LineWidth',6,'ButtonDownFcn',@startDragFcn)