5

The phaseCorrelate seems to be an undocumented function of the OpenCV Python wrapper. The doc of the C++ function is here.

When I call the function from Python I get the following error:

cv2.error: ..\..\..\src\opencv\modules\imgproc\src\phasecorr.cpp:495: error: (-215)    src1.type() == CV_32FC1 || src1.type() == CV_64FC1

Every OpenCV 2 function uses numpy arrays, I suspect that this function has been left from the older wrapper. Maybe I need to convert the numpy arrays to CvMats before calling the function? How do I do that?

b_m
  • 1,473
  • 2
  • 18
  • 29

2 Answers2

7

You don't need to convert it into cvMat.

The error says your input should be of float32 or float64 with single channel.

So convert the images accordingly.

And both the images should be of same size. Then apply phaseCorrelate function, as follows:

>>> src1 = cv2.imread('sudoku.jpg',0)   # load first image in grayscale
>>> src2 = cv2.imread('su1.png',0)      # load second image in grayscale
>>> src1 = np.float32(src1)             # convert first into float32
>>> src2 = np.float32(src2)             # convert second into float32  
>>> ret = cv2.phaseCorrelate(src1,src2) # now calculate the phase correlation
>>> ret
(-0.024777238426224812, 0.0011736626157130559)

(Both my images are same, except change in brightness.)

Regarding documentation, you can post a bug report at code.opencv.org.

Abid Rahman K
  • 51,886
  • 31
  • 146
  • 157
  • I almost forgot to mention that I love your blog, extremely helpful. – b_m Nov 29 '12 at 19:37
  • 2
    Can I calculate the angle of difference of the two pictures using the phasecorrelate() results? – K K Nov 15 '18 at 09:30
1

Accordingly to the docs you can convert a numpy array into a CvMat as follows:

>>> import cv, numpy
>>> a = numpy.ones((480, 640))
>>> mat = cv.fromarray(a)
>>> print mat.rows
480
>>> print mat.cols
640
Vicent
  • 5,322
  • 2
  • 28
  • 36