1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217
| import numpy as np import cv2 import pcl
class stereoCamera(object): def __init__(self): self.cam_matrix_left = np.array([[368.675898674749, -1.50245100976057, 308.223174252045], [0, 369.172983810346, 184.420046614954], [0, 0, 1]]) self.cam_matrix_right = np.array([[374.452750889965, -0.933263166338735, 284.273373661522], [0, 374.79807385038, 196.866606672554], [0, 0, 1]])
self.distortion_l = np.array([[-0.0169072200281734, -0.0393656307016634, 0.00315587347941657, -0.00142406381437646, 0]])
self.distortion_r = np.array([[-0.137714949099853, 0.347043476849145, 0.00882677131371728, -0.0137088374523692, 0]])
self.R = np.array([[0.997827201730772, 0.000619636065025224, 0.0658824068874556], [0.00205989330848618, 0.999173532016587, -0.0405956864340979], [-0.0658531117388919, 0.0406431909259702, 0.997001253111379]])
self.T = np.array([[-60.1391668881605], [-0.22679212052219], [4.53161173378334]])
self.focal_length = 859.367
self.baseline = 61
def preprocess(img1, img2): if (img1.ndim == 3): img1 = cv2.cvtColor(img1, cv2.COLOR_BGR2GRAY) if (img2.ndim == 3): img2 = cv2.cvtColor(img2, cv2.COLOR_BGR2GRAY)
img1 = cv2.equalizeHist(img1) img2 = cv2.equalizeHist(img2)
return img1, img2
def undistortion(image, camera_matrix, dist_coeff): undistortion_image = cv2.undistort(image, camera_matrix, dist_coeff)
return undistortion_image
def getRectifyTransform(height, width, config): left_K = config.cam_matrix_left right_K = config.cam_matrix_right left_distortion = config.distortion_l right_distortion = config.distortion_r R = config.R T = config.T
R1, R2, P1, P2, Q, roi1, roi2 = cv2.stereoRectify(left_K, left_distortion, right_K, right_distortion, (width, height), R, T, alpha=0)
map1x, map1y = cv2.initUndistortRectifyMap(left_K, left_distortion, R1, P1, (width, height), cv2.CV_32FC1) map2x, map2y = cv2.initUndistortRectifyMap(right_K, right_distortion, R2, P2, (width, height), cv2.CV_32FC1)
return map1x, map1y, map2x, map2y, Q
def rectifyImage(image1, image2, map1x, map1y, map2x, map2y): rectifyed_img1 = cv2.remap(image1, map1x, map1y, cv2.INTER_AREA) rectifyed_img2 = cv2.remap(image2, map2x, map2y, cv2.INTER_AREA)
return rectifyed_img1, rectifyed_img2
def draw_line(image1, image2): height = max(image1.shape[0], image2.shape[0]) width = image1.shape[1] + image2.shape[1]
output = np.zeros((height, width, 3), dtype=np.uint8) output[0:image1.shape[0], 0:image1.shape[1]] = image1 output[0:image2.shape[0], image1.shape[1]:] = image2
line_interval = 50 for k in range(height // line_interval): cv2.line(output, (0, line_interval * (k + 1)), (2 * width, line_interval * (k + 1)), (0, 255, 0), thickness=2, lineType=cv2.LINE_AA)
return output
def stereoMatchSGBM(left_image, right_image, down_scale=False): if left_image.ndim == 2: img_channels = 1 else: img_channels = 3 blockSize = 3 paraml = {'minDisparity': 0, 'numDisparities': 128, 'blockSize': blockSize, 'P1': 8 * img_channels * blockSize ** 2, 'P2': 32 * img_channels * blockSize ** 2, 'disp12MaxDiff': 1, 'preFilterCap': 63, 'uniquenessRatio': 15, 'speckleWindowSize': 100, 'speckleRange': 1, 'mode': cv2.STEREO_SGBM_MODE_SGBM_3WAY }
left_matcher = cv2.StereoSGBM_create(**paraml) paramr = paraml paramr['minDisparity'] = -paraml['numDisparities'] right_matcher = cv2.StereoSGBM_create(**paramr)
size = (left_image.shape[1], left_image.shape[0]) if down_scale == False: disparity_left = left_matcher.compute(left_image, right_image) disparity_right = right_matcher.compute(right_image, left_image)
else: left_image_down = cv2.pyrDown(left_image) right_image_down = cv2.pyrDown(right_image) factor = left_image.shape[1] / left_image_down.shape[1]
disparity_left_half = left_matcher.compute(left_image_down, right_image_down) disparity_right_half = right_matcher.compute(right_image_down, left_image_down) disparity_left = cv2.resize(disparity_left_half, size, interpolation=cv2.INTER_AREA) disparity_right = cv2.resize(disparity_right_half, size, interpolation=cv2.INTER_AREA) disparity_left = factor * disparity_left disparity_right = factor * disparity_right
trueDisp_left = disparity_left.astype(np.float32) / 16. trueDisp_right = disparity_right.astype(np.float32) / 16.
return trueDisp_left, trueDisp_right
if __name__ == '__main__':
i = 3
video = cv2.VideoCapture(1)
video.set(cv2.CAP_PROP_FRAME_WIDTH, 2560) video.set(cv2.CAP_PROP_FRAME_HEIGHT, 720) if video.isOpened(): while True: ret, frame = video.read() if not ret: print("Video End!") break iml = frame[0:720, 0:1280] imr = frame[0:720, 1280:2560]
height, width = iml.shape[0:2]
config = stereoCamera()
map1x, map1y, map2x, map2y, Q = getRectifyTransform(height, width, config) iml_rectified, imr_rectified = rectifyImage(iml, imr, map1x, map1y, map2x, map2y)
line = draw_line(iml_rectified, imr_rectified)
disp, _ = stereoMatchSGBM(iml_, imr_, True)
points_3d = cv2.reprojectImageTo3D(disp, Q)
def onMouse(event, x, y, flags, param): if event == cv2.EVENT_LBUTTONDOWN: dis = ((points_3d[y, x, 0] ** 2 + points_3d[y, x, 1] ** 2 + points_3d[y, x, 2] ** 2) ** 0.5) / 500 print('点的相对距离为 %0.3f cm' % dis)
cv2.namedWindow("disparity", 0) cv2.imshow("disparity", disp) cv2.setMouseCallback("disparity", onMouse, 0)
cv2.namedWindow("iml", 0) cv2.imshow("iml", iml)
if cv2.waitKey(1) & 0xFF == ord('q'): break pass pass
|