├── .gitignore ├── README.md ├── code ├── calibration.py ├── draw.py ├── main.py └── stereo_calibration.py ├── left ├── left01.jpg ├── left02.jpg ├── left03.jpg ├── left04.jpg ├── left05.jpg ├── left06.jpg ├── left07.jpg ├── left08.jpg ├── left09.jpg ├── left11.jpg ├── left12.jpg ├── left13.jpg └── left14.jpg ├── requirements.txt ├── result ├── calibresult │ └── left01.png ├── disparity │ └── ranging.png └── stereo_calibresult │ ├── calibrated.png │ ├── left01.png │ ├── matlab-calib.png │ └── right01.png └── right ├── right01.jpg ├── right02.jpg ├── right03.jpg ├── right04.jpg ├── right05.jpg ├── right06.jpg ├── right07.jpg ├── right08.jpg ├── right09.jpg ├── right11.jpg ├── right12.jpg ├── right13.jpg └── right14.jpg /.gitignore: -------------------------------------------------------------------------------- 1 | .idea/ 2 | 3 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # 普通相机图像测距 2 | 代码运行环境: `Python 3.6.3` 3 | 下载本仓库并安装相关包: 4 | 5 | git clone https://github.com/techkang/postgraduate 6 | cd postgraduate 7 | pip3 install -r requirements.txt 8 | ## 1.文件目录 9 | ├── result(存放相关结果) 10 | │   └── ... 11 | ├── code (代码) 12 | │   ├── calibration.py (单目标定、校正) 13 | │   ├── draw.py (推导视差公式时绘图的代码) 14 | │   ├── main.py (单、双目标定、校正及测距) 15 | │   └── stereo_calibration.py (双目标定、校正) 16 | ├── left (测试图片) 17 | │   ├── left01.jpg 18 | │   ├── ... 19 | │   └── left14.jpg 20 | ├── README.md 21 | ├── right (测试图片) 22 | │ ├── right01.jpg 23 | │ ├── ... 24 | │ └── right14.jpg 25 | └── requirements.txt 26 | 27 | ## 2.单目相机标定 28 | ### 运行代码 29 | 30 | cd code 31 | python3 calibration.py [file] 32 | 其中,file 是可选参数,默认为 01,表示对 left 文件夹中 left01.jpg 图像进行标定。可以指定其他值对left文件夹中的图片进行标定(注意0不能省略)。 33 | ### 运行结果 34 | 以 left01.jpg 为例,原图和修正后的图像如下: 35 | ![Alt](https://raw.githubusercontent.com/techkang/postgraduate/master/left/left01.jpg) 36 | ![Alt](https://raw.githubusercontent.com/techkang/postgraduate/master/result/calibresult/left01.png) 37 | ## 3.双目相机标定 38 | ### 运行代码 39 | 40 | cd code 41 | python3 stereo_calibration.py [file] 42 | 其中,file 是可选参数,默认为 01,表示对 left 文件夹中 left01.jpg 图像和 right 文件夹中的 right01.jpg 图像进行标定。可以指定其他值对left和right文件夹中的图片进行标定(注意0不能省略)。 43 | ### 运行结果 44 | 运行程序,输出对指定图片双目标定的结果,同时将结果保存在`result/stereo_calibresult/`文件夹下。本节代码未能按照预期的结果运行,输出照片发生了明显的旋转。以 left01.jpg 和 right01.jpg 为例,输出结果如图所示: 45 | ![Alt](https://raw.githubusercontent.com/techkang/postgraduate/master/result/stereo_calibresult/calibrated.png) 46 | 47 | 经过分析,发现 OpenCV 一般不能很好的标定双目图像,故使用 MATLAB 先标定图像,例如 left01 和 right01 的 R (旋转矩阵)和 T (平移矩阵)如下: 48 | 49 | ||R ||T| 50 | |:--:|:--:|:--:|:--:| 51 | |1|-0.0032|-0.0057|-83.0973| 52 | |0.0033|0.9999|0.0096|1.0605| 53 | |0.0057|-0.0097|0.9999|0.0392| 54 | | | | | | | 55 | 56 | 在程序中修改代码,手动指定 R 和 T 的值,运行程序,输出校正后的图像如下: 57 | ![Alt](https://raw.githubusercontent.com/techkang/postgraduate/master/result/stereo_calibresult/matlab-calib.png) 58 | 59 | ## 4.视差测距 60 | 本函数可以同时实现单目标定、双目标定和校正,以及测距。 61 | ### 运行代码 62 | 63 | cd code 64 | python3 mian.py [--key=value] 65 | 其中,`[--key=value]`是可选参数,可选择的值、含义及其默认值如下: 66 | 67 | sample = '01' # 测试图片 68 | disp_calib = False # 是否展示单目校正结果 69 | stereo_calib = True # 是否进行双目校正 70 | disp_stereo_calib = False # 是否展示双目校正结果 71 | disparity = True # 是否利用视差估算距离 72 | R = dict({'01': np.array([[1, -0.0032, -0.005], [0.0033, 0.9999, 0.0096], 73 | [0.0057, -0.0097, 0.9999]])}) # 由 MATLAB 标定的旋转矩阵 74 | T = dict({'01': np.array([-83.0973, 1.0605, 0.0392])}) # 由 MATLAB 标定的平移矩阵 75 | matlab = True # 在双目校正时是否使用 matlab 标定的值 76 | num = 3 # StereoSGBM_create 函数参数:最小可能的差异值 77 | blockSize = 5 # StereoSGBM_create 函数参数:匹配的块大小。 78 | 79 | 例如,输入命令`python3 main.py --disp_calib=True`,可以在测距的同时,还会输出单目测距的结果。如果保持默认值,则会对 left01 和 right01 图像进行测距,不输出单目校正和双目校正的结果,使用由 MATLAB 标定的旋转矩阵和平移矩阵。 80 | ### 运行结果 81 | 按照默认参数,输出结果如图: 82 | 83 | ![Alt](https://raw.githubusercontent.com/techkang/postgraduate/master/result/disparity/ranging.png) 84 | 85 | 点击左图,可以在命令行中输出该点的深度,然而,无论是从输出结果,还是右边的视差图,都离预期结果相差较远。点击左图时发现,经常有些点为负值。右图视差图中,标定板的视差应该相近,为一个平滑的渐变颜色,然而视差图没有体现出这一点。 -------------------------------------------------------------------------------- /code/calibration.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import glob 3 | import numpy as np 4 | 5 | import fire 6 | 7 | 8 | def calibration(des='01'): 9 | # termination criteria 10 | criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 30, 0.001) 11 | 12 | # prepare object points, like (0,0,0), (1,0,0), (2,0,0) ....,(6,5,0) 13 | objp = np.zeros((6 * 7, 3), np.float32) 14 | objp[:, :2] = np.mgrid[0:7, 0:6].T.reshape(-1, 2) 15 | 16 | # Arrays to store object points and image points from all the images. 17 | objpoints = [] # 3d point in real world space 18 | imgpoints = [] # 2d points in image plane. 19 | 20 | images = glob.glob('../left/*.jpg') 21 | 22 | for fname in images: 23 | img = cv2.imread(fname) 24 | gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) 25 | 26 | # Find the chess board corners 27 | ret, corners = cv2.findChessboardCorners(gray, (7, 6), None) 28 | 29 | # If found, add object points, image points (after refining them) 30 | if ret == True: 31 | objpoints.append(objp) 32 | 33 | corners2 = cv2.cornerSubPix(gray, corners, (11, 11), (-1, -1), criteria) 34 | imgpoints.append(corners2) 35 | 36 | # Draw and display the corners 37 | img = cv2.drawChessboardCorners(img, (7, 6), corners2, ret) 38 | cv2.imshow('img', img) 39 | cv2.waitKey(500) 40 | 41 | cv2.destroyAllWindows() 42 | 43 | ret, mtx, dist, rvecs, tvecs = cv2.calibrateCamera(objpoints, imgpoints, gray.shape[::-1], None, None) 44 | img = cv2.imread('../left/left' + str(des) + '.jpg') 45 | h, w = img.shape[:2] 46 | newcameramtx, roi = cv2.getOptimalNewCameraMatrix(mtx, dist, (w, h), 1, (w, h)) 47 | # undistort 48 | dst = cv2.undistort(img, mtx, dist, None, newcameramtx) 49 | 50 | # crop the image 51 | x, y, w, h = roi 52 | dst = dst[y:y + h, x:x + w] 53 | cv2.imwrite('../calibresult/left' + str(des) + '.png', dst) 54 | 55 | 56 | if __name__ == '__main__': 57 | fire.Fire(calibration) 58 | -------------------------------------------------------------------------------- /code/draw.py: -------------------------------------------------------------------------------- 1 | import matplotlib.pyplot as plt 2 | 3 | plt.scatter((-3,3,-2.25,2.25,0),(-1,-1,0,0,3)) 4 | 5 | plt.plot((-3,0),(-1,3),color='b') 6 | plt.plot((3,0),(-1,3),color='b') 7 | plt.plot((3,-3),(-1,-1),color='b') 8 | plt.plot((-2.25,2.25),(0,0),color='b') 9 | 10 | plt.plot((-2.25,-2.25),(0,-1),'--',color='black') 11 | plt.plot((0,0),(3,-1),'--',color='black') 12 | 13 | plt.text(1,-0.9,'b',fontdict={'size':16}) 14 | plt.text(-1, 0.2,'b-|d|',fontdict={'size':16}) 15 | 16 | plt.annotate(r'f', xy=(-2.25, -0.4), xycoords='data', xytext=(+30, -30), 17 | textcoords='offset points', fontsize=16, 18 | arrowprops=dict(arrowstyle='->', connectionstyle="arc3,rad=.2")) 19 | 20 | plt.annotate(r'z' , xy=(0, 1), xycoords='data', xytext=(+30, -30), 21 | textcoords='offset points', fontsize=16, 22 | arrowprops=dict(arrowstyle='->', connectionstyle="arc3,rad=.2")) 23 | 24 | plt.annotate(r'O' , xy=(-3, -1), xycoords='data', xytext=(+30, -30), 25 | textcoords='offset points', fontsize=16, 26 | arrowprops=dict(arrowstyle='->', connectionstyle="arc3,rad=.2")) 27 | 28 | plt.annotate(r"O'" , xy=(3, -1), xycoords='data', xytext=(+30, -30), 29 | textcoords='offset points', fontsize=16, 30 | arrowprops=dict(arrowstyle='->', connectionstyle="arc3,rad=.2")) 31 | 32 | plt.annotate(r'$p_l$' , xy=(-2.25, 0), xycoords='data', xytext=(+30, -30), 33 | textcoords='offset points', fontsize=16, 34 | arrowprops=dict(arrowstyle='->', connectionstyle="arc3,rad=.2")) 35 | 36 | plt.annotate(r'$p_r$' , xy=(2.25, 0), xycoords='data', xytext=(+30, -30), 37 | textcoords='offset points', fontsize=16, 38 | arrowprops=dict(arrowstyle='->', connectionstyle="arc3,rad=.2")) 39 | 40 | plt.annotate(r'$X$' , xy=(0, 3), xycoords='data', xytext=(+30, -30), 41 | textcoords='offset points', fontsize=16, 42 | arrowprops=dict(arrowstyle='->', connectionstyle="arc3,rad=.2")) 43 | 44 | plt.axis('off') 45 | 46 | plt.show() -------------------------------------------------------------------------------- /code/main.py: -------------------------------------------------------------------------------- 1 | # -*- coding:utf-8 -*- 2 | # __author__ = "Kang Sheng" 3 | import glob 4 | 5 | import cv2 6 | import numpy as np 7 | import matplotlib.pyplot as plt 8 | import fire 9 | 10 | 11 | class Config(object): 12 | sample = '01' # 测试图片 13 | disp_calib = False # 是否展示单目校正结果 14 | stereo_calib = True # 是否进行双目校正 15 | disp_stereo_calib = False # 是否展示双目校正结果 16 | disparity = True # 是否利用视差估算距离 17 | R = dict({'01': np.array([[1, -0.0032, -0.005], [0.0033, 0.9999, 0.0096], 18 | [0.0057, -0.0097, 0.9999]])}) # 由 MATLAB 标定的旋转矩阵 19 | T = dict({'01': np.array([-83.0973, 1.0605, 0.0392])}) # 由 MATLAB 标定的平移矩阵 20 | # TODO: 补充其他图像的R和T 21 | matlab = True # 在双目校正时是否使用 matlab 标定的值 22 | num = 3 # StereoSGBM_create 函数参数:最小可能的差异值 23 | blockSize = 5 # StereoSGBM_create 函数参数:匹配的块大小。 24 | 25 | 26 | opt = Config() 27 | 28 | 29 | def calibration(**kwargs): 30 | for k, v in kwargs.items(): 31 | setattr(opt, k, v) 32 | # termination criteria 33 | criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 30, 0.001) 34 | 35 | # prepare object points, like (0,0,0), (1,0,0), (2,0,0) ....,(6,5,0) 36 | objp = np.zeros((6 * 7, 3), np.float32) 37 | objp[:, :2] = np.mgrid[0:7, 0:6].T.reshape(-1, 2) 38 | 39 | # Arrays to store object points and image points from all the images. 40 | objpoints = [] # 3d point in real world space 41 | imgpoints = [] # 2d points in image plane. 42 | 43 | objpoints_r = [] 44 | imgpoints_r = [] 45 | 46 | images = glob.glob('../left/*.jpg') 47 | images_r = glob.glob('../right/*.jpg') 48 | images.sort() 49 | images_r.sort() 50 | 51 | for fname, fname_r in zip(images, images_r): 52 | img = cv2.imread(fname) 53 | gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) 54 | 55 | img_r = cv2.imread(fname_r) 56 | gray_r = cv2.cvtColor(img_r, cv2.COLOR_BGR2GRAY) 57 | 58 | # Find the chess board corners 59 | ret, corners = cv2.findChessboardCorners(gray, (7, 6), None) 60 | ret_r, corners_r = cv2.findChessboardCorners(gray_r, (7, 6), None) 61 | 62 | # If found, add object points, image points (after refining them) 63 | if ret == True and ret_r == True: 64 | objpoints.append(objp) 65 | objpoints_r.append(objp) 66 | 67 | corners2 = cv2.cornerSubPix(gray, corners, (11, 11), (-1, -1), 68 | criteria) 69 | corners2_r = cv2.cornerSubPix(gray_r, corners_r, (11, 11), (-1, -1), 70 | criteria) 71 | imgpoints.append(corners2) 72 | imgpoints_r.append(corners2_r) 73 | 74 | # Draw and display the corners 75 | if opt.disp_calib: 76 | cv2.imshow('img', img) 77 | cv2.waitKey(500) 78 | 79 | ret, mtx, dist, rvecs, tvecs = cv2.calibrateCamera(objpoints, imgpoints, 80 | gray.shape[::-1], None, 81 | None) 82 | img = cv2.imread('../left/left' + str(opt.sample) + '.jpg') 83 | h, w = img.shape[:2] 84 | newcameramtx, roi = cv2.getOptimalNewCameraMatrix(mtx, dist, (w, h), 1, 85 | (w, h)) 86 | # undistort 87 | dst = cv2.undistort(img, mtx, dist, None, newcameramtx) 88 | 89 | # crop the image 90 | x, y, w, h = roi 91 | dst = dst[y:y + h, x:x + w] 92 | if opt.disp_calib: 93 | cv2.imwrite('../calibresult/left' + str(opt.sample) + '.png', dst) 94 | 95 | ret, mtx_r, dist_r, rvecs, tvecs = cv2.calibrateCamera(objpoints_r, 96 | imgpoints_r, 97 | gray_r.shape[::-1], 98 | None, None) 99 | img_r = cv2.imread('../right/right' + str(opt.sample) + '.jpg') 100 | h, w = img_r.shape[:2] 101 | newcameramtx_r, roi = cv2.getOptimalNewCameraMatrix(mtx_r, dist_r, (w, h), 102 | 1, (w, h)) 103 | # undistort 104 | dst_r = cv2.undistort(img_r, mtx_r, dist_r, None, newcameramtx_r) 105 | 106 | # crop the image 107 | x, y, w, h = roi 108 | dst_r = dst_r[y:y + h, x:x + w] 109 | if opt.disp_calib: 110 | cv2.imwrite('../calibresult/right' + str(opt.sample) + '.png', dst) 111 | 112 | if not opt.stereo_calib: 113 | exit(0) 114 | 115 | retval, cameraMatrix1, distCoeffs1, cameraMatrix2, distCoeffs2, R, T, E, F = \ 116 | cv2.stereoCalibrate(objpoints, imgpoints, imgpoints_r, mtx, 117 | dist, mtx_r, dist_r, gray.shape[::-1]) 118 | 119 | if opt.matlab: 120 | try: 121 | R = opt.R[opt.sample] 122 | T = opt.T[opt.sample] 123 | except: 124 | print('Please modify config to add R and T for ' + opt.sample) 125 | 126 | R1, R2, P1, P2, Q, validPixROI1, validPixROI2 = cv2.stereoRectify( 127 | cameraMatrix1, distCoeffs1, cameraMatrix2, distCoeffs2, 128 | gray.shape[::-1], R, T) 129 | 130 | left_map1, left_map2 = cv2.initUndistortRectifyMap(cameraMatrix1, 131 | distCoeffs1, R1, P1, 132 | gray.shape[::-1], 133 | cv2.INTER_NEAREST) 134 | right_map1, right_map2 = cv2.initUndistortRectifyMap(cameraMatrix2, 135 | distCoeffs2, R2, 136 | P2, gray.shape[::-1], 137 | cv2.INTER_NEAREST) 138 | 139 | img = cv2.imread('../left/left' + str(opt.sample) + '.jpg') 140 | gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) 141 | 142 | img = cv2.imread(('../right/right' + str(opt.sample) + '.jpg')) 143 | gray_r = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) 144 | 145 | imgL = cv2.remap(gray, left_map1, left_map2, cv2.INTER_LINEAR) 146 | imgR = cv2.remap(gray_r, right_map1, right_map2, cv2.INTER_LINEAR) 147 | 148 | if opt.disp_stereo_calib: 149 | cv2.imwrite( 150 | '../result/stereo_calibresult/left' + str(opt.sample) + '.png', 151 | imgL) 152 | cv2.imwrite( 153 | '../result/stereo_calibresult/right' + str(opt.sample) + '.png', 154 | imgR) 155 | 156 | plt.subplot(121) 157 | plt.title('left') 158 | plt.imshow(imgL, cmap='gray') 159 | plt.axis('off') 160 | plt.subplot(122) 161 | plt.title('right') 162 | plt.imshow(imgR, cmap='gray') 163 | plt.axis('off') 164 | plt.show() 165 | 166 | if not opt.disparity: 167 | exit(0) 168 | 169 | cv2.namedWindow("depth") 170 | cv2.namedWindow("disparity") 171 | cv2.moveWindow("depth", 0, 0) 172 | cv2.moveWindow("disparity", 600, 0) 173 | 174 | def callbackFunc(e, x, y, f, p): 175 | if e == cv2.EVENT_LBUTTONDOWN: 176 | print(threeD[y][x]) 177 | 178 | cv2.setMouseCallback("depth", callbackFunc, None) 179 | 180 | stereo = cv2.StereoSGBM_create(numDisparities=16 * opt.num, 181 | blockSize=opt.blockSize) 182 | disparity = stereo.compute(imgL, imgR) 183 | 184 | disp = cv2.normalize(disparity, disparity, alpha=0, beta=255, 185 | norm_type=cv2.NORM_MINMAX, dtype=cv2.CV_8U) 186 | # 将图片扩展至3d空间中,其z方向的值则为当前的距离 187 | threeD = cv2.reprojectImageTo3D(disparity.astype(np.float32) / 16., Q) 188 | 189 | cv2.imshow("disparity", disp) 190 | cv2.imshow("depth", imgL) 191 | 192 | key = cv2.waitKey(0) 193 | if key == ord("q"): 194 | exit(0) 195 | elif key == ord("s"): 196 | cv2.imwrite("../result/disparity/disparity"+opt.sample+".png", disp) 197 | 198 | 199 | if __name__ == '__main__': 200 | fire.Fire(calibration) 201 | -------------------------------------------------------------------------------- /code/stereo_calibration.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import glob 3 | import numpy as np 4 | import matplotlib.pyplot as plt 5 | import scipy.io as scio 6 | 7 | import fire 8 | 9 | 10 | def calibration(des='01', data_file='../stat/matlab.mat'): 11 | # termination criteria 12 | criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 30, 0.001) 13 | 14 | # prepare object points, like (0,0,0), (1,0,0), (2,0,0) ....,(6,5,0) 15 | objp = np.zeros((6 * 7, 3), np.float32) 16 | objp[:, :2] = np.mgrid[0:7, 0:6].T.reshape(-1, 2) 17 | 18 | # Arrays to store object points and image points from all the images. 19 | objpoints = [] # 3d point in real world space 20 | imgpoints = [] # 2d points in image plane. 21 | 22 | objpoints_r = [] 23 | imgpoints_r = [] 24 | 25 | images = glob.glob('../left/*.jpg') 26 | images_r = glob.glob('../right/*.jpg') 27 | images.sort() 28 | images_r.sort() 29 | 30 | for fname, fname_r in zip(images, images_r): 31 | img = cv2.imread(fname) 32 | gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) 33 | 34 | img_r = cv2.imread(fname_r) 35 | gray_r = cv2.cvtColor(img_r, cv2.COLOR_BGR2GRAY) 36 | 37 | # Find the chess board corners 38 | ret, corners = cv2.findChessboardCorners(gray, (7, 6), None) 39 | ret_r, corners_r = cv2.findChessboardCorners(gray_r, (7, 6), None) 40 | 41 | # If found, add object points, image points (after refining them) 42 | if ret == True and ret_r == True: 43 | objpoints.append(objp) 44 | objpoints_r.append(objp) 45 | 46 | corners2 = cv2.cornerSubPix(gray, corners, (11, 11), (-1, -1), 47 | criteria) 48 | corners2_r = cv2.cornerSubPix(gray_r, corners_r, (11, 11), (-1, -1), 49 | criteria) 50 | imgpoints.append(corners2) 51 | imgpoints_r.append(corners2_r) 52 | 53 | # Draw and display the corners 54 | # cv2.imshow('img', img) 55 | # cv2.waitKey(500) 56 | 57 | ret, mtx, dist, rvecs, tvecs = cv2.calibrateCamera(objpoints, imgpoints, 58 | gray.shape[::-1], None, 59 | None) 60 | img = cv2.imread('../left/left' + str(des) + '.jpg') 61 | h, w = img.shape[:2] 62 | newcameramtx, roi = cv2.getOptimalNewCameraMatrix(mtx, dist, (w, h), 1, 63 | (w, h)) 64 | # undistort 65 | dst = cv2.undistort(img, mtx, dist, None, newcameramtx) 66 | 67 | # crop the image 68 | x, y, w, h = roi 69 | dst = dst[y:y + h, x:x + w] 70 | 71 | ret, mtx_r, dist_r, rvecs, tvecs = cv2.calibrateCamera(objpoints_r, 72 | imgpoints_r, 73 | gray_r.shape[::-1], 74 | None, None) 75 | img_r = cv2.imread('../right/right' + str(des) + '.jpg') 76 | h, w = img_r.shape[:2] 77 | newcameramtx_r, roi = cv2.getOptimalNewCameraMatrix(mtx_r, dist_r, (w, h), 78 | 1, (w, h)) 79 | # undistort 80 | dst_r = cv2.undistort(img_r, mtx_r, dist_r, None, newcameramtx_r) 81 | 82 | # crop the image 83 | x, y, w, h = roi 84 | dst_r = dst_r[y:y + h, x:x + w] 85 | 86 | retval, cameraMatrix1, distCoeffs1, cameraMatrix2, distCoeffs2, R, T, E, F = \ 87 | cv2.stereoCalibrate(objpoints, imgpoints, imgpoints_r, mtx, 88 | dist, mtx_r, dist_r, gray.shape[::-1]) 89 | 90 | R = np.array([[1, -0.0032, -0.005], [0.0033, 0.9999, 0.0096], 91 | [0.0057, -0.0097, 0.9999]]) 92 | T = np.array([-83.0973, 1.0605, 0.0392]) 93 | # TODO: import mat and read stat from mat file. 94 | 95 | R1, R2, P1, P2, Q, validPixROI1, validPixROI2 = cv2.stereoRectify( 96 | cameraMatrix1, distCoeffs1, cameraMatrix2, distCoeffs2, 97 | gray.shape[::-1], R, T) 98 | 99 | left_map1, left_map2 = cv2.initUndistortRectifyMap(cameraMatrix1, 100 | distCoeffs1, R1, P1, 101 | gray.shape[::-1], 102 | cv2.INTER_NEAREST) 103 | right_map1, right_map2 = cv2.initUndistortRectifyMap(cameraMatrix2, 104 | distCoeffs2, R2, 105 | P2, gray.shape[::-1], 106 | cv2.INTER_NEAREST) 107 | 108 | img = cv2.imread('../left/left' + str(des) + '.jpg') 109 | gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) 110 | 111 | img = cv2.imread(('../right/right' + str(des) + '.jpg')) 112 | gray_r = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) 113 | 114 | des_l = cv2.remap(gray, left_map1, left_map2, cv2.INTER_LINEAR) 115 | cv2.imwrite('../result/stereo_calibresult/left' + str(des) + '.png', des_l) 116 | des_r = cv2.remap(gray_r, right_map1, right_map2, cv2.INTER_LINEAR) 117 | cv2.imwrite('../result/stereo_calibresult/right' + str(des) + '.png', des_r) 118 | 119 | plt.subplot(121) 120 | plt.title('left') 121 | plt.imshow(des_l, cmap='gray') 122 | plt.axis('off') 123 | plt.subplot(122) 124 | plt.title('right') 125 | plt.imshow(des_r, cmap='gray') 126 | plt.axis('off') 127 | plt.show() 128 | 129 | 130 | if __name__ == '__main__': 131 | fire.Fire(calibration) 132 | -------------------------------------------------------------------------------- /left/left01.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/techkang/bi_calibration/5d315f81e4fd2daca00da950f8fdd12589fe928f/left/left01.jpg -------------------------------------------------------------------------------- /left/left02.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/techkang/bi_calibration/5d315f81e4fd2daca00da950f8fdd12589fe928f/left/left02.jpg -------------------------------------------------------------------------------- /left/left03.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/techkang/bi_calibration/5d315f81e4fd2daca00da950f8fdd12589fe928f/left/left03.jpg -------------------------------------------------------------------------------- /left/left04.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/techkang/bi_calibration/5d315f81e4fd2daca00da950f8fdd12589fe928f/left/left04.jpg -------------------------------------------------------------------------------- /left/left05.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/techkang/bi_calibration/5d315f81e4fd2daca00da950f8fdd12589fe928f/left/left05.jpg -------------------------------------------------------------------------------- /left/left06.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/techkang/bi_calibration/5d315f81e4fd2daca00da950f8fdd12589fe928f/left/left06.jpg -------------------------------------------------------------------------------- /left/left07.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/techkang/bi_calibration/5d315f81e4fd2daca00da950f8fdd12589fe928f/left/left07.jpg -------------------------------------------------------------------------------- /left/left08.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/techkang/bi_calibration/5d315f81e4fd2daca00da950f8fdd12589fe928f/left/left08.jpg -------------------------------------------------------------------------------- /left/left09.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/techkang/bi_calibration/5d315f81e4fd2daca00da950f8fdd12589fe928f/left/left09.jpg -------------------------------------------------------------------------------- /left/left11.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/techkang/bi_calibration/5d315f81e4fd2daca00da950f8fdd12589fe928f/left/left11.jpg -------------------------------------------------------------------------------- /left/left12.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/techkang/bi_calibration/5d315f81e4fd2daca00da950f8fdd12589fe928f/left/left12.jpg -------------------------------------------------------------------------------- /left/left13.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/techkang/bi_calibration/5d315f81e4fd2daca00da950f8fdd12589fe928f/left/left13.jpg -------------------------------------------------------------------------------- /left/left14.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/techkang/bi_calibration/5d315f81e4fd2daca00da950f8fdd12589fe928f/left/left14.jpg -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- 1 | opencv-python 2 | numpy 3 | fire 4 | matplotlib 5 | -------------------------------------------------------------------------------- /result/calibresult/left01.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/techkang/bi_calibration/5d315f81e4fd2daca00da950f8fdd12589fe928f/result/calibresult/left01.png -------------------------------------------------------------------------------- /result/disparity/ranging.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/techkang/bi_calibration/5d315f81e4fd2daca00da950f8fdd12589fe928f/result/disparity/ranging.png -------------------------------------------------------------------------------- /result/stereo_calibresult/calibrated.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/techkang/bi_calibration/5d315f81e4fd2daca00da950f8fdd12589fe928f/result/stereo_calibresult/calibrated.png -------------------------------------------------------------------------------- /result/stereo_calibresult/left01.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/techkang/bi_calibration/5d315f81e4fd2daca00da950f8fdd12589fe928f/result/stereo_calibresult/left01.png -------------------------------------------------------------------------------- /result/stereo_calibresult/matlab-calib.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/techkang/bi_calibration/5d315f81e4fd2daca00da950f8fdd12589fe928f/result/stereo_calibresult/matlab-calib.png -------------------------------------------------------------------------------- /result/stereo_calibresult/right01.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/techkang/bi_calibration/5d315f81e4fd2daca00da950f8fdd12589fe928f/result/stereo_calibresult/right01.png -------------------------------------------------------------------------------- /right/right01.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/techkang/bi_calibration/5d315f81e4fd2daca00da950f8fdd12589fe928f/right/right01.jpg -------------------------------------------------------------------------------- /right/right02.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/techkang/bi_calibration/5d315f81e4fd2daca00da950f8fdd12589fe928f/right/right02.jpg -------------------------------------------------------------------------------- /right/right03.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/techkang/bi_calibration/5d315f81e4fd2daca00da950f8fdd12589fe928f/right/right03.jpg -------------------------------------------------------------------------------- /right/right04.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/techkang/bi_calibration/5d315f81e4fd2daca00da950f8fdd12589fe928f/right/right04.jpg -------------------------------------------------------------------------------- /right/right05.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/techkang/bi_calibration/5d315f81e4fd2daca00da950f8fdd12589fe928f/right/right05.jpg -------------------------------------------------------------------------------- /right/right06.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/techkang/bi_calibration/5d315f81e4fd2daca00da950f8fdd12589fe928f/right/right06.jpg -------------------------------------------------------------------------------- /right/right07.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/techkang/bi_calibration/5d315f81e4fd2daca00da950f8fdd12589fe928f/right/right07.jpg -------------------------------------------------------------------------------- /right/right08.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/techkang/bi_calibration/5d315f81e4fd2daca00da950f8fdd12589fe928f/right/right08.jpg -------------------------------------------------------------------------------- /right/right09.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/techkang/bi_calibration/5d315f81e4fd2daca00da950f8fdd12589fe928f/right/right09.jpg -------------------------------------------------------------------------------- /right/right11.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/techkang/bi_calibration/5d315f81e4fd2daca00da950f8fdd12589fe928f/right/right11.jpg -------------------------------------------------------------------------------- /right/right12.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/techkang/bi_calibration/5d315f81e4fd2daca00da950f8fdd12589fe928f/right/right12.jpg -------------------------------------------------------------------------------- /right/right13.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/techkang/bi_calibration/5d315f81e4fd2daca00da950f8fdd12589fe928f/right/right13.jpg -------------------------------------------------------------------------------- /right/right14.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/techkang/bi_calibration/5d315f81e4fd2daca00da950f8fdd12589fe928f/right/right14.jpg --------------------------------------------------------------------------------