Ask Your Question

Sobel derivatives in the 45 and 135 degree direction

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

Lassan gravatar image

updated 2017-08-28 05:27:36 -0500

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)?


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


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 -0500 )edit

Kind of like this

getDerivKernels(kx,ky, 0,1, 7);
tarchon gravatar imagetarchon ( 2016-01-31 22:21:13 -0500 )edit

2 answers

Sort by ยป oldest newest most voted

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

Misha gravatar image

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

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


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 -0500 )edit

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

Adi gravatar image

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

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



Or you can rotate image beforehand :) warpAffine function can help you to do this:

Kirill Kornyakov gravatar imageKirill Kornyakov ( 2012-07-16 05:36:48 -0500 )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 -0500 )edit

Completely agree

Kirill Kornyakov gravatar imageKirill Kornyakov ( 2012-07-17 02:02:51 -0500 )edit
Login/Signup to Answer

Question Tools


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

Seen: 2,689 times

Last updated: Jul 22 '12