Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Initialize numpy array (cv2 python) and PerspectiveTransform

I am trying to convert some of my C++ OpenCV code to Python, and have hit a stumbling block.

I am trying to get a numpy array into the PerspectiveTransform function and have hit the following assert in the underlying C++ code (matmul.cpp, ln 1926):

CV_Assert( scn + 1 == m.cols && (depth == CV_32F || depth == CV_64F));

Now this is telling me that, first, the number of columns in the transformation matrix should be one more than the number of channels in the input matrix, and, second, that the source matrix should be made of floating point elements, right?

Here's the code that is causing the exception:

rotMat = buildRotMat(roll, pitch, yaw)
corners = np.ones((1,1,2), np.float32)
newCorners = np.zeros((1,1,2), np.float32)
cv2.perspectiveTransform(corners, newCorners, rotMat)

and from a separate function:

rotMat = np.array(
[[math.cos(alpha)*math.cos(beta),
math.cos(alpha)*math.sin(beta)*math.sin(gamma)-math.sin(alpha)*math.cos(gamma),
math.cos(alpha)*math.sin(beta)*math.cos(gamma)+math.sin(alpha)*math.sin(gamma)],
[math.sin(alpha)*math.cos(beta),
math.sin(alpha)*math.sin(beta)*math.sin(gamma)+math.cos(alpha)*math.cos(gamma),
math.sin(alpha)*math.sin(beta)*math.cos(gamma)-math.cos(alpha)*math.sin(gamma)],
[-math.sin(beta),
math.cos(beta)*math.sin(gamma),
math.cos(beta)*math.cos(gamma)]], dtype=float32)

My guess would be that I've made a numpy error (when trying to initialize a 2-channel floating point array), since I'm learning Python as I go, and this code did work fine in C++. Any help or insight would be greatly appreciated.

Thanks, Matt