MIN_MATCH_COUNT = 10 good12 = [] for m, n in matches12: if m.distance < 0.7 * n.distance: good12.append(m) if len(good12) > MIN_MATCH_COUNT: src_pts = np.float32([kp1[m.queryIdx].pt for m in good12]).reshape(-1, 1, 2) dst_pts = np.float32([kp2[m.trainIdx].pt for m in good12]).reshape(-1, 1, 2) K = np.array([[1000, 0, img1.shape[1] / 2], [0, 1000, img1.shape[0] / 2], [0, 0, 1]]) _, rvec12, tvec12, inliers = cv2.solvePnPRansac(dst_pts, src_pts, K, np.zeros((4, 1))) else: print("Not enough matches are found - {}/{}".format(len(good12), MIN_MATCH_COUNT)) good23 = [] for m, n in matches23: if m.distance < 0.7 * n.distance: good23.append(m) if len(good23) > MIN_MATCH_COUNT: src_pts = np.float32([kp2[m.queryIdx].pt for m in good23]).reshape(-1, 1, 2) dst_pts = np.float32([kp3[m.trainIdx].pt for m in good23]).reshape(-1, 1, 2) K = np.array([[1000, 0, img2.shape[1] / 2], [0, 1000, img2.shape[0] / 2], [0, 0, 1]]) _, rvec23, tvec23, inliers = cv2.solvePnPRansac(dst_pts, src_pts, K, np.zeros((4, 1))) else: print("Not enough matches are found - {}/{}".format(len(good23), MIN_MATCH_COUNT)) 报错OpenCV(4.7.0) /io/opencv/modules/calib3d/src/solvepnp.cpp:242: error: (-215:Assertion failed) npoints >= 4 && npoints == std::max(ipoints.checkVector(2, CV_32F), ipoints.checkVector(2, CV_64F)) in function 'solvePnPRansac'
时间: 2024-04-26 14:23:19 浏览: 163
这个错误通常是由于输入的点数不足导致的。solvePnPRansac函数需要输入至少四个点,以估计相机的位姿。请检查你的输入点数是否符合要求。同时也可以检查点的数据类型是否正确,应该是CV_32F或CV_64F类型的二维向量。
在你的代码中,错误发生在solvePnPRansac函数中,可能是因为dst_pts或src_pts的维度不正确造成的。你可以检查一下这两个数组的shape是否正确。此外,你可以尝试将dst_pts和src_pts的维度改为(2, N),其中N为匹配点的数量,看看是否有改善。
如果还有问题,请提供更多的代码和具体错误信息,我会尽力帮你解决。
阅读全文