How to draw a line on an image in matlab?

2019-01-11 01:35发布

I have two points lets say:

  • P(x,y) [point lies at the top of image]
  • P'(x',y') [point lies at bottom of image]

Now i want to draw a line betwen these two points....and the line should appear on image means should be visible.

how to do this????

标签: matlab line draw
6条回答
地球回转人心会变
2楼-- · 2019-01-11 01:56

The simplest way to draw a line onto an image is to use 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)

If you want a different color, either change the letter to any of rgbcmykw, or use RGB triplets (red is [1 0 0]). Have a look at the lineseries properties for more formatting options.

查看更多
萌系小妹纸
3楼-- · 2019-01-11 01:56

Starting with version R2014a you can use insertShape as follows:

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

You can also draw multiple lines with the same command, but x1,x2,y2,y3 must be column vectors with each row representing a new line.

insertShape also allows you to draw rectangles, circles, and polygons.

查看更多
ら.Afraid
4楼-- · 2019-01-11 02:07

Like this:

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

Where y is the "down" direction and x is the "right" direction in the image. Change the color and width as necessary to be visible.

查看更多
萌系小妹纸
5楼-- · 2019-01-11 02:07

If you have the Computer Vision toolbox. You can simply use shapeInserter.

Check out http://www.mathworks.com/help/vision/ref/vision.shapeinserter-class.html

To specify lines, you have to use the line below. Otherwise, you may get a rectangle

Example:

%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);

Take a look at the properties to see what you can edit.

查看更多
叛逆
6楼-- · 2019-01-11 02:12

You could download and use hline and vline in conjunction with hold on, using the techniques from visiting Steve on Image Processing. Or just use his techniques. Either way it works.

查看更多
贼婆χ
7楼-- · 2019-01-11 02:23
load clown
image(X)
colormap(map)
c = size(X,2)
mid = round(c/2)
X(:,mid) = 1
image(X)
查看更多
登录 后发表回答