I am trying to rectified two stereo images using cv2.stereoRectify after finding a camera matrix of two cameras (K1,D1,K2,D2,R,T). After that, I got R1, R2, P1, P2, Q, roi1, roi2 and use these parameters in cv2.initUndistortRectifyMap to get left rectified-image and right-rectified image. But after using this method the rectified images are not good. I using alpha = -1. Here is my code:
R1, R2, P1, P2, Q, roi1, roi2 = cv2.stereoRectify(cal_data.camera_model.get('M1'), cal_data.camera_model.get('dist1'), cal_data.camera_model.get('M2'), cal_data.camera_model.get('dist2'),
(960, 544), cal_data.camera_model.get('R'), cal_data.camera_model.get('T'), alpha=-1)
print(R1, R2, P1, P2)
leftFrame = cv2.imread('/home/nikhil_m/Pictures/Webcam/2019-07-29-171837.jpg')
rightFrame = cv2.imread('/home/nikhil_m/Pictures/Webcam/2019-07-29-171809.jpg')
leftFrame = cv2.resize(leftFrame,(960,544))
rightFrame = cv2.resize(rightFrame, (960, 544))
leftMapX, leftMapY = cv2.initUndistortRectifyMap(cal_data.camera_model.get('M1'), cal_data.camera_model.get('dist1'), R1, P1, (960,544), cv2.CV_32FC1)
left_rectified = cv2.remap(leftFrame, leftMapX, leftMapY, cv2.INTER_LINEAR, cv2.BORDER_CONSTANT)
rightMapX, rightMapY = cv2.initUndistortRectifyMap(cal_data.camera_model.get('M2'), cal_data.camera_model.get('dist2'), R2, P2, (960,544), cv2.CV_32FC1)
right_rectified = cv2.remap(rightFrame, rightMapX, rightMapY, cv2.INTER_LINEAR, cv2.BORDER_CONSTANT)
Is there any better way to rectified images or I am doing something wrong.Please help
Camera matrix:
Intrinsic_mtx_1 [[1.22248627e+03 0.00000000e+00 5.24929333e+02]
[0.00000000e+00 1.32603348e+03 4.99669610e+01]
[0.00000000e+00 0.00000000e+00 1.00000000e+00]]
dist_1 [[ 0.09850468 1.08533383 -0.10682535 0.01777223 -3.39061053]]
Intrinsic_mtx_2 [[1.07148978e+03 0.00000000e+00 4.21476300e+02]
[0.00000000e+00 1.09912897e+03 2.61293969e+02]
[0.00000000e+00 0.00000000e+00 1.00000000e+00]]
dist_2 [[-0.15751877 -0.12428592 -0.01325468 0.02449842 3.72130512]]
R [[ 0.89624385 -0.12740274 -0.42487116]
[ 0.14523621 0.98934946 0.00969995]
[ 0.41911026 -0.0704002 0.90520186]]
T [[16.81657383]
[-5.69906211]
[ 2.42601652]]
E [[ -2.74088083 -1.99896304 -5.18233385]
[ -4.87369619 0.87480896 -16.25313836]
[ 7.55012482 15.91139212 -2.25824725]]
F [[ 1.77370674e-06 1.19257563e-06 3.10912454e-03]
[ 3.07460616e-06 -5.08784373e-07 1.09461230e-02]
[-6.78615804e-03 -1.05410214e-02 1.00000000e+00]]