Ask Your Question
3

cv2.perspectiveTransform() with Python

asked 2012-07-11 11:40:15 -0600

StevenBell gravatar image

I am attempting to use the perspectiveTransform() function in Python to warp a set of points:

import cv2
import numpy as np

a = np.array([[1, 2], [4, 5], [7, 8]], dtype='float32')
h = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]], dtype='float32')
pointsOut = cv2.perspectiveTransform(a, h)

However, all I get is an error:

cv2.error: /build/buildd/opencv-2.3.1/modules/core/src/matmul.cpp:1916:
error: (-215) scn + 1 == m.cols && (depth == CV_32F || depth == CV_64F)
in function perspectiveTransform

I assume I'm passing the arrays in the wrong format somehow, but I've tried quite a few different combinations of array shapes and data types with no success. What's the right way to do this?

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
5

answered 2012-07-11 11:58:00 -0600

StevenBell gravatar image

Found the solution in the find_obj.py sample. The catch is that the C++ code is expecting a "two-channel or three-channel floating-point array, where each element is a 2D/3D vector", which translates in Python/NumPy to a 3-dimensional array.

import cv2
import numpy as np

a = np.array([[1, 2], [4, 5], [7, 8]], dtype='float32')
h = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]], dtype='float32')
a = np.array([a])

pointsOut = cv2.perspectiveTransform(a, h)
edit flag offensive delete link more

Comments

Thank you!

slow gravatar imageslow ( 2015-07-09 11:28:43 -0600 )edit

Thanks , nice catch..

alexisk1 gravatar imagealexisk1 ( 2018-10-09 13:56:10 -0600 )edit

Question Tools

Stats

Asked: 2012-07-11 11:40:15 -0600

Seen: 44,217 times

Last updated: Jul 11 '12