Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I interpret the orientation of the gradient when using imgradient in MATLAB?

I am finding the gradient of an image. For now I am simply using a 5 x 5 image. I am more interested in finding the direction of the gradient but I am not getting the results manually on paper as I get them using MATLAB function imgradient. Please refer to the following images to know more about the input images and the Sobel filter that is used here to find the gradient of an image. One of the 3 x 3 sobel operator used here is the one that I get using the function

f1 = fspecial('sobel');

and the other one is obtained by just transposing the f1.

Please note that I am trying to find the direction of only one pixel here that is rounded by red color. Here in the first two cases my result matches with that i obtain using imgradient function but in the third case imgradient gives -135 degree whereas I am getting it to be -45. Please help me find the error.

Case one and case two

Case three (Mismatch in results)

Also please explain how to interpret the following gradient directions as shown in the follwing image.

enter image description here

like image 948
Navdeep Sony Avatar asked Feb 06 '16 06:02

Navdeep Sony


People also ask

What is the orientation of a gradient?

The gradient orientation is used to determine in which direction the change in intensity is pointing. As the name suggests, the gradient orientation will give us an angle or ? that we can use to quantify the direction of the change.

What is gradient orientation in image processing?

An image gradient is a directional change in the intensity or color in an image. The gradient of the image is one of the fundamental building blocks in image processing. For example, the Canny edge detector uses image gradient for edge detection.

What is the orientation relationship between an edge and its gradient?

The "true" edge point is the point at which slope is steepest along the gradient corresponding to the edge of an object. The gradient will be steepest when it is perpendicular to the edge of the object.

What does Imgradient do in Matlab?

[ Gmag , Gdir ] = imgradient( Gx , Gy ) returns the gradient magnitude and direction from the directional gradients Gx and Gy in the x and y directions, respectively.


1 Answers

Your calculations are correct but it is highly recommended that you don't use the atan(y/x) definition because this calculation is not cognizant of the quadrant that the angle of the gradient resides in. Doing atan(y/x) with your components would falsely report the angle to be -45 degrees when that isn't correct. You should use atan2 instead.

Now the internals of imgradient are quite straight forward. I'd like to point out that the angle reported by imgradient is assuming that the y coordinate is increasing from bottom to top. In addition, imgradient should report the angle of orientation that is pointing to the greatest rate of change. In the case of images, this points in the direction where we progress from dark pixels to light pixels.

First a call to imgradientxy is called and a call to fspecial('sobel') is made if you provide the sobel flag to imgradient. In fact, this portion of imgradientxy is what is important to remember (starting at line 75: MATLAB R2015a):

case 'sobel'
    h = -fspecial('sobel'); %// Align mask correctly along the x- and y- axes
    Gx = imfilter(I,h','replicate'); %'
    if nargout > 1
        Gy = imfilter(I,h,'replicate');
    end

Notice that the negative of the output of fspecial is performed as well as the comment provided at that line. This is to ensure that the mask to detect horizontal edges (i.e. Gy) is y-down (as it is commonly known in computer graphics). Specifically, the origin of the image is at the top-left corner and not the bottom left.

This is a pictorial representation of how the coordinate system is laid out in y-down:

Source: Wikipedia - Rotation Matrix

As such, when finding the orientation there is an additional requirement to ensure that the angle of the orientation of the gradient is with respect to the y-up coordinate system which is what we're used to. Therefore when you are finding the angle of orientation of the gradient, you need to negate the y coordinate before calculating the angle so that the angle is with respect to the standard convention instead.

Pursuing the definition of the gradient that you seek is the conventional system of the y coordinate increasing from bottom to top. The negation is required and in fact if you examine the source code for imgradient, this is precisely what is being done at line 127 of the code (version R2015a):

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

You may be asking yourself why there is a need to negate the mask and again negate the y coordinate after to find the orientation. The reason why is because the modified mask is required to properly capture the magnitude of the gradient and so we negate the mask once and find the gradient magnitude and then we negate the y coordinate so that we can find the angle with respect to the conventional coordinate system.

In your case, given that Gx = 765 and Gy = -765, substituting these quantities into the above equation yields:

>> Gy = 765;
>> Gx = -765;
>> Gdir = atan2(-Gy,Gx)*180/pi

Gdir =

  -135

This makes sense because the gradient direction corresponds to the direction towards the greatest rate of change. -135 degrees means that we're pointing to the south west which does make sense as we are progressing from dark pixels to light pixels.

Now if you consult your third example image, the angles reported by imgradient are indeed correct. Simply draw a line from the dark area to the light area and see what angle it makes with the x axis where it is aligned with the columns increasing towards the right. The first angle of +90 degrees makes sense as we are moving from bottom to top to follow the dark area and light. This is a similar situation with the situation where the image is reversed. The third situation is what we have seen before and the fourth situation is simply the third situation rotated by 180 degrees and so naturally the angle of orientation from dark to light is now +45 degrees as opposed to -135 degrees previously.

like image 160
rayryeng Avatar answered Oct 19 '22 04:10

rayryeng