Search code examples
matlabmatlab-figure

Draw angle line over an image - Matlab


Why the angle line I draw is not the correct one. instead of the 45 deg I get the 315?

Script:

clc;
clear;
url='http://clipart-library.com/images/Bcgrakezi.png';
I = imread(url);
imshow(I);
hold on;

[y1,x1,z1] = size(I);
cy=y1/2;
cx=x1/2;

sz = 50;
scatter(cx,cy,sz,'d')

lineLength = 250;
angle = 45;
xAngleLine(1) = cx;
yAngleLine(1) = cy;
xAngleLine(2) = xAngleLine(1) + lineLength * cosd(angle);
yAngleLine(2) = yAngleLine(1) + lineLength * sind(angle);
plot(xAngleLine, yAngleLine,'g','LineWidth',5);

Solution

  • When showing images, such as with imshow or image, MATLAB inverts the y axis. This is so the first row of the image data (lowest row index) appears at the top of the plot. To account for this, you therefore need to flip the sign in the second-to-last line of code to a negative:

    yAngleLine(2) = yAngleLine(1) - lineLength * sind(angle);