OpenCV : SolvePnP is giving different results for same input parameters

Viewed 2207

I am trying to estimate the 3D Pose of an object using solvePnP in python. But the problem is even if I kept both the camera and object static, the output of solvePnP (rvec and tvec) are changing. The world coordinates system is centered on the object and moves along with it. I am passing 4 image points and the corresponding 4 object points.

Calling SolvePnP:

retval, rvec, tvec = cv2.solvePnP(cam.object_points, cam.image_points, cam.camera_matrix, cam.dist_coefficients, None, None, False, cv2.SOLVEPNP_ITERATIVE)

Output 1:

Image points: 
[[ 236.  243.]
 [  43.  368.]
 [ 404.  372.]
 [ 235.  357.]]
Object points: 
[[ 0.   0.   0. ]
 [ 6.5  0.   0. ]
 [ 0.   0.   6.5]
 [ 0.   6.5  0. ]]
R VECT==========
[[-0.56619693]
 [-2.27732794]
 [ 0.71053527]]
T VECT==========
[[ 0.54725923]
 [-0.45834745]
 [ 0.58522831]]

Output 2:

Image points: 
[[ 236.  243.]
 [  43.  369.]
 [ 404.  372.]
 [ 235.  357.]]
Object points: 
[[ 0.   0.   0. ]
 [ 6.5  0.   0. ]
 [ 0.   0.   6.5]
 [ 0.   6.5  0. ]]
R VECT==========
[[ 0.33325838]
 [ 2.12767845]
 [ 0.98248134]]
T VECT==========
[[ -2.60687131]
 [  0.37989386]
 [ 23.85078678]]

The object points and image points are identical but solvePnP still gives several different results. The above results are alternating one after another for alternative frames.

How should I resolve it?

1 Answers

You could try to look at solvePnpGeneric which returns all the possible solutions. solvePnp might be hesitating between two solutions and giving you alternating results.

solvePnpGeneric was introduced in OpenCV 4.1.2 and allows access to the different solutions as well as their re-projection error.

Related