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

时间:2010-08-20 18:40:14

标签: matlab line draw

我有两点可以说:

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

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

怎么做????

6 个答案:

答案 0 :(得分: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)

如果您想要不同的颜色,请将字母更改为rgbcmykw中的任意一个,或使用RGB三元组(红色为[1 0 0])。有关更多格式选项,请查看lineseries properties

答案 1 :(得分:9)

从版本R2014a开始,您可以按如下方式使用insertShape:

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

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

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

答案 2 :(得分:6)

像这样:

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

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

答案 3 :(得分:1)

load clown
image(X)
colormap(map)
c = size(X,2)
mid = round(c/2)
X(:,mid) = 1
image(X)

答案 4 :(得分:1)

如果您有计算机视觉工具箱。你可以简单地使用shapeInserter。

查看http://www.mathworks.com/help/vision/ref/vision.shapeinserter-class.html

要指定行,您必须使用下面的行。否则,您可能会得到一个矩形

示例:

%draw a line from point (100,100) to (200,200) on an image saved as nextFrame

line = int32([100 100  200 200]);
shapeInserter = vision.ShapeInserter('Shape', 'Lines');
nextFrame = step(shapeInserter, nextFrame, line);

查看属性以查看可以编辑的内容。

答案 5 :(得分:0)

您可以使用访问hline and vline的技巧,与hold on一起下载并使用Steve on Image Processing。或者只是使用他的技术。无论哪种方式都有效。

相关问题