使用opencv的立体声图像校正无法正常工作

时间:2019-07-30 05:05:09

标签: opencv computer-vision

我试图在找到两个摄像机(K1,D1,K2,D2,R,T)的摄像机矩阵后,使用cv2.stereoRectify校正两个立体图像。之后,我得到了R1,R2,P1,P2,Q,roi1,roi2,并在cv2.initUndistortRectifyMap中使用这些参数来获取左整流图像和右整流图像。 但是使用此方法后,校正后的图像效果不好。我使用alpha = -1。 这是我的代码:

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)

有没有更好的方法来校正图像,或者我做错了。请帮助

编辑:校准的图像。 enter image description here

原始图片: enter image description here enter image description here

相机矩阵:

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]]

1 个答案:

答案 0 :(得分:1)

查看您的旋转矩阵R。如果您的摄像机是平行的,则它应该靠近单位矩阵。我使用此code检查了您的相机之间的角度,结果证明这些角度为[-4.44710871 -24.77842715 9.20475808],以度为单位。因此,我假设您的相机不是平行的,这就是为什么您的方法不起作用的原因。请参阅此thread中的答案,以了解如何针对您的情况进行正确的立体声矫正。