1

I have a problem with the cv2.phase() function. I coded the following:

img = cv2.imread("1.jpg", 0)
cv2.imshow("image", img)

img_dx = cv2.Sobel(img, cv2.CV_8U, 1, 0)
img_dy = cv2.Sobel(img, cv2.CV_8U, 0, 1)
angles = cv2.phase(img_dy, img_dx)

and it returns an assertion error when calling cv2.phase(). Both input images to the phase function are generated by a call of the cv2.sobel() function using the same input image. So the dtype of both input images is uint8 and they have the same size. So i dont understand why i get an assertion error.

The full error message i get is:

OpenCV Error: Assertion failed (src1.size() == src2.size() && type == src2.type() && (depth == CV_32F || depth == CV_64F)) in cv::phase, file ..\..\..\modules\core\src\mathfuncs.cpp, line 209
Jeru Luke
  • 20,118
  • 13
  • 80
  • 87
Lars_R
  • 11
  • 1
  • 2

2 Answers2

1

You must pass your images as a float variable for finding Sobel edges. So change your code to the following:

img_dx = cv2.Sobel(img, cv2.CV_32F, 1, 0)
img_dy = cv2.Sobel(img, cv2.CV_32F, 0, 1)

Now you should be able to find the phase...

Phase in radians:

OpenCV finds the phase in radians by default:

phase = cv2.phase(sobelx, sobely)

Phase in degrees:

To specify that you want the phase in degrees you must set the flag angleInDegrees = True as shown:

phase = cv2.phase(sobelx, sobely, angleInDegrees = True)
Jeru Luke
  • 20,118
  • 13
  • 80
  • 87
0

From the Documentation: http://docs.opencv.org/2.4/modules/core/doc/operations_on_arrays.html#phase

phase Calculates the rotation angle of 2D vectors.

C++: void phase(InputArray x, InputArray y, OutputArray angle, bool angleInDegrees=false)

Python: cv2.phase(x, y[, angle[, angleInDegrees]]) → angle

Parameters:

x – input **floating-point array** of x-coordinates of 2D vectors.

y – input array of y-coordinates of 2D vectors; it must have the same size and the same type as x.

angle – output array of vector angles; it has the same size and **same type** as x .

angleInDegrees – when true, the function calculates the angle in degrees, otherwise, they are measured in radians.