# Sobel derivatives in the 45 and 135 degree direction 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 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.

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));
`

Sort by » oldest newest most voted

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.

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

more

1

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

Official site

GitHub

Wiki

Documentation