Machine Vision

cancel
Showing results for 
Search instead for 
Did you mean: 

What is the correct way to compute the orientation of the gradient in Sobel operator?

Solved!
Go to solution

I've been trying to find the correct way to compute the orientation of the gradient in Sobel operator. The formula

atan[x, y] = Math.Atan2((respondY), (respondX)) * (180.00 / Math.PI);
if (atan[x, y] < 0) atan[x, y] = atan[x, y] + 180;

seems clear enough to understand and it looks like it should work. And it works for horizontal and vertical lines. But if the input image has a diagonal, then the computed angle is incorrect.

This is my code:

        double[,] respond = new double[4, 4];
        double[,] atan = new double[4, 4];
        double respondX;
        double respondY;
        int[,] Gx = new int[3, 3] { { -1, 0, 1 }, { -2, 0, 2 }, { -1, 0, 1 } };
        int[,] Gy = new int[3, 3] { { -1, -2, -1 }, { 0, 0, 0 }, { 1, 2, 1 } };

        int[,] image = new int[6, 6] { {255, 255, 255, 255, 255, 0},
                                       {255,255,255,   255, 0,   255},
                                       {255,255,255,   0,   255, 255},
                                       {255,255,0,     255, 255, 255},
                                       {255,0,  255,   255, 255, 255},
                                       {0,  255,255,   255, 255, 255},
                                     };
        for (int x = 0; x < atan.GetLength(0); x++)
            for (int y = 0; y < atan.GetLength(1); y++)
            {
                respondX = 0.0;
                respondY = 0.0;

                for (int u = 0; u < 3; u++)
                    for (int v = 0; v < 3; v++)
                    {
                        respondX += Gx[u, v] * image[u + x, v + y];
                        respondY += Gy[u, v] * image[u + x, v + y];
                    }

                // finding the magnitude
                atan[x, y] = (Math.Atan2((respondY), (respondX)) * (180.00 / Math.PI));
                if (atan[x, y] < 0) atan[x, y] = atan[x, y] + 180;

                if ((atan[x, y] > 0 && atan[x, y] < 22.5) || (atan[x, y] > 157.5 && atan[x, y] <= 180))
                {
                    atan[x, y] = 0;
                }
                else if (atan[x, y] > 22.5 && atan[x, y] < 67.5)
                {
                    atan[x, y] = 45;
                }
                else if (atan[x, y] > 67.5 && atan[x, y] < 112.5)
                {
                    atan[x, y] = 90;
                }
                else if (atan[x, y] > 112.5 && atan[x, y] < 157.5)
                {
                    atan[x, y] = 135;
                }
                respond[x, y] = Math.Sqrt(respondX * respondX + respondY * respondY); 
            }

And as you can see from the image array the edge is "travelling" from the bottom left to the upper right corner (what is equivalent to 45°). So, because the angle that I get (the gradient orientation) should be perpendicular to the edge, the result should cointain a value of 135°. But I get the next values in the atan array:

0   45  45  0   
45  45  0   45  
45  0   45  45  
0   45  45  0

What am I doing wrong?

Thanks in advance!

0 Kudos
Message 1 of 3
(4,203 Views)
Solution
Accepted by Tanya_K

You are doing it right, but you misinterpreted your axis directions.

 

X is positive to the right.  Y is positive down.  Therefore, a 45 degree angle is down and to the right.  Your line of zeroes is at a 135 degree angle, which would give you a 45 degree gradient.  Angles are measured starting from the horizontal, and are positive as you go clockwise (down).

 

Bruce

 

 

Bruce Ammons
Ammons Engineering
0 Kudos
Message 2 of 3
(4,172 Views)

Thank you! You helped me very much!
P.S. sorry for such a late answer

0 Kudos
Message 3 of 3
(4,046 Views)