error: (-215:Assertion failed) ( (npoints >= 4) ... in function ‘solvePnPGeneric‘

在使用solvePnP函数时可能会报以下错误

OpenCV(4.4.0) /tmp/pip-req-build-xgme2194/opencv/modules/calib3d/src/solvepnp.cpp:753: 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 'solvePnPGeneric'

可能引起此问题的原因有
cv2.solvePnP(in3Dpoint, in2Dpoint, cam_mat, dist_coeffs, rvec, tvec, flags=flag)

1、输入的参数维度不对应,可检查in3Dpoint, in2Dpoint,是否对应,相机参数维度是否正确
2、参数类型是否正确,要求CV_64F。在python中可以设置为dtype = “double”。如下:

        camera_matrix = np.array(
                                [[focal_length, 0, center[0]],
                                [0, focal_length, center[1]],
                                [0, 0, 1]], dtype = "double"
                                )

一般就可以解决此问题

你可能感兴趣的:(Python相关学习)