OpenCV Q&A Forum - RSS feedhttp://answers.opencv.org/questions/OpenCV answersenCopyright <a href="http://www.opencv.org">OpenCV foundation</a>, 2012-2018.Sun, 31 Jan 2016 22:21:13 -0600Sobel derivatives in the 45 and 135 degree directionhttp://answers.opencv.org/question/387/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((Y*sin(alpha))^2 + (X*cos(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.
Mon, 16 Jul 2012 04:42:56 -0500http://answers.opencv.org/question/387/sobel-derivatives-in-the-45-and-135-degree-direction/Comment by tarchon for <p>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)?</p>
<p>Thanks</p>
<p>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.</p>
<p>I've come across :
sqrt((Y<em>sin(alpha))^2 + (X</em>cos(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?</p>
<p>Many thanks.</p>
http://answers.opencv.org/question/387/sobel-derivatives-in-the-45-and-135-degree-direction/?comment=86109#post-id-86109For 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.Sun, 31 Jan 2016 22:17:16 -0600http://answers.opencv.org/question/387/sobel-derivatives-in-the-45-and-135-degree-direction/?comment=86109#post-id-86109Comment by tarchon for <p>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)?</p>
<p>Thanks</p>
<p>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.</p>
<p>I've come across :
sqrt((Y<em>sin(alpha))^2 + (X</em>cos(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?</p>
<p>Many thanks.</p>
http://answers.opencv.org/question/387/sobel-derivatives-in-the-45-and-135-degree-direction/?comment=86110#post-id-86110Kind 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);Sun, 31 Jan 2016 22:21:13 -0600http://answers.opencv.org/question/387/sobel-derivatives-in-the-45-and-135-degree-direction/?comment=86110#post-id-86110Answer by Misha for <p>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)?</p>
<p>Thanks</p>
<p>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.</p>
<p>I've come across :
sqrt((Y<em>sin(alpha))^2 + (X</em>cos(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?</p>
<p>Many thanks.</p>
http://answers.opencv.org/question/387/sobel-derivatives-in-the-45-and-135-degree-direction/?answer=629#post-id-629Warping 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.Sun, 22 Jul 2012 11:33:37 -0500http://answers.opencv.org/question/387/sobel-derivatives-in-the-45-and-135-degree-direction/?answer=629#post-id-629Comment by Puneet for <p>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:</p>
<p>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.</p>
<p>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.</p>
<p>This approximation good for almost any practical purpose, so don't hesitate to use it.</p>
http://answers.opencv.org/question/387/sobel-derivatives-in-the-45-and-135-degree-direction/?comment=35253#post-id-35253but 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?Wed, 18 Jun 2014 09:28:49 -0500http://answers.opencv.org/question/387/sobel-derivatives-in-the-45-and-135-degree-direction/?comment=35253#post-id-35253Answer by Adi for <p>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)?</p>
<p>Thanks</p>
<p>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.</p>
<p>I've come across :
sqrt((Y<em>sin(alpha))^2 + (X</em>cos(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?</p>
<p>Many thanks.</p>
http://answers.opencv.org/question/387/sobel-derivatives-in-the-45-and-135-degree-direction/?answer=390#post-id-390The [Sobel derivatives are separable](https://en.wikipedia.org/wiki/Sobel_operator).
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.
Mon, 16 Jul 2012 05:06:26 -0500http://answers.opencv.org/question/387/sobel-derivatives-in-the-45-and-135-degree-direction/?answer=390#post-id-390Comment by Kirill Kornyakov for <p>The <a href="https://en.wikipedia.org/wiki/Sobel_operator">Sobel derivatives are separable</a>. <br>
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. </p>
http://answers.opencv.org/question/387/sobel-derivatives-in-the-45-and-135-degree-direction/?comment=392#post-id-392Or 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.Mon, 16 Jul 2012 05:36:48 -0500http://answers.opencv.org/question/387/sobel-derivatives-in-the-45-and-135-degree-direction/?comment=392#post-id-392Comment by Adi for <p>The <a href="https://en.wikipedia.org/wiki/Sobel_operator">Sobel derivatives are separable</a>. <br>
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. </p>
http://answers.opencv.org/question/387/sobel-derivatives-in-the-45-and-135-degree-direction/?comment=429#post-id-429True :-), though I am not sure it will be more efficient as warping is an expensive operation.Tue, 17 Jul 2012 01:43:06 -0500http://answers.opencv.org/question/387/sobel-derivatives-in-the-45-and-135-degree-direction/?comment=429#post-id-429Comment by Kirill Kornyakov for <p>The <a href="https://en.wikipedia.org/wiki/Sobel_operator">Sobel derivatives are separable</a>. <br>
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. </p>
http://answers.opencv.org/question/387/sobel-derivatives-in-the-45-and-135-degree-direction/?comment=432#post-id-432Completely agreeTue, 17 Jul 2012 02:02:51 -0500http://answers.opencv.org/question/387/sobel-derivatives-in-the-45-and-135-degree-direction/?comment=432#post-id-432