4

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?

vwvw
  • 372
  • 4
  • 16
djkpA
  • 1,224
  • 2
  • 27
  • 57
  • 2
    `SolvePnP` is a [_non-deterministic_](https://faisalsikder.wordpress.com/2010/02/15/difference-between-algorithm-and-heuristic/) algorithm meaning that it is subject to chance, hence the different results with the same inputs. – Elouarn Laine May 17 '17 at 14:28
  • But that drastic change I used ITERATIVE flag – djkpA May 17 '17 at 14:32
  • is there a link that tells about non-determinism within solvePnp? I know there is a solvePnpRansac version, that is definitively non-deterministic. – Micka Mar 08 '18 at 13:06
  • 1
    Please provide all input data (camera matrix and distortion coefficients are missing in the example). – Nejc Mar 09 '18 at 16:44

1 Answers1

1

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.

vwvw
  • 372
  • 4
  • 16