Error using solvePnPRansac function

I am using Python 2.7 and opencv 3.0.0. I am trying to make a pose assessment on a live video. Therefore, I used calibrate.py provided by opencv. It works well. In this program, I added at the end of the line for processing information, to lead the axis. I used this: http://docs.opencv.org/master/d7/d53/tutorial_py_pose.html#gsc.tab=0

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

This error has appeared!

 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 this at all!

Can someone help me?

Here is my video processing code:

 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() 
+5
source share
4 answers

I had the same problem. I used solvePnP instead of solvePnPRansac and it worked fine. I think solvePnPRansac in python has an error.

+2
source

solvePnpRansac has only three output values:

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

therefore, removing _, at the beginning should make the program work again.

0
source

You need to define obj_points in the same way in the example. I do not see the obj_points definition in the provided code snippet, and I think the problem is

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

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

This code can be used to include an extra dimension in your model:

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

You can also find more details in the problem tracker on github

0
source

Source: https://habr.com/ru/post/1235898/


All Articles