draw a line on an image
Afficher commentaires plus anciens
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
Réponse acceptée
Plus de réponses (1)
David Young
le 3 Oct 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 commentaires
Fatma Gargouri
le 3 Oct 2011
Image Analyst
le 3 Oct 2011
Issue the hold on command
im=imread('img.jpg');
imshow (im);
hold on;
line ([50, 40],[100, 100]);
Walter Roberson
le 3 Oct 2011
You never need to use "hold on" when you are using line() afterwards.
However, you do need "hold on" if you are using imshow() after line()
Fatma Gargouri
le 4 Oct 2011
Image Analyst
le 3 Juin 2015
What does that mean? Post your reply/clarification in a new question. When you say "see the line" do you mean that you want to burn the line into the image instead of being plotted in the graphical overlay? Or do you want to print out the pixel values of the image underneath the line into the command window or a file?
Jes
le 3 Juin 2015
Yes I want to burn the line into the image instead of graphical overlay
Image Analyst
le 3 Juin 2015
See attached demos that do that.
Somsubhro Chaudhuri
le 15 Août 2017
Hello - follow up question.
Can I add a line that is defined in the code rather than being an interactive line?
Image Analyst
le 15 Août 2017
Of course. Just hard-code in the x and y values of the endpoints of the line.
Somsubhro Chaudhuri
le 16 Août 2017
Modifié(e) : Somsubhro Chaudhuri
le 16 Août 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
le 16 Août 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.
Catégories
En savoir plus sur Image Thresholding dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!