Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

phaseCorrelate function in OpenCV Python

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?

like image 991
b_m Avatar asked Nov 29 '12 16:11

b_m


2 Answers

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.

like image 117
Abid Rahman K Avatar answered Sep 30 '22 08:09

Abid Rahman K


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
like image 24
Vicent Avatar answered Sep 30 '22 07:09

Vicent