Ask Your Question

Revision history [back]

projectPoint is not working in openCV throwing error on bad arguments

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

projectPoint is not working in openCV throwing error on bad arguments

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!AtkGxS3Klodvi0f2vcfjsPJfTRjP