Search code examples
pythonopencvpose-estimation

I am trying to use cv2.projectPoints() but I am getting an error


This is the error:

imgpts, jac = cv2.projectPoints(axis, rvecs, tvecs, mtx, dist)
cv2.error: OpenCV(4.2.0) C:\projects\opencv-python\opencv\modules\calib3d\src\calibration.cpp:603: 
error: (-5:Bad argument) Rotation must be represented by 1x3 or 3x1 floating-point rotation vector, or 
3x3 rotation matrix in function 'cvProjectPoints2Internal'

This is my code:

axis = np.float32([[3, 0, 0], [0, 10, 0], [0, 0, -50]]).reshape(-1, 3)#axis of coordinates

# PnP calculates the rotation vector and translation vector
rvecs, tvecs, inliers = cv2.solvePnP(obj_points, image_points, mtx, dist)

print(f"mtx shape: {mtx.shape}, tvecs shape: {tvecs.shape}")
print(f"mtx:\n {mtx}")
print(f"tvecs:\n {tvecs}")

# Calculate the coordinates of 3D points projected on 2D image plane
imgpts, jac = cv2.projectPoints(axis, rvecs, tvecs, mtx, dist)

And this is the outpot:

mtx shape: (3, 3), tvecs shape: (3, 1)

mtx:
 [[1.71223579e+03 0.00000000e+00 1.02990683e+03]
 [ 0.00000000e+00 1.70818827e+03 7.83446773e+02]
 [ 0.00000000e+00 0.00000000e+00 1.00000000e+00]]

tvecs:
 [[-0.09038089]
 [ -0.05386737]
 [ -0.01652085]]

I dont have any idea about how to solve it. It seem like the problem is in the shape of the arguments. But when i checked they seem in the right shape. so I dont know what is the problem, but if you do it be very helpful.


Solution

  • Maybe you are trying to get some code working from an old tutorial, or you misread function documentation about return values. Try assigning returning values from solvePnP like this:

    result, rvecs, tvecs = cv2.solvePnP(obj_points, image_points, mtx, dist)
    

    There should be no "inliers" in solvePnP function (at least as of opencv4).