Baptiste Valadié
Baptiste Valadié

Reputation: 53

Error using solvePnPRansac function

I'm using Python 2.7 and opencv 3.0.0. I'm trying to do a pose estimation on a live video. So i used the calibrate.py gave by opencv. it works good. In this program, I added at the end lines to treat the informations in order to pose axis. I used this : http://docs.opencv.org/master/d7/d53/tutorial_py_pose.html#gsc.tab=0

On the line with solvePnPRansac function I wrote this instead : _, rvecs, tvecs, inliers = cv2.solvePnPRansac(obj_points[0], corners2, camera_matrix, dist_coefs) adding the _,at the beginning of the line.

I have this error appearing !

error: C:\builds\master_PackSlaveAddon-win64-vc12-static\opencv\modules\core\src\matrix.cpp:2294: error: (-215) d == 2 && (sizes[0] == 1 || sizes[1] == 1 || sizes[0]*sizes[1] == 0) in function cv::_OutputArray::create

I don't understand it at all !

Can someone help me ?

Here is my code to treat the video :

cap = cv2.VideoCapture(0)

while(1):


    # Take each frame
    ret, frame = cap.read()
    gray = cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)

    # Find the chess board corners
    ret, corners = cv2.findChessboardCorners(gray, (6,5),None)

    if ret:

        term = ( cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_COUNT, 30, 0.1 )

        corners2 = cv2.cornerSubPix(gray,corners,(11,11),(-1,-1),term)


        _, rvecs, tvecs, inliers = cv2.solvePnPRansac(obj_points[0], corners2, camera_matrix, dist_coefs)

        imgpts, jac = cv2.projectPoints(axis, rvecs, tvecs, camera_matrix, dist_coefs)


        frame = draw(frame,corners2,imgpts)

    cv2.imshow('img',frame)

    k = cv2.waitKey(5) & 0xFF
    if k == 27:
        break
cap.release()
cv2.destroyAllWindows()

Upvotes: 5

Views: 2475

Answers (4)

goe
goe

Reputation: 2303

There is a difference in the 3D model point definition between solvePnP and solvePnPRansac. Is not clear in the documentation, but solvePnP needs the model defined with a matrix with dimensions 3xN/Nx3 and solvePnPRansac needs the model with a matrix with dimensions 3x1xN/Nx1x3.

You can use this code to include the extra dimension to your model:

modelNx1x3 = np.zeros((N, 1, 3), np.float32)
modelNx1x3[:, 0, :] = modelNx3[:, :]

You can also find further details in the issue tracker in github

Upvotes: 0

ilan grinberg
ilan grinberg

Reputation: 56

I had the same problem. I used solvePnP instead of solvePnPRansac and it worked fine. I guess solvePnPRansac in python has a bug.

Upvotes: 3

Thesane
Thesane

Reputation: 1398

You need to define obj_points the same manner in the example. I don't see obj_points definition in the provided code snippet and I think that's the issue

obj_points= np.zeros((6*7,3), np.float32)
obj_points[:,:2] = np.mgrid[0:7,0:6].T.reshape(-1,2)

Upvotes: 0

PhilLab
PhilLab

Reputation: 5007

solvePnpRansac has only three output values:

OutputArray     rvec,
OutputArray     tvec, 
OutputArray     inliers = noArray(), 

so removing the _, in the beginning should make the program work again.

Upvotes: 0

Related Questions