如何在matlab中在图像上画一条线?

che*_*hee 19 matlab line draw

我有两点可以说:

  • P(x,y)[点位于图像的顶部]
  • P'(x',y')[点位于图像底部]

现在我想在这两点之间划一条线......并且线应该出现在图像上意味着应该是可见的.

这该怎么做????

Jon*_*nas 16

在线条上绘制线条的最简单方法是使用PLOT.

%# read and display image
img = imread('autumn.tif');
figure,imshow(img)

%# make sure the image doesn't disappear if we plot something else
hold on

%# define points (in matrix coordinates)
p1 = [10,100];
p2 = [100,20];

%# plot the points.
%# Note that depending on the definition of the points,
%# you may have to swap x and y
plot([p1(2),p2(2)],[p1(1),p2(1)],'Color','r','LineWidth',2)
Run Code Online (Sandbox Code Playgroud)

如果您想要不同的颜色,请将字母更改为任何颜色,rgbcmykw或使用RGB三元组(红色[1 0 0]).查看lineseries属性以获取更多格式选项.


小智 10

从版本R2014a开始,您可以使用insertShape,如下所示:

img = insertShape(img,'Line',[x1 y1 x2 y2],'LineWidth',2,'Color','blue');

您也可以使用相同的命令绘制多行,但x1,x2,y2,y3必须是列向量,每行代表一个新行.

insertShape还允许您绘制矩形,圆形和多边形.


Luk*_*uke 6

像这样:

figure;
hold on;
imagesc(img);
line([x1,x2],[y1,y2],'Color','r','LineWidth',2)
hold off
Run Code Online (Sandbox Code Playgroud)

其中y是"向下"方向,x是图像中的"向右"方向.根据需要更改颜色和宽度以使其可见.