def depth_map(img1, img2): (img1, img2) = rectification.undistort_rectify(cam, img1, img2) global disparity disparity = stereo.compute(img1, img2) disparity = np.uint8(disparity) cv2.imshow('disparity', disparity) cv2.waitKey(0)
def depth_map_from_cams(left, right, width, height): cap_left = cv2.VideoCapture(left) cap_right = cv2.VideoCapture(right) while True: ret1, img1 = cap_left.read() ret2, img2 = cap_right.read() # Wait until ret1/ret2 is true since camera setup needs some time. if ret1 and ret2: img1 = cv2.cvtColor(img1, cv2.COLOR_BGR2GRAY) img2 = cv2.cvtColor(img2, cv2.COLOR_BGR2GRAY) # Resize img1 = cv2.resize(img1, (width, height)) img2 = cv2.resize(img2, (width, height)) # Rectify, undistort (img1, img2) = rectification.undistort_rectify(cam, img1, img2) disparity = stereo.compute(img1,img2) rows = 6 cols = 8 # found_left, corners_left = cv2.findChessboardCorners(img1, (rows, cols)) # found_right, corners_right = cv2.findChessboardCorners(img2, (rows, cols)) # if found_left and found_right: # img1 = calibration.draw_horizontal_lines(img1, corners_left) # img2 = calibration.draw_horizontal_lines(img2, corners_right) # cv2.imshow("horizontal lines on the stereo pair".format(0), np.hstack((img1, img2))) # min = disparity.min() # max = disparity.max() # disparity = np.uint8(255 * (disparity - min) / (max - min)) disparity = np.uint8(disparity) # cv2.imshow("disparity", np.hstack((img1, img2, disparity))) cv2.imshow("disparity", disparity) if cv2.waitKey(1) & 0xFF == ord('q'): print (bm) break # Capture an image and save if cv2.waitKey(1) == ord('s'): cv2.imwrite("left.jpg", img1) cv2.imwrite("right.jpg", img2) print ("image saved") cap_left.release() cap_right.release() cv2.destroyAllWindows()
def show_rectified_cameras(left, right, width, height, stereoCamera): """ Show two rectified cameras in a horizontal aligned window """ cap_left = cv2.VideoCapture(left) w = cap_left.get(cv2.CAP_PROP_FRAME_WIDTH) h = cap_left.get(cv2.CAP_PROP_FRAME_HEIGHT) print(w, h) cap_right = cv2.VideoCapture(right) size = (width, height) print("resized to: ", size) while True: ret1, img1 = cap_left.read() ret2, img2 = cap_right.read() # Wait until ret1 and ret2 are true since camera setup needs some time. if ret1 and ret2: img1 = cv2.cvtColor(img1, cv2.COLOR_BGR2GRAY) img2 = cv2.cvtColor(img2, cv2.COLOR_BGR2GRAY) img1 = cv2.resize(img1, size) img2 = cv2.resize(img2, size) (img1, img2) = rectification.undistort_rectify(stereoCamera, img1, img2) img1 = rectification.draw_horizontal_lines(img1) img2 = rectification.draw_horizontal_lines(img2) cv2.imshow("rectified stereo cameras", np.hstack((img1, img2))) if cv2.waitKey(1) & 0xFF == ord('q'): break cap_left.release() cap_right.release() cv2.destroyAllWindows()
Undistort and rectify a stereo image pair given the calibration parameters. Horizontal lines will be drawn on the undistorted & rectified pair to verify the result. Example usage: python show_rectified.py calib_params.yml stereo_images/0L.jpg stereo_images/0R.jpg """ parser = argparse.ArgumentParser( description= 'Show undistorted & rectified stereo image pair. The input left img1 and right img2 is the original distorted and unrectified images. This program undistort and rectify img1 and img2 using calibration parameters read from a yaml file.' ) parser.add_argument( 'calibration_file', help='the yaml file containing the calibration pararmeters (.yml)') parser.add_argument('img1', help='left image') parser.add_argument('img2', help='right image') args = parser.parse_args() cam = StereoCamera(args.calibration_file) img1 = cv2.imread(args.img1, 0) img2 = cv2.imread(args.img2, 0) (img1, img2) = rectification.undistort_rectify(cam, img1, img2) img1 = rectification.draw_horizontal_lines(img1) img2 = rectification.draw_horizontal_lines(img2) cv2.imshow("horizontal lines on stereo pair", np.hstack((img1, img2))) cv2.waitKey(0)