solvePnP throws error: (-215:Assertion failed) src.size == dst.size && src.channels() == dst.channels() in function 'cvConvertScale'

Question:

I have 4 world coordinate points of a box and their image points . I’m trying to calculate the pose of the camera but I’m getting an error

File c:Usersnmorsi200AppDataLocalTempProg6_4.py", line 292, in <module>
    cv.solvePnP(objectPoints,imagePoints,cameraMatrix,distCoeffs)
cv2.error: OpenCV(4.5.5) D:aopencv-pythonopencv-pythonopencvmodulescoresrcconvert_c.cpp:113: error: (-215:Assertion failed) src.size == dst.size && src.channels() == dst.channels() in function 'cvConvertScale

Here is my code:

imagePoints = np.float32([[[ 544 ,337 ],
        [ 886 ,337   ],
        [886 ,781],
        [ 544 ,781 ]]])


objectPoints = np.float32([[[ 430 ,-210,90 ],
        [ 430 ,-110 ,90  ],
        [580 ,-210,90],
        [ 886 ,-110,90 ]]])

cameraMatrix = np.float32([ 1.5968554012182622e+04, 0., 5.9163145834154648e+02, 0.,
       1.0919346214593939e+04, 8.3519145165493478e+02, 0., 0., 1. ])

distCoeffs = np.float32([ 3.1817705820413217e+01, -2.4334106040843017e+03,
       -5.7690325903983741e-01, -8.4352650966664180e-02,
       -1.9337299660588971e+04 ])

cv.solvePnP(objectPoints,imagePoints,cameraMatrix,distCoeffs) 
Asked By: Nour Morsi

||

Answers:

Your camera matrix is a 1-D array when it should be a 2-D array of shape (3,3). OpenCV requires this shape. It does not accept a flat array.

cameraMatrix = np.float32([ ... ]).reshape((3,3))
Answered By: Christoph Rackwitz