SolvePNP_P3P doesn't work for 4 points. Please help.

asked 2019-09-27 13:08:18 -0500

updated 2019-09-27 20:37:40 -0500

supra56 gravatar image

Hello, I'm using the solvePnP() function to get the extrinsics for a camera. I'm calibrating wrt the ground. I have taken 4 points on the ground. I tried to use the solvePnP_P3P flag because I was using exactly 4 points but I get an error when I run the function.

System details: Windows 10. OpenCV 4.1

Code snippet:

import numpy as np
from numpy.linalg import inv
import cv2
import sys


# with open('./calib/calibration.yaml') as f:
#     loadeddict = yaml.load(f)
# camera_matrix = loadeddict.get('camera_matrix')
# dist_coeffs = loadeddict.get('dist_coeff')


camera_matrix = [[1029.61564, 0.00000000, 916.314412],
 [0.00000000 ,1026.37518 ,526.909999],
 [0.00000000, 0.00000000, 1.00000000]]

dist_coeffs = [[-0.3725, 0.21018, -0.001127, 0.00013219, -0.09244]] 

#tnsPoints = [(0.0, 1.0, 0.0), (3.0, 1.0, 0.0), (2.0, 6.0, 0.0), (0.0, 6.0, 0.0)]
tnsPoints = [(0.0, 0.0, 0.0), (1.0, 0.0, 0.0), (0.0, 1.0, 0.0), (0.0, 0.0, 1.0)]

imPoints = [(809, 915), (1181, 893), (1020, 362), (811, 372)]

retval, rvec, tvec = cv2.solvePnP(np.array(tnsPoints),np.array(imPoints),np.asarray(camera_matrix),np.asarray(dist_coeffs), flags=cv2.SOLVEPNP_P3P)

Error that I get:

Traceback (most recent call last):
  File "C:\Users\pavithra\Pictures\Camera Roll\project_3D_to_2D.py", line 2016, in <module>
    retval, rvec, tvec = cv2.solvePnP(np.array(tnsPoints),np.array(imPoints),np.asarray(camera_matrix),np.asarray(dist_coeffs), flags=cv2.SOLVEPNP_P3P)
cv2.error: OpenCV(4.1.0) C:\projects\opencv-python\opencv\modules\calib3d\src\solvepnp.cpp:92: error: (-215:Assertion failed) ( (npoints >= 4) || (npoints == 3 && flags == SOLVEPNP_ITERATIVE && useExtrinsicGuess) ) && npoints == std::max(ipoints.checkVector(2, CV_32F), ipoints.checkVector(2, CV_64F)) in function 'cv::solvePnP'

Please let me know if anybody has any idea of what is going on.

Thank you.

edit retag flag offensive close merge delete