MATLAB手动绘制具有固定点的线?

时间:2015-12-29 18:04:40

标签: matlab

在Matlab中,如果我想在图像上手动绘制一条线,两点不固定, 我可以使用getline()函数,这个函数返回(x1,y1)(x2,y2),它们是终点'坐标。

然而,现在我有一个已知的固定点,比如说(x0,y0)。我希望用户绘制通过这一点的线。有什么方法吗?

我知道我可以让用户只确定另一个点,因此两个点形成一条线。但是,getline()函数有虚线动画,可以让用户在选择过程中清楚地看到整行,但是getpts()没有。因此,用户可能无法精确划线。

有没有什么方法可以让用户用一个固定点绘制一条线。

谢谢!

2 个答案:

答案 0 :(得分:1)

您可以使用ginput。

x1 = 2.5; %fixed x
y1 = 2.5; %fixed y
plot(x1,y1,'o');
ylim([0 5])
xlim([0 5])
hold on
[x2,y2] = ginput(1); %ask for the second point
plot([x1 x2],[y1 y2],'o',[x1 x2],[y1 y2]) %plot the result

修改

对于第二个问题,如果您有图像处理工具箱,则可以使用impoint

x1 = 2.5;
y1 = 2.5;
ax0 = plot(x1,y1,'o');
ylim([0 5])
xlim([0 5])
hold on

mypoint = impoint;

w = waitforbuttonpress;
while w <= 0
w = waitforbuttonpress;
end

mypoint = getPosition(mypoint);
x2 = mypoint(1);
y2 = mypoint(2);

x = [x1 x2];
y = [y1 y2];
ax1 = plot(x,y,'o');
ax2 = plot(x,y);
axis('equal');
delete(ax0);

拖动点后,只需按键盘上的任意位置进行点验证即可。

答案 1 :(得分:1)

如果你想保留&#34;动画&#34;在绘制线条时getline的效果,您可以使用imline function

它允许你画线并调整起点和终点;一旦该行正常,您只需要double-click就可以了。

您还可以添加一个标志来控制该行是否应从预定义的点(如您在问题中)开始。

在以下代码中,imline用于在图表上添加一行。

% Initial point coords
x0=3
y0=3
% Define the flag to control the starting point of the line
%   plot_form_fixed_point=1 ==> the line will start from the pre-defined
%                               starting point, regardless, the adjustment
%                               of the initial point
%   plot_form_fixed_point=0 ==> the pre-defined initial point is not
%                               considered
plot_form_fixed_point=1
% Plot some data (not strictly needed, just to populate the axes)
plot(randi([-3 10],5,1))
hold on
% Plot the first point / marker
plot(x0,y0,'o','markerfacecolor','r','markeredgecolor','r')
grid on
drawnow
% Draw and adjust the line
h = imline;
pos = wait(h)
% Check if the line has to start from the pre-defined starting point
if(plot_form_fixed_point)
   plot([x0 pos(2,1)],[y0 pos(2,2)],'o-r','markerfacecolor','g','markeredgecolor','g')
else
   plot(pos(:,1),pos(:,2),'o-r','markerfacecolor','g','markeredgecolor','g')
end
delete(h)

enter image description here

如果您没有Image Processing toolbox,可以这样使用getline

  • 指向光标&#34;关闭&#34;到起点然后划线。
  • plot指令中指定getline返回的第一个点的预定义点。

如果需要,使用getline,您可以仅left-click double-click% Initial point coords x0=1 y0=2 % Plot the first point / marker plot(x0,y0,'o','markerfacecolor','r','markeredgecolor','r') grid on drawnow % Use "getline to draw the line, the user should use the previously plotted % marker as a refrence stating point [x1,y1]=getline() hold on % Define the flag to control the starting point of the line % plot_form_fixed_point=1 ==> the line will start from the pre-defined % starting point, regardless, the adjustment % of the initial point % plot_form_fixed_point=0 ==> the pre-defined initial point is not % considered plot_form_fixed_point=0 % Plot the line from the first point st by the user discrding the first % point identified by usign "getline" if(~plot_form_fixed_point) plot(x1,y1,'o-r','markerfacecolor','g','markeredgecolor','g') else plot([x0 x1(2:end)'],[y0 y1(2:end)'],'o-r','markerfacecolor','g','markeredgecolor','g') end 来绘制由多个细分构成的行,以结束该过程。)

同样在这种情况下,您可以添加控件,如果该行应从预定义的点(如您在问题中)开始。

std::getline()

enter image description here

enter image description here

希望这有帮助。