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

 채택된 답변

Fangjun Jiang
Fangjun Jiang 2011년 10월 3일

7 개 추천

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

Fatma Gargouri
Fatma Gargouri 2011년 10월 3일
hi
yes I know
but the problem is that the line is shown alone and not on an image
Fangjun Jiang
Fangjun Jiang 2011년 10월 3일
use "hold on"
Fatma Gargouri
Fatma Gargouri 2011년 10월 4일
thank you :))
Sumit Nishar
Sumit Nishar 2019년 10월 22일
then how to save the image after hold on
....as its unable to save the image
Amber Elferink
Amber Elferink 2021년 1월 11일
you have to do hold off at the end again (for next viewers)
Walter Roberson
Walter Roberson 2021년 1월 12일
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()

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

David Young
David Young 2011년 10월 3일

2 개 추천

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

Fatma Gargouri
Fatma Gargouri 2011년 10월 3일
when I wrote this only the image is shown without line
im=imread('img.jpg');
line ([50, 40],[100, 100]);
imshow (im);
Image Analyst
Image Analyst 2011년 10월 3일
Issue the hold on command
im=imread('img.jpg');
imshow (im);
hold on;
line ([50, 40],[100, 100]);
Walter Roberson
Walter Roberson 2011년 10월 3일
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
Fatma Gargouri 2011년 10월 4일
thank you :))
Jes
Jes 2015년 6월 3일
편집: Jes 2015년 6월 3일
I have a follow up question here. After this line command, how to see the line as a pixel values in the same image?
Image Analyst
Image Analyst 2015년 6월 3일
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
Jes 2015년 6월 3일
Yes I want to burn the line into the image instead of graphical overlay
Somsubhro Chaudhuri
Somsubhro Chaudhuri 2017년 8월 15일
Hello - follow up question.
Can I add a line that is defined in the code rather than being an interactive line?
Image Analyst
Image Analyst 2017년 8월 15일
Of course. Just hard-code in the x and y values of the endpoints of the line.
Somsubhro Chaudhuri
Somsubhro Chaudhuri 2017년 8월 16일
편집: Somsubhro Chaudhuri 2017년 8월 16일
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 2017년 8월 16일
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.

댓글을 달려면 로그인하십시오.

태그

질문:

2011년 10월 3일

댓글:

2021년 1월 12일

Community Treasure Hunt

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

Start Hunting!

Translated by