def __init__(self, calib_foler='calib_files_test', SGBM_setting='settings/SGBM'): self.calibration = StereoCalibration(input_folder=calib_foler) self.block_matcher = StereoSGBM() self.block_matcher.load_settings(settings=SGBM_setting) self.CalibratedPair = CalibratedPair(None, self.calibration, self.block_matcher) self.imageSize = (1280, 720) self.left_img = None self.right_img = None
def main(): """Produce PLY point clouds from stereo image pair.""" parser = argparse.ArgumentParser(description="Read images taken with " "stereo pair and use them to produce 3D " "point clouds that can be viewed with " "MeshLab.", parents=[STEREO_BM_FLAG]) parser.add_argument("calibration", help="Path to calibration folder.") parser.add_argument("left", help="Path to left image") parser.add_argument("right", help="Path to right image") parser.add_argument("output", help="Path to output file.") parser.add_argument("--bm_settings", help="Path to block matcher's settings.") args = parser.parse_args() image_pair = [cv2.imread(image) for image in [args.left, args.right]] calib_folder = args.calibration if args.use_stereobm: block_matcher = StereoBM() else: block_matcher = StereoSGBM() if args.bm_settings: block_matcher.load_settings(args.bm_settings) camera_pair = CalibratedPair(None, StereoCalibration(input_folder=calib_folder), block_matcher) rectified_pair = camera_pair.calibration.rectify(image_pair) points = camera_pair.get_point_cloud(rectified_pair) points = points.filter_infinity() points.write_ply(args.output)
def __init__(self, calib_foler = 'calib_files_test', SGBM_setting = 'settings/SGBM'): self.calibration = StereoCalibration(input_folder=calib_foler) self.block_matcher = StereoSGBM() self.block_matcher.load_settings(settings=SGBM_setting) self.CalibratedPair = CalibratedPair(None, self.calibration, self.block_matcher) self.imageSize = (1280, 720) self.left_img = None self.right_img = None
def main(): parser = ArgumentParser( description="Read images taken from a calibrated " "stereo pair, compute disparity maps from them and " "show them interactively to the user, allowing the " "user to tune the stereo block matcher settings in " "the GUI.", parents=[STEREO_BM_FLAG]) parser.add_argument( "calibration_folder", help="Directory where calibration files for the stereo " "pair are stored.") parser.add_argument("image_folder", help="Directory where input images are stored.") parser.add_argument("--bm_settings", help="File to save last block matcher settings to.", default="") args = parser.parse_args() calibration = StereoCalibration(input_folder=args.calibration_folder) input_files = find_files(args.image_folder) if args.use_stereobm: block_matcher = StereoBM() else: block_matcher = StereoSGBM() image_pair = [cv2.imread(image) for image in input_files[:2]] input_files = input_files[2:] rectified_pair = calibration.rectify(image_pair) tuner = BMTuner(block_matcher, calibration, rectified_pair) while input_files: image_pair = [cv2.imread(image) for image in input_files[:2]] rectified_pair = calibration.rectify(image_pair) tuner.tune_pair(rectified_pair) input_files = input_files[2:] for param in block_matcher.parameter_maxima: print("{}\n".format(tuner.report_settings(param))) if args.bm_settings: block_matcher.save_settings(args.bm_settings)
class Point_cloud: def __init__(self, calib_foler='calib_files_test', SGBM_setting='settings/SGBM'): self.calibration = StereoCalibration(input_folder=calib_foler) self.block_matcher = StereoSGBM() self.block_matcher.load_settings(settings=SGBM_setting) self.CalibratedPair = CalibratedPair(None, self.calibration, self.block_matcher) self.imageSize = (1280, 720) self.left_img = None self.right_img = None def load_image(self, leftPath, rightPath): self.left_img = cv2.imread(leftPath) self.right_img = cv2.imread(rightPath) def rectify_image(self): return self.calibration.rectify([self.left_img, self.right_img]) def get_disparity(self): imagePair = self.rectify_image() return self.block_matcher.get_disparity(imagePair) def get_point_cloud(self, filename): rectify_pair = self.CalibratedPair.calibration.rectify( [self.left_img, self.right_img]) points = self.CalibratedPair.get_point_cloud(rectify_pair) points = points.filter_infinity() points.write_ply(filename) def highlight_point_cloud(self, rectangle): rectify_pair = self.CalibratedPair.calibration.rectify( [self.left_img, self.right_img]) disparity = self.get_disparity() points = self.block_matcher.get_3d(disparity, self.calibration.disp_to_depth_mat) colors = rectify_pair[0] #Conver to homogeneous coord pta = rectangle[0] + (1, ) ptb = rectangle[1] + (1, ) ptc = rectangle[2] + (1, ) ptd = rectangle[3] + (1, ) for row in range(0, self.imageSize[1]): for col in range(0, self.imageSize[0]): p = (col, row, 1) if pointInQuadrilateral(p, pta, ptb, ptc, ptd): colors[row, col, 0] = 255 colors[row, col, 1] = 0 colors[row, col, 2] = 0 point_cloud = PointCloud(points, colors) point_cloud.write_ply('test.ply') def find_pos(self, quadra): rectify_pair = self.CalibratedPair.calibration.rectify( [self.left_img, self.right_img]) disparity = self.block_matcher.get_disparity(rectify_pair) points = self.block_matcher.get_3d(disparity, self.calibration.disp_to_depth_mat) #Find the larger rectangle that contains quadrilateral rmin = min(map(lambda x: x[0], quadra)) rmax = max(map(lambda x: x[0], quadra)) cmin = min(map(lambda x: x[1], quadra)) cmax = max(map(lambda x: x[1], quadra)) point_lst = [] pta = quadra[0] + (1, ) ptb = quadra[1] + (1, ) ptc = quadra[2] + (1, ) ptd = quadra[3] + (1, ) for row in range(rmin, rmax + 1): for col in range(cmin, cmax + 1): p = (col, row, 1) # if pointInQuadrilateral(p, pta, ptb, ptc, ptd): point_lst.append(points[col, row]) x_list = map(lambda x: x[0], point_lst) x_list.sort() y_list = map(lambda x: x[1], point_lst) y_list.sort() z_list = map(lambda x: x[2], point_lst) z_list.sort() print(len(x_list)) median = (x_list[len(x_list) / 2], y_list[len(y_list) / 2], z_list[len(z_list) / 3]) return median
import cv2 from stereovision.blockmatchers import StereoBM, StereoSGBM from stereovision.calibration import StereoCalibration from stereovision.stereo_cameras import CalibratedPair from stereovision.ui_utils import STEREO_BM_FLAG if __name__ == '__main__': image_pair = [ cv2.imread(image) for image in ['./test/left_test.jpg', './test/right_test.jpg'] ] calib_folder = 'calibration_result' use_stereobm = False bm_settings = 'bm_setting' if use_stereobm: block_matcher = StereoBM() else: block_matcher = StereoSGBM() if bm_settings: block_matcher.load_settings(bm_settings) camera_pair = CalibratedPair(None, StereoCalibration(input_folder=calib_folder), block_matcher) rectified_pair = camera_pair.calibration.rectify(image_pair) points = camera_pair.get_point_cloud(rectified_pair) points = points.filter_infinity() points.write_ply('./test/test_poc')
class Point_cloud: def __init__(self, calib_foler = 'calib_files_test', SGBM_setting = 'settings/SGBM'): self.calibration = StereoCalibration(input_folder=calib_foler) self.block_matcher = StereoSGBM() self.block_matcher.load_settings(settings=SGBM_setting) self.CalibratedPair = CalibratedPair(None, self.calibration, self.block_matcher) self.imageSize = (1280, 720) self.left_img = None self.right_img = None def load_image(self, leftPath, rightPath): self.left_img = cv2.imread(leftPath) self.right_img = cv2.imread(rightPath) def rectify_image(self): return self.calibration.rectify([self.left_img, self.right_img]) def get_disparity(self): imagePair = self.rectify_image() return self.block_matcher.get_disparity(imagePair) def get_point_cloud(self, filename): rectify_pair = self.CalibratedPair.calibration.rectify([self.left_img, self.right_img]) points = self.CalibratedPair.get_point_cloud(rectify_pair) points = points.filter_infinity() points.write_ply(filename) def highlight_point_cloud(self, rectangle): rectify_pair = self.CalibratedPair.calibration.rectify([self.left_img, self.right_img]) disparity = self.get_disparity() points = self.block_matcher.get_3d(disparity, self.calibration.disp_to_depth_mat) colors = rectify_pair[0] #Conver to homogeneous coord pta = rectangle[0] + (1,) ptb = rectangle[1] + (1,) ptc = rectangle[2] + (1,) ptd = rectangle[3] + (1,) for row in range(0, self.imageSize[1]): for col in range(0, self.imageSize[0]): p = (col, row, 1) if pointInQuadrilateral(p, pta, ptb, ptc, ptd): colors[row, col, 0] = 255 colors[row, col, 1] = 0 colors[row, col, 2] = 0 point_cloud = PointCloud(points, colors) point_cloud.write_ply('test.ply') def find_pos(self, quadra): rectify_pair = self.CalibratedPair.calibration.rectify([self.left_img, self.right_img]) disparity = self.block_matcher.get_disparity(rectify_pair) points = self.block_matcher.get_3d(disparity, self.calibration.disp_to_depth_mat) #Find the larger rectangle that contains quadrilateral rmin = min(map(lambda x: x[0], quadra)) rmax = max(map(lambda x: x[0], quadra)) cmin = min(map(lambda x: x[1], quadra)) cmax = max(map(lambda x: x[1], quadra)) point_lst = [] pta = quadra[0] + (1,) ptb = quadra[1] + (1,) ptc = quadra[2] + (1,) ptd = quadra[3] + (1,) for row in range(rmin, rmax + 1): for col in range(cmin, cmax + 1): p = (col, row, 1) # if pointInQuadrilateral(p, pta, ptb, ptc, ptd): point_lst.append(points[col, row]) x_list = map(lambda x: x[0], point_lst) x_list.sort() y_list = map(lambda x: x[1], point_lst) y_list.sort() z_list = map(lambda x: x[2], point_lst) z_list.sort() print(len(x_list)) median = (x_list[len(x_list) / 2], y_list[len(y_list) / 2], z_list[len(z_list) / 3]) return median