projectPoint is not working in openCV throwing error on bad arguments

asked 2018-11-05 10:51:58 -0600

shivamp21 gravatar image

updated 2018-11-05 11:58:57 -0600

I am using OpenCV 3.1.0 with anaconda and I was trying to find the orientation of an object from a 2D image and my code ends up with an error notifying that

{{{OpenCV Error: Bad argument (Rotation must be represented by 1x3 or 3x1 floating-point rotation vector, or 3x3 rotation matrix) in cvProjectPoints2, file /home/travis/miniconda/conda-bld/conda_1486587069159/work/opencv-3.1.0/modules/calib3d/src/calibration.cpp, line 608}}

here is my code :

def draw(img, corners, imgpts):
    corner = tuple(corners[0].ravel())
    img = cv2.line(img, corner, tuple(imgpts[0].ravel()), (255,0,0), 5)
    img = cv2.line(img, corner, tuple(imgpts[1].ravel()), (0,255,0), 5)
    img = cv2.line(img, corner, tuple(imgpts[2].ravel()), (0,0,255), 5)
    return img

cap = cv2.VideoCapture(1)

with np.load('calibData.npz') as X:
    mtx, dist, rvecs, tvecs = [X[i] for i in 
                                       ('mtx','dist','rvecs','tvecs')]

criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 30, 0.001)
objp = np.zeros((7*7,3), np.float32)
objp[:,:2] = np.mgrid[0:7,0:7].T.reshape(-1,2)

axis = np.float32([[3,0,0], [0,3,0], [0,0,-3]]).reshape(-1,3)

while(1):
    ret1, frame = cap.read()
# print(frame)
    cv2.imshow('frame', frame)
    img = frame

    gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
    ret, corners = cv2.findChessboardCorners(gray, (7,7),None)


    if ret == True:
        i += 1
        corners2 = cv2.cornerSubPix(gray,corners,(11,11),(-1,-1),criteria)

        # Find the rotation and translation vectors.
        rvecs, tvecs, inliers = cv2.solvePnP(objp, corners2, mtx, dist)

        # project 3D points to image plane
        imgpts, jac = cv2.projectPoints(axis, rvecs, tvecs, mtx, dist)

        img = draw(img,corners2,imgpts)
        cv2.imshow('img',img)
        cv2.waitKey(500)
        k = cv2.waitKey(0) & 0xff
        if k == 's':
            cv2.imwrite('fname'+str(i)+'.png', img)

    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

cap.release()
cv2.destroyAllWindows()

and here is the error message

OpenCV Error: Bad argument (Rotation must be represented by 1x3 or 3x1 floating-point rotation vector, or 3x3 rotation matrix) in cvProjectPoints2, file /home/travis/miniconda/conda-bld/conda_1486587069159/work/opencv-3.1.0/modules/calib3d/src/calibration.cpp, line 608 Traceback (most recent call last): File "pose.py", line 45, in <module> imgpts, jac = cv2.projectPoints(axis, rvecs, tvecs, mtx, dist) cv2.error: /home/travis/miniconda/conda-bld/conda_1486587069159/work/opencv-3.1.0/modules/calib3d/src/calibration.cpp:608: error: (-5) Rotation must be represented by 1x3 or 3x1 floating-point rotation vector, or 3x3 rotation matrix in function cvProjectPoints2

In windows10 anaconda env I also encountered this. Here is the link:- https://1drv.ms/u/s!AtkGxS3Klodvi0f2v...

edit retag flag offensive close merge delete

Comments

cap = cv2.VideoCapture(1)

to

cap = cv2.VideoCapture(0)
supra56 gravatar imagesupra56 ( 2018-11-05 11:04:15 -0600 )edit

Remove it: cv2.waitKey(500)

supra56 gravatar imagesupra56 ( 2018-11-05 11:34:57 -0600 )edit

Done it ! and tried again the error is yet in its place. In windows10 anaconda env I also encountered this. Here is the link:- https://1drv.ms/u/s!AtkGxS3Klodvi0f2v...

shivamp21 gravatar imageshivamp21 ( 2018-11-05 11:56:11 -0600 )edit

Try this: rvec, tvec, inliers = cv2.solvePnPRansac(objpoints, corners, rgb_mtx, rgb_dist)

supra56 gravatar imagesupra56 ( 2018-11-05 12:30:23 -0600 )edit

I noticed bad arguement coming from this.

axis = np.float32([[3,0,0], [0,3,0], [0,0,-3]]).reshape(-1,3)

Change this:

objp = np.zeros((7*7,3), np.float32)

to

objp = np.zeros((7,3), np.float32)

Btw, you can use this: cv2.solvePnP to see if you're getting any problem.

supra56 gravatar imagesupra56 ( 2018-11-05 12:33:48 -0600 )edit

Damn! lunch time. Am I?

supra56 gravatar imagesupra56 ( 2018-11-05 12:48:23 -0600 )edit

Actually, I just formulated the bug its here : rvecs, tvecs, inliers = cv2.solvePnP(objp, corners2, mtx, dist) replace rvecs with ret I was following the docs. http://opencv-python-tutroals.readthe... and there was this written as it is.

but there is now a problem with imshow('img', img) in while loop it's not showing the image.

shivamp21 gravatar imageshivamp21 ( 2018-11-05 14:45:37 -0600 )edit

Move this outside of If block condition.

 cv2.imshow('img',img)
 cv2.waitKey(500)
 k = cv2.waitKey(0) & 0xff
 if k == 's':
       cv2.imwrite('fname'+str(i)+'.png', img)
supra56 gravatar imagesupra56 ( 2018-11-05 19:47:56 -0600 )edit