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

lia*_*non 5 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'
Run Code Online (Sandbox Code Playgroud)

这是我的代码:

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)
Run Code Online (Sandbox Code Playgroud)

我不知道如何解决它。我试图玩弄争论,但没有帮助。如果您知道解决此错误的方法,那将会非常有帮助。

spa*_*tti 5

我最近遇到了这个错误,我通过将参数ndarrays 设置为浮点数而不是整数来解决它。您可以通过两种方式做到这一点:

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]])
Run Code Online (Sandbox Code Playgroud)
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')
Run Code Online (Sandbox Code Playgroud)