spmallick / learnopencv

Learn OpenCV : C++ and Python Examples
https://www.learnopencv.com/
20.69k stars 11.52k forks source link

How to get the stereo re-projection error from the individually calibrated left and right cameras using `cv2.projectPoints` in stereo-camera code #764

Open aaronlsmiles opened 1 year ago

aaronlsmiles commented 1 year ago

I am trying to obtain the reprojection errors (RPE) during stereo-camera calibrate.

For the left I used the following code after calculating left camera parameters:

mean_error = 0
for i in range(len(obj_pts)):
    imgpoints2, _ = cv2.projectPoints(obj_pts[i], rvecsL[i], tvecsL[i], mtxL, distL)
    error = cv2.norm(img_ptsL[i], imgpoints2, cv2.NORM_L2)/len(imgpoints2)
    mean_error += error
print( "total error (left): {}".format(mean_error/len(obj_pts)) )

And the following code after calculating the right parameters:

mean_error = 0
for i in range(len(obj_pts)):
    imgpoints3, _ = cv2.projectPoints(obj_pts[i], rvecsR[i], tvecsR[i], mtxR, distR)
    error = cv2.norm(img_ptsR[i], imgpoints3, cv2.NORM_L2)/len(imgpoints3)
    mean_error += error
print( "total error (right): {}".format(mean_error/len(obj_pts)) )

However, when I attempt the following code to obtain the stereo RPE:

mean_error = 0
for i in range(len(obj_pts)):
    imgpoints4, _ = cv2.projectPoints(obj_pts[i], rvecsL[i], rvecsR[i], tvecsL[i], tvecsR[i], new_mtxL, new_mtxR, distL, distR)
    error = cv2.norm(img_ptsL[i], img_ptsR[i], imgpoints4, cv2.NORM_L2)/len(imgpoints4)
    mean_error += error
print( "total error (stereo): {}".format(mean_error/len(obj_pts)) )

I get this error: projectPoints() takes at most 8 arguments (9 given)

What can I do here to get the combined rvecs, tvecs, mtx, and dist, so that I can give projectPoints 5 arguments and obtain the stereo RPE?

Cheers!

brmarkus commented 1 year ago

The (C++)API "projectPoints()" supports max. 8 parameters, but your code tries to use 9 parameters...

See the documentation for different publicly available versions of OpenCV:

https://docs.opencv.org/3.4/d9/d0c/group__calib3d.html#ga1019495a2c8d1743ed5cc23fa0daff8c https://docs.opencv.org/4.5.0/d9/d0c/group__calib3d.html#ga1019495a2c8d1743ed5cc23fa0daff8c https://docs.opencv.org/4.6.0/d9/d0c/group__calib3d.html#ga1019495a2c8d1743ed5cc23fa0daff8c

Are you sure you are using the API in the correct way?

aaronlsmiles commented 1 year ago

Apologies if my question wasn't clear. I understand that the code tries 9 arguments, what I'm asking is how to combine those parameters that are left and right, so that I will be able to give projectPoints 5 arguments.