OpenCV Q&A Forum - RSS feedhttp://answers.opencv.org/questions/OpenCV answersenCopyright <a href="http://www.opencv.org">OpenCV foundation</a>, 2012-2018.Sat, 21 Jul 2012 01:39:19 -0500Weird result while finding anglehttp://answers.opencv.org/question/234/weird-result-while-finding-angle/Hi
I wanted to find the angle of a curve at every pixel. For that, I applied canny edge detection. For test image, i took a filled rectangle. So obviously, canny edge result is just a rectangle.
Then i applied Sobel derivative in x and y direction and to find the angle, i took **arctan(dy/dx)**. ( dx = dx+0.001 to avoid divide by zero)
**For vertical side, i got an angle of 0** which is true since dy=0 in vertical direction and dx is some high value.
**But for horizontal side also, i got zero.** I don't understand why. When i took its corresponding dx,dy values, dx = 0.001... and **dy = 0**. Isn't it wrong to get dy = 0 on the horizontal side of the rectangle?
I checked the dy image, and it clearly shows the top and bottom sides of the rectangle. **But still, why dy = 0 on that side?**
Below is my dx image:
![image description](/upfiles/13419390596711553.png)
My dy image:
![image description](/upfiles/13419390788350963.png)
Tue, 10 Jul 2012 11:52:57 -0500http://answers.opencv.org/question/234/weird-result-while-finding-angle/Comment by Abid Rahman K for <p>Hi</p>
<p>I wanted to find the angle of a curve at every pixel. For that, I applied canny edge detection. For test image, i took a filled rectangle. So obviously, canny edge result is just a rectangle.</p>
<p>Then i applied Sobel derivative in x and y direction and to find the angle, i took <strong>arctan(dy/dx)</strong>. ( dx = dx+0.001 to avoid divide by zero)</p>
<p><strong>For vertical side, i got an angle of 0</strong> which is true since dy=0 in vertical direction and dx is some high value.</p>
<p><strong>But for horizontal side also, i got zero.</strong> I don't understand why. When i took its corresponding dx,dy values, dx = 0.001... and <strong>dy = 0</strong>. Isn't it wrong to get dy = 0 on the horizontal side of the rectangle?</p>
<p>I checked the dy image, and it clearly shows the top and bottom sides of the rectangle. <strong>But still, why dy = 0 on that side?</strong></p>
<p>Below is my dx image:</p>
<p><img alt="image description" src="/upfiles/13419390596711553.png"></p>
<p>My dy image:</p>
<p><img alt="image description" src="/upfiles/13419390788350963.png"></p>
http://answers.opencv.org/question/234/weird-result-while-finding-angle/?comment=246#post-id-246I know. But I get both as zero. that is my question. since equation is arctan(dy/dx), we should get 90 for horizontal sides. ( ie dy is high on horizontal side , and dx = 0 there, check out the images)Wed, 11 Jul 2012 06:47:24 -0500http://answers.opencv.org/question/234/weird-result-while-finding-angle/?comment=246#post-id-246Comment by Daniil Osokin for <p>Hi</p>
<p>I wanted to find the angle of a curve at every pixel. For that, I applied canny edge detection. For test image, i took a filled rectangle. So obviously, canny edge result is just a rectangle.</p>
<p>Then i applied Sobel derivative in x and y direction and to find the angle, i took <strong>arctan(dy/dx)</strong>. ( dx = dx+0.001 to avoid divide by zero)</p>
<p><strong>For vertical side, i got an angle of 0</strong> which is true since dy=0 in vertical direction and dx is some high value.</p>
<p><strong>But for horizontal side also, i got zero.</strong> I don't understand why. When i took its corresponding dx,dy values, dx = 0.001... and <strong>dy = 0</strong>. Isn't it wrong to get dy = 0 on the horizontal side of the rectangle?</p>
<p>I checked the dy image, and it clearly shows the top and bottom sides of the rectangle. <strong>But still, why dy = 0 on that side?</strong></p>
<p>Below is my dx image:</p>
<p><img alt="image description" src="/upfiles/13419390596711553.png"></p>
<p>My dy image:</p>
<p><img alt="image description" src="/upfiles/13419390788350963.png"></p>
http://answers.opencv.org/question/234/weird-result-while-finding-angle/?comment=247#post-id-247Could you explain, what is dx an dy image? Sobel gets an one input array and produces one output. Why you have two images?Wed, 11 Jul 2012 07:17:55 -0500http://answers.opencv.org/question/234/weird-result-while-finding-angle/?comment=247#post-id-247Comment by Daniil Osokin for <p>Hi</p>
<p>I wanted to find the angle of a curve at every pixel. For that, I applied canny edge detection. For test image, i took a filled rectangle. So obviously, canny edge result is just a rectangle.</p>
<p>Then i applied Sobel derivative in x and y direction and to find the angle, i took <strong>arctan(dy/dx)</strong>. ( dx = dx+0.001 to avoid divide by zero)</p>
<p><strong>For vertical side, i got an angle of 0</strong> which is true since dy=0 in vertical direction and dx is some high value.</p>
<p><strong>But for horizontal side also, i got zero.</strong> I don't understand why. When i took its corresponding dx,dy values, dx = 0.001... and <strong>dy = 0</strong>. Isn't it wrong to get dy = 0 on the horizontal side of the rectangle?</p>
<p>I checked the dy image, and it clearly shows the top and bottom sides of the rectangle. <strong>But still, why dy = 0 on that side?</strong></p>
<p>Below is my dx image:</p>
<p><img alt="image description" src="/upfiles/13419390596711553.png"></p>
<p>My dy image:</p>
<p><img alt="image description" src="/upfiles/13419390788350963.png"></p>
http://answers.opencv.org/question/234/weird-result-while-finding-angle/?comment=245#post-id-245You have a rectangle (I'm suppose it sides are parallel to coordinate axises), so one of angles must be 90 degrees (on vertical side) and the other is 0 (on horizontal side).Wed, 11 Jul 2012 05:57:17 -0500http://answers.opencv.org/question/234/weird-result-while-finding-angle/?comment=245#post-id-245Comment by Abid Rahman K for <p>Hi</p>
<p>I wanted to find the angle of a curve at every pixel. For that, I applied canny edge detection. For test image, i took a filled rectangle. So obviously, canny edge result is just a rectangle.</p>
<p>Then i applied Sobel derivative in x and y direction and to find the angle, i took <strong>arctan(dy/dx)</strong>. ( dx = dx+0.001 to avoid divide by zero)</p>
<p><strong>For vertical side, i got an angle of 0</strong> which is true since dy=0 in vertical direction and dx is some high value.</p>
<p><strong>But for horizontal side also, i got zero.</strong> I don't understand why. When i took its corresponding dx,dy values, dx = 0.001... and <strong>dy = 0</strong>. Isn't it wrong to get dy = 0 on the horizontal side of the rectangle?</p>
<p>I checked the dy image, and it clearly shows the top and bottom sides of the rectangle. <strong>But still, why dy = 0 on that side?</strong></p>
<p>Below is my dx image:</p>
<p><img alt="image description" src="/upfiles/13419390596711553.png"></p>
<p>My dy image:</p>
<p><img alt="image description" src="/upfiles/13419390788350963.png"></p>
http://answers.opencv.org/question/234/weird-result-while-finding-angle/?comment=251#post-id-251dx is result i got while applying sobel in horizontal direction. and dy in vertical direction.Wed, 11 Jul 2012 11:03:47 -0500http://answers.opencv.org/question/234/weird-result-while-finding-angle/?comment=251#post-id-251Answer by Ilya Lysenkov for <p>Hi</p>
<p>I wanted to find the angle of a curve at every pixel. For that, I applied canny edge detection. For test image, i took a filled rectangle. So obviously, canny edge result is just a rectangle.</p>
<p>Then i applied Sobel derivative in x and y direction and to find the angle, i took <strong>arctan(dy/dx)</strong>. ( dx = dx+0.001 to avoid divide by zero)</p>
<p><strong>For vertical side, i got an angle of 0</strong> which is true since dy=0 in vertical direction and dx is some high value.</p>
<p><strong>But for horizontal side also, i got zero.</strong> I don't understand why. When i took its corresponding dx,dy values, dx = 0.001... and <strong>dy = 0</strong>. Isn't it wrong to get dy = 0 on the horizontal side of the rectangle?</p>
<p>I checked the dy image, and it clearly shows the top and bottom sides of the rectangle. <strong>But still, why dy = 0 on that side?</strong></p>
<p>Below is my dx image:</p>
<p><img alt="image description" src="/upfiles/13419390596711553.png"></p>
<p>My dy image:</p>
<p><img alt="image description" src="/upfiles/13419390788350963.png"></p>
http://answers.opencv.org/question/234/weird-result-while-finding-angle/?answer=590#post-id-590Sobel uses the following kernel to compute y-derivative:
-1 -2 -1
0 0 0
1 2 1
So if you have a 1-pixel wide horizontal line from Canny edges then it will be taken with zero weight in Sobel. That is why dy = 0 on this line.
Try to run the Sobel operator on the original image instead of Canny edges and you should get correct results.Fri, 20 Jul 2012 13:34:26 -0500http://answers.opencv.org/question/234/weird-result-while-finding-angle/?answer=590#post-id-590Comment by Abid Rahman K for <p>Sobel uses the following kernel to compute y-derivative:</p>
<pre><code>-1 -2 -1
0 0 0
1 2 1
</code></pre>
<p>So if you have a 1-pixel wide horizontal line from Canny edges then it will be taken with zero weight in Sobel. That is why dy = 0 on this line.</p>
<p>Try to run the Sobel operator on the original image instead of Canny edges and you should get correct results.</p>
http://answers.opencv.org/question/234/weird-result-while-finding-angle/?comment=594#post-id-594yeah. I already found it and did as you mentioned and it worked. So I was about to answer my own question. But before that, you answered it . Anyway thanks!!!Sat, 21 Jul 2012 01:39:19 -0500http://answers.opencv.org/question/234/weird-result-while-finding-angle/?comment=594#post-id-594Answer by blue for <p>Hi</p>
<p>I wanted to find the angle of a curve at every pixel. For that, I applied canny edge detection. For test image, i took a filled rectangle. So obviously, canny edge result is just a rectangle.</p>
<p>Then i applied Sobel derivative in x and y direction and to find the angle, i took <strong>arctan(dy/dx)</strong>. ( dx = dx+0.001 to avoid divide by zero)</p>
<p><strong>For vertical side, i got an angle of 0</strong> which is true since dy=0 in vertical direction and dx is some high value.</p>
<p><strong>But for horizontal side also, i got zero.</strong> I don't understand why. When i took its corresponding dx,dy values, dx = 0.001... and <strong>dy = 0</strong>. Isn't it wrong to get dy = 0 on the horizontal side of the rectangle?</p>
<p>I checked the dy image, and it clearly shows the top and bottom sides of the rectangle. <strong>But still, why dy = 0 on that side?</strong></p>
<p>Below is my dx image:</p>
<p><img alt="image description" src="/upfiles/13419390596711553.png"></p>
<p>My dy image:</p>
<p><img alt="image description" src="/upfiles/13419390788350963.png"></p>
http://answers.opencv.org/question/234/weird-result-while-finding-angle/?answer=498#post-id-498I'm not sure I understand the question, but here is a an example which might help you. Usually when I have an nonsense numerical result as you describe, it is due to a data type error. For example, if you are somehow casting to an integer before dividing or passing to arctan the fractional part would be discarded resulting in a zero angle.
#!/usr/bin/env python
import cv2, cv
import numpy as np
# draw a rectangle
image = np.zeros((100,100), dtype='f4')
image[25:75,30:70] = 50
# take the x and y sobel derivative
dx = cv2.Sobel(image, -1, 1, 0)
dy = cv2.Sobel(image, -1, 0, 1)
# display the results with nice scaling
cv2.imshow("original", image)
cv2.imshow("dx", cv2.convertScaleAbs(dx, None, 1./2, 100))
cv2.imshow("dy", cv2.convertScaleAbs(dy, None, 1./2, 100))
# convert dx, dy to magnitude and angle
# angle is in radians
mag, theta = cv2.cartToPolar(dx, dy)
# display in HSV so angle has a simple mapping
theta_hsv = np.zeros((100,100,3), dtype='f4')
# Hue is angle in degrees
theta_hsv[...,0] = np.degrees(theta)
# S and V are 1.0
theta_hsv[...,1:] = 1.0
# Perform the colorspace conversion
# Note that you need to import the old
# cv modlue to get the conversion constants
theta_bgr = cv2.cvtColor(theta_hsv, cv.CV_HSV2BGR)
# and show the angles
cv2.imshow("theta", theta_bgr)
# press 'q' to exit
while cv2.waitKey(10) != ord('q'):
pass
Wed, 18 Jul 2012 10:42:00 -0500http://answers.opencv.org/question/234/weird-result-while-finding-angle/?answer=498#post-id-498