Ask Your Question
5

Sobel derivatives in the 45 and 135 degree direction

asked 2012-07-16 04:42:56 -0600

Lassan gravatar image

updated 2020-11-28 06:44:31 -0600

Hi, From the documentation, the Sobel edge detector seems to work only for horizontal and vertical direction edges (by specifying 0,1 or 1,0). Is there any way to get diagonal edges (i.e. 45deg and 135deg) with cvSobel (not cvCanny)?

Thanks

UPDATE: Thanks for the suggestions! I can get what I need by rotating the image beforehand, but it is quite inefficient. I need to access the pixels for both the x-yand diagonal45-diagonal135 matrices, so by rotating the image, I incur the penalty of an extra pass over the pixels.

I've come across : sqrt((Ysin(alpha))^2 + (Xcos(alpha))^2) (where Y and X are derivatives in the y and x direction) which supposedly allows the calculation of the derivative in any direction given by alpha. However, when I try this, the results are different to when rotating the image by 45 degrees before hand. Is that expression correct? If not, how can I go about calculating the derivatives I need without rotating the image beforehand?

Many thanks.

edit retag flag offensive close merge delete

Comments

For the 45 and 135, you want one of the "Kirsch" or "compass" operators. It's basically`

-2 -1 0
-1  0 1
 0  1 2

and the various symmetries. In general, what you can do is rotate the Sobel kernel with warpAffine instead of the image. Like turn the lightbulb instead of the ladder. For anything but the orthogonals and diagonals, I'd recommend using a larger kernel than 3x3 though to get good results.

tarchon gravatar imagetarchon ( 2016-01-31 22:17:16 -0600 )edit

Kind of like this

getDerivKernels(kx,ky, 0,1, 7);
k=kx*ky.t();                      
warpAffine(k,kr,getRotationMatrix2D(Point2f(3,3),angle,1.),Size(7,7));
filter2D(ftemp,grad,CV_32F,kr);
tarchon gravatar imagetarchon ( 2016-01-31 22:21:13 -0600 )edit

2 answers

Sort by » oldest newest most voted
2

answered 2012-07-22 11:33:37 -0600

Misha gravatar image

updated 2012-07-22 11:34:45 -0600

Warping the image is a very expensive operation, but if accuracy is important you will have to use it. If you can accept approximation of gradient than you can use formula you mentioned. This formula gives you a good approximation but don't think that the result is exact. Lets see an example to understand why this is happening:

Image 1: White and black regions separated by vertical line. Gradient in X direction is 1, Gradient in Y direction is 0. According to formula gradient in 45 degree direction is 1/sqrt(2), which is also correct. So far so good.

Image 2: White and black region separated by diagonal line. Gradient in X and Y directions is 1/sqrt(2). According to formula the gradient in 45 degree direction is 1/sqrt(sqrt(2)). Oops, it should be 1.

This approximation good for almost any practical purpose, so don't hesitate to use it.

edit flag offensive delete link more

Comments

but since the code is using sqrt((Ysin(alpha))^2 + (Xcos(alpha))^2), it will lead to exactly same values for 45 and 135 degrees. Don't you think the values should be different or am I missing something?

Puneet gravatar imagePuneet ( 2014-06-18 09:28:49 -0600 )edit
3

answered 2012-07-16 05:06:26 -0600

Adi gravatar image

updated 2012-07-16 05:06:42 -0600

The Sobel derivatives are separable.
This means that given Ix and Iy (the image derivatives along the X and Y axes), you can generate the derivatives in any other direction.

edit flag offensive delete link more

Comments

1

Or you can rotate image beforehand :) warpAffine function can help you to do this: http://docs.opencv.org/modules/imgproc/doc/geometric_transformations.html?highlight=warpaffine#warpaffine.

Kirill Kornyakov gravatar imageKirill Kornyakov ( 2012-07-16 05:36:48 -0600 )edit

True :-), though I am not sure it will be more efficient as warping is an expensive operation.

Adi gravatar imageAdi ( 2012-07-17 01:43:06 -0600 )edit

Completely agree

Kirill Kornyakov gravatar imageKirill Kornyakov ( 2012-07-17 02:02:51 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2012-07-16 04:42:56 -0600

Seen: 8,319 times

Last updated: Jul 22 '12