Skip to content

solvePnPRansac produces incorrect results in OpenCV 4.7.0.72 and later versions #26954

@fwzdev1

Description

@fwzdev1

System Information

I encountered an issue with OpenCV versions 4.7.0.72 and later, where the results of cv2.solvePnPRansac() are incorrect. This issue does not occur in OpenCV 4.6.0.66.

Issue:

The object is a plane, similar to a chessboard pattern.

When using cv2.solvePnPRansac() with the following data:

3D object points (objectPoints)
2D image points (imagePoints)
Camera matrix (cameraMatrix)
Distortion coefficients (distCoeffs)

The output rvec (rotation vector) and tvec (translation vector) from OpenCV 4.7.0.72 and later versions are incorrect. However, the results from OpenCV 4.6.0.66 are correct and consistent with the expected output.

Problem exist both c++ and python version.

Detailed description

The results from OpenCV 4.6.0.66 are correct. However, starting from OpenCV 4.7.0.72, the results for rvec and tvec are incorrect and inconsistent with the previous version.

OpenCV 4.6.0.66:
Rotation vector (rvec):
[[-0.82648057]
[-0.02017779]
[-0.05026374]]
Translation vector (tvec):
[[-6.50527281e-01]
[ 1.25261525e+02]
[ 7.13647929e+02]]

OpenCV 4.7.0.72 or later:
Rotation vector (rvec):
[[ 0.52837279]
[-0.04799924]
[-0.04445835]]
Translation vector (tvec):
[[ -3.93779101]
[137.47153793]
[719.65360295]]

Steps to reproduce

  1. Use OpenCV 4.6.0.66 and run the code.
  2. Use OpenCV 4.7.0.72 or later and run the same code.
  3. Compare the outputs for rvec and tvec.
import cv2
import numpy as np

def solve_pnp_ransac():
    # 3D object points (objectPoints)
    objectPoints = np.array([
        [0.5, 61.799999, 0],
        [188.3, 61.799999, 0],
        [94.400002, 61.799999, 0],
        [-93.400002, 61.799999, 0],
        [94.400002, -60.700001, 0],
        [-93.400002, -60.700001, 0],
        [-187.3, 61.799999, 0],
        [188.3, -60.700001, 0],
        [-280, 61.799999, 0],
        [0.5, -60.700001, 0],
        [-93.400002, -182, 0],
        [94.400002, -182, 0],
        [281, -60.700001, 0],
        [281, 61.799999, 0]
    ], dtype=np.float32)

    # 2D image points (imagePoints)
    imagePoints = np.array([
        [648.5, 550.25],
        [819, 543],
        [734, 546.5],
        [562, 554.75],
        [718.25, 463],
        [567.5, 469.25],
        [475, 559],
        [793.25, 460.25],
        [386.875, 562.75],
        [643.5, 465.75],
        [569.75, 400.75],
        [707.25, 396],
        [867.25, 456.5],
        [901.5, 538]
    ], dtype=np.float32)

    # Camera intrinsic matrix (cameraMatrix)
    cameraMatrix = np.array([
        [613.63098, 0, 645.79266],
        [0, 613.43958, 397.38477],
        [0, 0, 1]
    ], dtype=np.float32)

    # Distortion coefficients (distCoeffs)
    distCoeffs = np.array([0, 0, 0, 0, 0, 0, 0, 0], dtype=np.float32)

    # Initialize rotation vector (rvec) and translation vector (tvec)
    rvec = np.zeros((3, 1), dtype=np.float32)
    tvec = np.zeros((3, 1), dtype=np.float32)

    # Inliers (inliers)
    inliers = None

    # Call solvePnPRansac
    success, rvec, tvec, inliers = cv2.solvePnPRansac(
        objectPoints, imagePoints, cameraMatrix, distCoeffs,
        rvec, tvec, useExtrinsicGuess=False, iterationsCount=100,
        reprojectionError=100.0, confidence=0.9, inliers=inliers,
        flags=cv2.SOLVEPNP_ITERATIVE)

    # Output results
    if success:
        print("SolvePnPRansac success!")
        print("Rotation vector (rvec):\n", rvec)
        print("Translation vector (tvec):\n", tvec)
    else:
        print("SolvePnPRansac failed!")

if __name__ == "__main__":
    solve_pnp_ransac()

Issue submission checklist

  • I report the issue, it's not a question
  • I checked the problem with documentation, FAQ, open issues, forum.opencv.org, Stack Overflow, etc and have not found any solution
  • I updated to the latest OpenCV version and the issue is still there
  • There is reproducer code and related data files (videos, images, onnx, etc)

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions