Yorian
Yorian

Reputation: 2062

OpenCV perspective transform in python

I'm trying to rectify an image in python. I have a Homography H (from a rotation matrix that rotates around the x, y and z axis) that looks like this for example: [[ 9.95671447e-01 7.83610423e-02 7.47993630e+02] [ -7.69292630e-02 9.96586377e-01 -4.48354859e+02] [ -3.48494755e-06 1.73615469e-06 9.98300856e-01]]

I thought I could do this woth cv2.perspectiveTransform() but I cant't get it to work. This is the code I use:

   # warp image
   img_cv2 = cv2.imread('surf.jpg', cv2.CV_LOAD_IMAGE_GRAYSCALE)
   # strange output but it does something:
   dst = cv2.perspectiveTransform(img_cv2,H)

But I get the following error:

    Traceback (most recent call last):
    File "C:\directory structure\python_files\Rectification\rectify.py", line 82, in <module>
    dst = cv2.perspectiveTransform(img_cv2,H)
    error: C:\slave\WinInstallerMegaPack\src\opencv\modules\core\src\matmul.cpp:1916: error: (-215) scn + 1 == m.cols && (depth == CV_32F || depth == CV_64F)`</pre>

Can anybody see what is going wrong?

Upvotes: 3

Views: 10383

Answers (3)

Abid Rahman K
Abid Rahman K

Reputation: 52646

It says the image should be np.float32 or np.float64.

So convert the image first by img_cv2 = np.float32(img_cv2).

Then apply the cv2.perspectiveTransform() and cv2.warpPerspective()

Check this tutorial for demo

Upvotes: 0

BConic
BConic

Reputation: 8990

I think what you want is cv2.warpPerspective (see documentation (link)), not cv2.perspectiveTransform.

Upvotes: 2

Anoop K. Prabhu
Anoop K. Prabhu

Reputation: 5655

The source and destination image must be floating point data.

cv2.perspectiveTransform(src, m[, dst]) → dst

Parameters:

  • src – input two-channel or three-channel floating-point array; each element is a 2D/3D vector to be transformed.
  • dst – output array of the same size and type as src.
  • m – 3x3 or 4x4 floating-point transformation matrix.

Refer:http://docs.opencv.org/modules/core/doc/operations_on_arrays.html?highlight=perspectivetransform#cv2.perspectiveTransform

So convert the 8U image to the appropriate datatype.

Upvotes: 2

Related Questions