我正在尝试使用cv2.solvePnP(),但出现错误

时间:2020-08-27 19:39:10

标签: python opencv

这是错误:

cv2.solvePnP(obj_points, image_points, mtx, dist)
cv2.error: OpenCV(4.2.0) C:\projects\opencv-python\opencv\modules\calib3d\src\solvepnp.cpp:754: error:
(-215:Assertion failed) ( (npoints >= 4) || (npoints == 3 && flags == SOLVEPNP_ITERATIVE && 
useExtrinsicGuess) ) && npoints == std::max(ipoints.checkVector(2, CV_32F), ipoints.checkVector(2, 
CV_64F)) in function 'cv::solvePnPGeneric'

这是我的代码:

mtx = np.load("./camera_params/mtx.npy")
dist = np.load("./camera_params/dist.npy")
obj_points = np.array([[0, 0, 0], [297, 0, 0], [297, 210, 0], [0, 210, 0]])
image_points = np.array([[416, 268], [422, 535], [826, 543], [829, 264]])
cv2.solvePnP(obj_points, image_points, mtx, dist)

我不知道如何解决。我试图处理这些争论,但没有帮助。如果您知道解决此错误的方法,将非常有帮助。

1 个答案:

答案 0 :(得分:2)

我最近有此错误,我通过使参数ndarray的浮点数而不是整型数来解决。您可以通过2种方式做到这一点:

obj_points = np.array([[0.0, 0.0, 0.0], [297.0, 0.0, 0.0], [297.0, 210.0, 0.0], [0.0, 210.0, 0.0]])
image_points = np.array([[416.0, 268.0], [422.0, 535.0], [826.0, 543.0], [829.0, 264.0]])
obj_points = np.array([[0, 0, 0], [297, 0, 0], [297, 210, 0], [0, 210, 0]])
obj_points = obj_points.astype('float32')
image_points = np.array([[416, 268], [422, 535], [826, 543], [829, 264]])
image_points = image_points.astype('float32')