# Revision history [back]

I'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
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