Filter löschen
Filter löschen

draw a line on an image

186 Ansichten (letzte 30 Tage)
Fatma Gargouri
Fatma Gargouri am 3 Okt. 2011
Kommentiert: Walter Roberson am 12 Jan. 2021
Hello
I want draw a line on an image, just by giving the coordinates of the extremities.
I tryed the function line(...) but it didn't give me the right result

Akzeptierte Antwort

Fangjun Jiang
Fangjun Jiang am 3 Okt. 2011
Pay attention of the syntax of line().
line([x1,x2],[y1,y2]), not line([x1,y1],[x2,y2]);
figure(1);line([0 1],[0 1]); will show a line
figure(2);line([0 0],[1 1]); won't show a line
  6 Kommentare
Amber Elferink
Amber Elferink am 11 Jan. 2021
you have to do hold off at the end again (for next viewers)
Walter Roberson
Walter Roberson am 12 Jan. 2021
I suspect Sumit either wanted to know about getframe() https://www.mathworks.com/help/matlab/ref/getframe.html or else wanted to know how to draw a line directly into an array with the intent to save the array as an image but without having to display on the screen.
The easiest way to draw a line directly into an array is to use Computer Vision Toolbox insertShape()

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

David Young
David Young am 3 Okt. 2011
Perhaps your problem is that figure coordinates and array indices have different conventions. So, for example
imshow(im)
line([50 100], [30 30])
draws a line from im(30, 50) to im(30, 100).
Arrays are indexed by (ROW,COLUMN). Positions in a figure are addressed by (X,Y). When you display an array as an image, using the usual conventions, ROW corresponds to Y and COLUMN corresponds to X.
  12 Kommentare
Somsubhro Chaudhuri
Somsubhro Chaudhuri am 16 Aug. 2017
Bearbeitet: Somsubhro Chaudhuri am 16 Aug. 2017
Thank you @Image Analyst. I managed doing that now. I now have a binary image with a line between two points (attached). Could you please suggest a way to only change the area right below the line to black?
I was thinking of defining the equation of the line, and then running a for loop to cover these pixels, but I want to enquire about a shorter way of doing this as I'll have to run this code for a very large number of images.
Thank you! Regards, Somsubhro
Image Analyst
Image Analyst am 16 Aug. 2017
You could take the endpoints of the line, then add on the bottom of the image coordinates, then call poly2mask() to get a binary image mask. Then use that to erase the image in that quadrilateral.

Melden Sie sich an, um zu kommentieren.

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by