About imgradient function in Matlab

I am having doubt to the used equation in the function of imgradient. In the line of 127:

    Gdir = atan2(-Gy,Gx)*180/pi; % Radians to degrees

Why the Gy have to be negative?


  • The y-axis is inverted in images (it increases downward instead of upward). This causes the angles to increase clockwise instead of counter-clockwise as you're used to. By flipping the y component of the gradient, this line computes an angle in the "normal" sense.

    Using the graph that @Dan linked in his comment:

    circle with angles on it

    In this graph, y increases upward, and angles increase counter-clockwise. In an image, the coordinate system is flipped. This leads to counter-intuitive angles. Hence they invert the y axis to compute the angle.