draw line in a picture

Hello guys does anyone know how can i draw a line in a picture.
For example i want to draw a 45 degree red line in the next picture starting from the top left corner of the picture and ending n the other end of the picture.

 Accepted Answer

img = imread('https://www.mathworks.com/matlabcentral/answers/uploaded_files/562881/image.jpeg');
sz = size(img);
endx = min(sz(1), sz(2));
h(1) = image(img);
hold on
h(2) = plot([1 endx], [1 endx], 'r');
hold off
Or if you need the line "burned in" to the image:
img = imread('https://www.mathworks.com/matlabcentral/answers/uploaded_files/562881/image.jpeg');
sz = size(img);
endx = min(sz(1), sz(2));
nimg = insertShape(img, 'Line', [1 1 endx endx], 'Color', 'r');
h(1) = image(nimg);
The difference between these two is that in the first version, the red line is only visible in what is drawn, with it not being made part of the image array; h will end up as a vector containing the handle to the image display object that only knows about the gray scale image, and a handle to the line display object. In the second version, the red line is part of the array stored in nimg, suitable for further processing (or writing to a file), and h will be the handle of the image display object that only knows about displaying the array that inherently has the red line as part of the data.

11 Comments

thanks for the answer!
how can i change this code if the angle is not 45degrees and its lets say 30?
If the purpose is to display only then see refline()
no its not only display i want the line part of the pic
Note that in the below, I had to decide which sides to make the lines relative to. I choose to use the normal cartesian coordinate system, lower row number corresponds to lower y, to bottom. This is the opposite of the semi-convention for images, that lower row numbers correspond to top of the image. That is why I specifically set ydir to normal.
It is not requried that you set ydir to normal, but when you look at the results, keep this in mind when you interpret them.
format long g
line_angles_deg = [30 45 80]; %degrees
line_cols = {'red', 'black', 'blue', 'green'}; %one-character codes not accepted for black
img = imread('https://www.mathworks.com/matlabcentral/answers/uploaded_files/562881/image.jpeg');
sz = size(img);
nimg = img;
for k = 1 : length(line_angles_deg)
theta_deg = line_angles_deg(k);
if sz(1) > tand(theta_deg) * sz(2)
%intersection is right side
endx = sz(2);
endy = endx .* tand(theta_deg);
else
%intersection is top
endy = sz(1);
endx = endy ./ tand(theta_deg);
end
nimg = insertShape(nimg, 'Line', [0 0 endx endy], 'Color', line_cols{k});
end
image(nimg); set(gca, 'ydir', 'normal'); box on
I didn't know about insertShape, neat!
thanks walter!!!
when i run your code i get this error
Error using insertShape>errIf2 (line 710)
The POSITION matrix must have at least 4 columns for shape Line
Error in insertShape>crossCheckShapePosition (line 438)
errIf2(errCond, 'vision:insertShape:posPolyNumPtsLT2', ...
Error in insertShape>crossCheckInputs (line 389)
crossCheckShapePosition(shape, position);
Error in insertShape>validateAndParseInputs (line 252)
crossCheckInputs(shape2, position, color);
Error in insertShape (line 109)
validateAndParseInputs(I, shape, position, varargin{:});
Error in addline (line 27)
nimg = insertShape(nimg, 'Line', [0 0; endx endy], 'Color', line_cols{k});
i used your first code and it worked
img = imread('1.jpg');
img=imcrop(img);
sz = size(img);
endx = min(sz(1), sz(2));
h(1) = image(img);
hold on
h(2) = plot([1 endx], [1 endx], 'r');
hold off
do you know how can i find the pixel values of the red line?
i need to plot the pixel values in a x-y graph but i dont know how
You should use improfile() using those coordinates against the array img
Thank you Walter for the help!
I corrected the insertShape call,
nimg = insertShape(nimg, 'Line', [0 0 endx endy], 'Color', line_cols{k});

Sign in to comment.

More Answers (1)

MG
MG on 26 Mar 2021
Would this work for you:
hold on
ax=axis;
x = ax(1:2);
y = ax([3 4]); %might want to change cordinates (check with 'axis on')
line(x,y,'Color','red','LineStyle','-')

4 Comments

What if the axis limits do not start and end at the image bounds or what if the image was plotted using a function like imshow() that produces it's own axes?
MG
MG on 26 Mar 2021
See Walter's reply. Our two replies were written (almost) simultaneoulsy, and I was not yet awae of his, leaving my addional reply redundant.
Walter Roberson
Walter Roberson on 26 Mar 2021
Edited: Walter Roberson on 26 Mar 2021
In my code, the approaches do not change if you use imshow() instead of image()
thanks for the answer michael

Sign in to comment.

Categories

Tags

Community Treasure Hunt

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

Start Hunting!