[Python] The function cv2.solvePnP keep crashing

Discussão em 'Python' iniciado por Stack, Julho 11, 2025.

  1. Stack

    Stack Membro Participativo

    I have a function which is supposed to return the rotation vector and translation vector (rvec and tvec) given some 3d points, some 2d points, and an intrinsics matrix

    def solvePnP(points_3d: list[list[float]], points_2d: list[list[int]], camera_mat: np.ndarray, dist_coeffs: np.ndarray):

    points_3d = np.array(points_3d)
    points_2d = np.array(points_2d)

    success, rvec, tvec = cv2.solvePnP(points_3d, points_2d, camera_mat, dist_coeffs)
    if success:
    return rvec, tvec


    However, it seems to crash when I get to cv2.solvePnP(), and I have no idea why. I'm using opencv version 4.11.0. I get this exception when it crashes:

    Traceback (most recent call last):
    File "/Users/gabi/Downloads/BusProjector/main.py", line 60, in <module>
    rvec, tvec = Calibrator.solvePnP(img1_3d_points, img_points2_set, calibrator._camera_matrix, calibrator._dist_coeffs)
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    File "/Users/gabi/Downloads/BusProjector/util/calibrator.py", line 149, in solvePnP
    success, rvec, tvec = cv2.solvePnP(points_3d, points_2d, camera_mat, dist_coeffs)
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    cv2.error: OpenCV(4.11.0) /Users/xperience/GHA-Actions-OpenCV/_work/opencv-python/opencv-python/opencv/modules/calib3d/src/solvepnp.cpp:824: error: (-215:Assertion failed) ( (npoints >= 4) || (npoints == 3 && flags == SOLVEPNP_ITERATIVE && useExtrinsicGuess) || (npoints >= 3 && flags == SOLVEPNP_SQPNP) ) && npoints == std::max(ipoints.checkVector(2, CV_32F), ipoints.checkVector(2, CV_64F)) in function 'solvePnPGeneric'

    Continue reading...

Compartilhe esta Página