OpenCV Python cv2.perspectiveTransform

This implementation really needs to be changed in a future version.

From the OpenCV docs for perspectiveTransform():

src – input two-channel (…) floating-point array

Slant emphasis added by me.

>>> A = np.array([[0, 0]], dtype=np.float32)
>>> A.shape
(1, 2)

So we see from here that A is just a single-channel matrix, that is, two-dimensional. One row, two cols. You instead need a two-channel image, i.e., a three-dimensional matrix where the length of the third dimension is 2 or 3 depending on if you’re sending in 2D or 3D points.

Long story short, you need to add one more set of brackets to make the set of points you’re sending in three-dimensional, where the x values are in the first channel, and the y values are in the second channel.

>>> A = np.array([[[0, 0]]], dtype=np.float32)
>>> A.shape
(1, 1, 2)

It’s not intuitive, and though it’s documented, it’s not very explicit on that point. That’s all you need. I’ve answered an identical question before, but for the cv2.transform() function.

Leave a Comment