我必须创建一个显示图像的GUI,并且用户必须能够执行以下操作:
1-使用鼠标选择多个点;
2-完成用户操作后,点击“返回” *;
3-单击“返回”后,如果用户要编辑这些点之一,则他/她必须单击所需的点并将其拖动到他/她想要的位置。
我创建了这个功能:
function [x, y] = test(img)
[lin, col] = size(img);
fig = figure('WindowButtonDownFcn', {@func, lin, col}, 'KeyPressFcn', @keyfunc);
imshow(img, []);
% axs = axes('position', [1 col 1 lin]);
set(gca, 'Ydir', 'reverse');
x = [];
y = [];
uiwait(fig);
function func(src, callback, lin, col)
seltype = get(fig, 'SelectionType');
set(gca, 'Ydir', 'reverse');
if strcmp(seltype, 'normal')
set(fig, 'Pointer', 'circle');
cp = get(fig, 'CurrentPoint');
xinit = cp(1, 1);
yinit = cp(1, 2);
x = [x, xinit];
y = [y, yinit];
hl = line('XData', xinit, 'YData', yinit, 'color', 'b', 'Marker', '.');
set(fig, 'WindowButtonMotionFcn', {@moveMouse, lin, col});
set(fig, 'WindowButtonUpFcn', @mouseRelease);
end
function moveMouse(src, callback, lin, col)
cp = get(fig, 'CurrentPoint');
xdata = [xinit, cp(1, 1)];
ydata = [yinit, cp(1, 2)];
set(hl, 'XData', xdata);
set(hl, 'YData', ydata);
drawnow;
end
function mouseRelease(src, callback)
last_selection = get(fig, 'SelectionType');
if strcmp(last_selection, 'alt')
set(fig, 'Pointer', 'arrow');
set(fig, 'WindowButtonMotionFcn','');
set(fig, 'WindowButtonUpFcn','');
else
return;
end
end
end
function keyfunc(src, callback)
keypressed = get(fig, 'CurrentCharacter');
if keypressed == 13
uiresume(fig);
end
end
end
Q1-可以绘制图像,但坐标系统在图的左上边缘具有零。如何将其移动到图像的左上方?
问题2-如何实现项目编号3(如果用户要编辑这些点之一,则他/她必须单击所需的点并将其拖动到他/她想要的位置)?
谢谢大家,
而不是得到CurrentPoint
的身影,你要得到CurrentPoint
的的轴对象。
cp = get(gca, 'CurrentPoint');
% Then get just the x/y position
cp = cp(1,1:2);
对于您的关于拖动点的问题的第二部分。您可能需要执行以下操作。
设置ButtonDownFcn
plot对象以触发回调函数
在此函数中,找到绘图上最接近单击点的点。
跟踪该索引并进行设置,WindowButtonMotionFcn
以便每当您移动鼠标时,该点便会移动到该位置。
进行设置,WindowButtonUpFcn
以便在释放鼠标按钮时将WindowButtonMotionFcn
其重置。
这样的事情应该会给你一个想法。
set(hl, 'ButtonDownFcn', @(src,evnt)clickedLine(src))
function clickedLine(src, evnt)
cp = get(ancestor(src, 'axes'), 'CurrentPoint');
xdata = get(src, 'XData');
ydata = get(src, 'YData');
% Find the index of the closest point
[~, ind] = min((xdata - cp(1,1)).^2 + (ydata - cp(1,2)).^2);
hfig = ancestor(src, 'figure');
switch get(hfig, 'SelectionType')
case 'alt'
% Right click deletes a point
xdata(ind) = [];
ydata(ind) = [];
set(src, 'XData', xdata, 'YData', ydata);
otherwise
% Set the WindowMotionFcn callback to track this point
set(hfig, 'WindowButtonMotionFcn', @(s,e)dragPoint(src,ind), ...
'WindowButtonUpFcn', @(s,e)stopDrag(s));
end
end
function dragPoint(plt, index)
xdata = get(plt, 'xdata');
ydata = get(plt, 'ydata');
% Get the current point
cp = get(ancestor(plt, 'axes'), 'CurrentPoint');
xdata(index) = cp(1,1);
ydata(index) = cp(1,2);
% Update the data and refresh
set(plt, 'XData', xdata, 'YData', ydata);
drawnow
end
function stopDrag(hfig)
set(hfig, 'WindowButtonMotionFcn', '', ...
'WindowButtonUpFcn', '');
end
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句