Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

What format does cv2.solvePnP use for points in Python?

I'm using OpenCV 2.3.1 under Ubuntu 12.04, and trying to use the cv2.solvePnP function. I cannot seem to figure out what format the two point arguments are supposed to be in. The document states:

objectPoints – Array of object points in the object coordinate space, 3xN/Nx3 1-channel or 1xN/Nx1 3-channel, where N is the number of points. vector<point3f> can be also passed here.

imagePoints – Array of corresponding image points, 2xN/Nx2 1-channel or 1xN/Nx1 2-channel, where N is the number of points. vector<point2f> can be also passed here.

I have so far tried the following inputs:

  • NumPy arrays with shape (N, 3) and (N, 2)
  • Numpy arrays with shape (N, 1, 3) and (N, 1, 2)
  • Python lists of NumPy arrays of (N,3) and (N,2)

The most typical error I get reads:

cv2.error: /build/buildd/opencv-2.3.1/modules/calib3d/src/solvepnp.cpp:52: error: (-215) npoints >= 0 && npoints == std::max(ipoints.checkVector(2, CV_32F), ipoints.checkVector(2, CV_64F)) in function solvePnP

What format are these two arguments supposed to be in? Is there something I'm missing?

What format does cv2.solvePnP use for points in Python?

I'm using OpenCV 2.3.1 under Ubuntu 12.04, and trying to use the cv2.solvePnP function. I cannot seem to figure out what format the two point arguments are supposed to be in. The document states:

objectPoints – Array of object points in the object coordinate space, 3xN/Nx3 1-channel or 1xN/Nx1 3-channel, where N is the number of points. vector<point3f> can be also passed here.

imagePoints – Array of corresponding image points, 2xN/Nx2 1-channel or 1xN/Nx1 2-channel, where N is the number of points. vector<point2f> can be also passed here.

I have so far tried the following inputs:

  • NumPy arrays with shape (N, 3) and (N, 2)
  • Numpy arrays with shape (N, 1, 3) and (N, 1, 2)
  • Python lists of NumPy arrays of (N,3) and (N,2)

The most typical error I get reads:

cv2.error: /build/buildd/opencv-2.3.1/modules/calib3d/src/solvepnp.cpp:52: error: (-215) npoints >= 0 && npoints == std::max(ipoints.checkVector(2, CV_32F), ipoints.checkVector(2, CV_64F)) in function solvePnP

What format are these two arguments supposed to be in? Is there something I'm missing?

EDIT: Based on blue's answer, I've refactored such that this code snippet:

print opts.shape, ipts.shape, temp_cam.shape, dc.shape > print opts.dtype, ipts.dtype, temp_cam.dtype, dc.dtype > rcv, tcv = cv2.solvePnP(opts, ipts, temp_cam, dc)

Produces:

(1420, 3, 1) (1420, 2, 1) (3, 3) (5, 1)

float64 float64 float64 float64

OpenCV Error: Assertion failed (src.dims <= 2 && esz <= (size_t)32) in transpose, file /build/buildd/opencv-2.3.1/modules/core/src/matrix.cpp, line 1680 terminate called after throwing an instance of 'cv::Exception' what(): /build/buildd/opencv-2.3.1/modules/core/src/matrix.cpp:1680: error: (-215) src.dims <= 2 && esz <= (size_t)32 in function transpose

So the sizes seem right, and the types seem right, but I'm still getting an error.

If I use the example code in the answer in ipython, I get a valid answer. Is there some other constraint I'm not recognizing?

What format does cv2.solvePnP use for points in Python?

I'm using OpenCV 2.3.1 under Ubuntu 12.04, and trying to use the cv2.solvePnP function. I cannot seem to figure out what format the two point arguments are supposed to be in. The document states:

objectPoints – Array of object points in the object coordinate space, 3xN/Nx3 1-channel or 1xN/Nx1 3-channel, where N is the number of points. vector<point3f> can be also passed here.

imagePoints – Array of corresponding image points, 2xN/Nx2 1-channel or 1xN/Nx1 2-channel, where N is the number of points. vector<point2f> can be also passed here.

I have so far tried the following inputs:

  • NumPy arrays with shape (N, 3) and (N, 2)
  • Numpy arrays with shape (N, 1, 3) and (N, 1, 2)
  • Python lists of NumPy arrays of (N,3) and (N,2)

The most typical error I get reads:

cv2.error: /build/buildd/opencv-2.3.1/modules/calib3d/src/solvepnp.cpp:52: error: (-215) npoints >= 0 && npoints == std::max(ipoints.checkVector(2, CV_32F), ipoints.checkVector(2, CV_64F)) in function solvePnP

What format are these two arguments supposed to be in? Is there something I'm missing?

EDIT: EDIT:

Based on blue's answer, I've refactored such that this code snippet:

print opts.shape, ipts.shape, temp_cam.shape, dc.shape > print opts.dtype, ipts.dtype, temp_cam.dtype, dc.dtype > rcv, tcv = cv2.solvePnP(opts, ipts, temp_cam, dc)

Produces:

(1420, 3, 1) (1420, 2, 1) (3, 3) (5, 1)

float64 float64 float64 float64

OpenCV Error: Assertion failed (src.dims <= 2 && esz <= (size_t)32) in transpose, file /build/buildd/opencv-2.3.1/modules/core/src/matrix.cpp, line 1680 terminate called after throwing an instance of 'cv::Exception' what(): /build/buildd/opencv-2.3.1/modules/core/src/matrix.cpp:1680: error: (-215) src.dims <= 2 && esz <= (size_t)32 in function transpose

So the sizes seem right, and the types seem right, but I'm still getting an error.

If I use the example code in the answer in ipython, I get a valid answer. Is there some other constraint I'm not recognizing?