Dhoni
Dhoni

Reputation: 1234

OpenCV : SolvePnP is giving different results for same input parameters

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?

Upvotes: 4

Views: 3692

Answers (1)

vwvw
vwvw

Reputation: 413

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.

Upvotes: 1

Related Questions