Ask Your Question

bluejay's profile - activity

2015-05-11 17:29:04 -0600 received badge  Necromancer (source)
2015-02-27 01:58:48 -0600 answered a question What format does cv2.solvePnP use for points in Python?

I solved this in OpenCV 2.4.9.

cv2.solvePnP requires that your input object and image arrays are in contiguous memory (one of the things that the Mat::checkVector() function requires by default). Further, if you use the CV_P3P method, it relies on cv::undistortPoints, which requires the image points to be in a 2 channel type.

So solvePnP will error-fail with

World = array([[-0.5, -0.5,  3. ],
               [ 0.5, -0.5,  3. ],
               [ 0.5,  0.5,  3. ],
               [-0.5,  0. ,  3. ]])
keyPoints = array([[ 279.03286469,  139.80463604,    1.        ],
                     [ 465.40665724,  136.70519839,    1.        ],
                     [ 465.40665724,  325.1505936 ,    1.        ],
                     [ 279.03286469,  230.927896  ,    1.        ]])

objectPoints = World
imagePoints = keyPoints[:,:2] # <--- THIS SLICE IS A PROBLEM CAUSER!!!
cv2.solvePnP(objectPoints, imagePoints, np.eye(3), np.zeros(5))

but it will succeed, even in CV_P3P mode, with

imagePoints = np.ascontiguousarray(keyPoints[:,:2]).reshape((4,1,2)) # Now OpenCV is HAPPY!
retval, rvec, tvec = cv2.solvePnP(op, ip, intrinsic_mx, distortion_coefs, flags=cv2.CV_P3P)