21

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????

Justin Ethier
  • 131,333
  • 52
  • 229
  • 284
chee
  • 979
  • 7
  • 18
  • 27
  • 1
    I believe what you're asking has been covered before: http://stackoverflow.com/questions/1940833/how-do-i-create-an-image-matrix-with-a-line-drawn-in-it-in-matlab, http://stackoverflow.com/questions/2464637/matlab-drawing-a-line-over-a-black-and-white-image, http://stackoverflow.com/questions/3178336/matlab-how-to-plot-x-y-on-image-and-save, http://stackoverflow.com/questions/575475/how-can-i-save-an-altered-image-in-matlab – gnovice Aug 20 '10 at 20:47
  • It's almost an exact duplicate, but not quite. – Jonas Aug 20 '10 at 20:49
  • http://stackoverflow.com/questions/2464637/matlab-drawing-a-line-over-a-black-and-white-image this link you gave works for binary images only...and my image is not binary. and rest of the links are not for my use.so i need something different. – chee Aug 21 '10 at 07:41
  • Have a look at the following two links: http://blogs.mathworks.com/loren/2010/07/22/graphical-display-techniques-part-1/ http://blogs.mathworks.com/loren/2010/08/05/graphical-display-techniques-part-2/ – zellus Aug 20 '10 at 23:10

6 Answers6

19

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.

Jonas
  • 74,690
  • 10
  • 137
  • 177
12

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.

Juan Terven
  • 224
  • 2
  • 4
6

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.

rayryeng
  • 102,964
  • 22
  • 184
  • 193
Luke
  • 5,329
  • 2
  • 29
  • 34
1

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.

I L
  • 186
  • 7
1
load clown
image(X)
colormap(map)
c = size(X,2)
mid = round(c/2)
X(:,mid) = 1
image(X)
MatlabDoug
  • 5,704
  • 1
  • 24
  • 36
0

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.

Community
  • 1
  • 1
rownage
  • 2,392
  • 3
  • 22
  • 31