【问题标题】:How to draw a line on an image in matlab?matlab如何在图片上画一条线?
【发布时间】:2010-08-20 18:40:13
【问题描述】:

我有两点可以说:

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

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

怎么做????

【问题讨论】:

标签: matlab line draw


【解决方案1】:

在图像上画线的最简单方法是使用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 了解更多格式选项。

【讨论】:

  • chee: a) 这是一个不同的问题,b) 对直线使用点斜率公式。
【解决方案2】:

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

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

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

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

【讨论】:

  • 这仅适用于您拥有计算机视觉工具箱的情况。你能推荐一个不使用它的方法吗?
  • 虚线怎么用?
【解决方案3】:

像这样:

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

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

【讨论】:

    【解决方案4】:
    load clown
    image(X)
    colormap(map)
    c = size(X,2)
    mid = round(c/2)
    X(:,mid) = 1
    image(X)
    

    【讨论】:

    • 如果是彩色图片,请使用X(:,mid,:) = [1 1 1];
    • 这是一条垂直线。坡度是无限的。
    • 用与背景图像颜色相反的颜色 (RGB) 沿线画一条线会很容易(不难)但更有趣...
    【解决方案5】:

    如果您有计算机视觉工具箱。您可以简单地使用 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);
    

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

    【讨论】:

      【解决方案6】:

      您可以使用访问Steve on Image Processing 中的技术下载和使用hline and vlinehold on。或者只是使用他的技术。无论哪种方式都有效。

      【讨论】:

        猜你喜欢
        • 2011-01-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-04-20
        • 2014-10-25
        • 2016-06-19
        • 1970-01-01
        • 2011-09-30
        相关资源
        最近更新 更多