ct_
ct_

Reputation: 1229

Sobel Edge Detection, edge orientation

I've implemented a Sobel Edge Detector and had some questions about computing edge orientations.

I'm using this function to compute edge intensities after having done the sobel kernel convolution.

Gxy = sqrt( pow(Gx, 2) + pow(Gy,2) )

Where Gx is sum of the convolution for the sobel kernel in the X direction and Gy is sum of the convolution for the sobel kernel in the Y direction. (note the sobel kernel in the X and Y direction are different kernels)

Y kernel:

X kernel:

when I try to compute the edge orientation (theta is in degrees), I'm using the following rules:

all my documentation is telling me the angles should be > 0 and < 360 and I continue to get edges with negative value orientations. Is there something I'm doing incorrectly when computing theta or my convolution? Or should i just add 360 or 180 to negative theta values?

thanks in advance,

Upvotes: 1

Views: 7550

Answers (2)

Yonatan Simson
Yonatan Simson

Reputation: 2575

If you are using OpenCV, C++ you could do the following:

    Mat gray = some grayscale image;

Calculate edge gradients in x/y axis'

    Mat dx(gray.rows, gray.cols, CV_16SC1);
    Mat dy(gray.rows, gray.cols, CV_16SC1);
    int aperture_size=3;
    Sobel(gray, dx, CV_32FC1, 1, 0, aperture_size, 1, 0, BORDER_REPLICATE);
    Sobel(gray, dy, CV_32FC1, 0, 1, aperture_size, 1, 0, BORDER_REPLICATE);

Calculate the angle and magnitude using OpenCV's cartToPolar

    Mat Mag(gray.size(), CV_32FC1);
    Mat Angle(gray.size(), CV_32FC1);
    cartToPolar(dx, dy, Mag, Angle, true);

Upvotes: 1

mpenkov
mpenkov

Reputation: 21922

It's hard to answer your question precisely because you haven't mentioned exactly how you calculate the arctan function.

For example, if you're using the standard library's atan function, then negative angles are to be expected.

Furthermore, you'll notice that atan with a single argument can only ever return values in the first and fourth quadrants (because, for example, tan 45 == tan 225 when using degrees).

If you really want an angle in one of the four quadrants (you should really ask yourself if this matters for your application), then have a look at atan2.

Upvotes: 3

Related Questions