-
-
Notifications
You must be signed in to change notification settings - Fork 56.5k
Description
System Information
I posted #26954 issue weeks ago, I initially suspected that the issue with solvePnPRansac was caused by setting an overly large reprojection error threshold, which led me to close the original issue. However, I have since determined that the reprojection error is not the root cause. After testing the function on multiple planar object datasets (similar to a chessboard grid), I found that all OpenCV versions after 4.6.0—including the latest 4.11 release—consistently produce incorrect results. When the results from OpenCV 4.6.0 and later versions differ, the versions after 4.6.0 (including the latest 4.11) are always incorrect. This pattern holds across all tested datasets, and the consistent failure cannot be attributed to the inherent randomness of the RANSAC algorithm.
Here comes my last issue content with new test data, code and params:
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):
[[-1.00723755]
[ 0.02060799]
[ 0.00964326]]
Translation vector (tvec):
[[-285.86571735]
[ -27.08810585]
[1687.58535944]]
OpenCV 4.7.0.72 or later:
Rotation vector (rvec):
[[ 1.00946528]
[-0.0967281 ]
[ 0.05093826]]
Translation vector (tvec):
[[-275.75708952]
[ -25.8612022 ]
[1450.52741509]]
Steps to reproduce
- Use OpenCV 4.6.0.66 and run the code.
- Use OpenCV 4.7.0.72 or later and run the same code.
- Compare the outputs for rvec and tvec.
import cv2
import numpy as np
def solve_pnp_ransac():
# 3D object points (objectPoints)
objectPoints = np.array(
[[20., 17.5, 0.],
[208., 17.5, 0.],
[208., 40.5, 0.],
[20., 40.5, 0.],
[440., 97.5, 0.],
[440., 120.5, 0.],
[20., 137.5, 0.],
[208., 137.5, 0.],
[208., 160.5, 0.],
[20., 160.5, 0.]]
, dtype=np.float32)
# 2D image points (imagePoints)
imagePoints = np.array(
[[545.44006, 387.48523],
[613.87695, 387.2843],
[613.37036, 391.72394],
[544.3327, 391.97958],
[700.9006, 403.2526],
[701.5016, 408.12985],
[538.9222, 411.964],
[611.1791, 411.69757],
[610.6426, 416.74878],
[537.5011, 416.98953]]
, dtype=np.float32)
# Camera intrinsic matrix (cameraMatrix)
cameraMatrix = np.array([
[609.472778, 0, 642.603699],
[0, 609.341736, 393.885529],
[0, 0, 1]
], dtype=np.float32)
# Distortion coefficients (distCoeffs)
distCoeffs = np.array([
-1.117186,
0.620454,
0.000008,
0.000002,
-0.116633,
-1.098497,
0.594703,
-0.106835
], dtype=np.float64)
# 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, iterationsCount=1000,
reprojectionError=3.0, confidence=0.99, 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)
# print("Inliers (inliers):\n", inliers)
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)