def createMinHpatches(): root = symlink('hpatches') all_folders = sorted(os.listdir(root)) v_folders = [ os.path.join(root, i) for i in all_folders if i.startswith('v_') ] sequences = [fromFolder(i) for i in v_folders] print('Original resolutions:') for seq in sequences: print('%s: %d x %d' % (seq.name, seq.images[0].shape[0], seq.images[0].shape[1])) for seq in sequences: while np.any(np.array(seq.images[0].shape) > 1000): seq.downSample() print('') print('New resolutions:') for seq in sequences: print('%s: %d x %d' % (seq.name, seq.images[0].shape[0], seq.images[0].shape[1])) dest = symlink('min_hpatches') os.makedirs(dest) for seq in sequences: seq.write(dest)
def __init__(self, sequence_id): seq_folder = os.path.join(symlink('kitti'), sequence_id) images, right_images = utils_path.imagesFromSubdirs( seq_folder, sub_names=['image_0', 'image_1'], extension='.png') assert len(images) == len(right_images) assert len(images) > 0 poses = np.loadtxt( os.path.join(symlink('kitti'), 'poses', sequence_id + '.txt')) assert poses.shape[0] == len(images) T_W_C = [ pose.fromMatrix(np.reshape(poses[i, :], (3, 4))) for i in range(poses.shape[0]) ] calibs = np.loadtxt(os.path.join(seq_folder, 'calib.txt'), usecols=range(1, 13)) assert len(calibs) == 4 left_K = np.reshape(calibs[0], [3, 4])[:3, :3] right_K = np.reshape(calibs[1], [3, 4])[:3, :3] baseline = 0.54 base.RectifiedStereoSequence.__init__(self, images, right_images, left_K, right_K, T_W_C, baseline, 'kt', sequence_id)
def makeCroppedGraySequence(seq_id, num_threads=8): src_folder = os.path.join(symlink('robotcar'), seq_id) dst_folder = os.path.join(symlink('robotcar_cropped_gray'), seq_id) if not os.path.exists(dst_folder): os.makedirs(dst_folder) # Convert images. stereo_dir = os.path.join(src_folder, 'stereo') for leftright in ['left', 'right']: in_dir = os.path.join(stereo_dir, leftright) images = utils_path.imagesFromDir(in_dir, extension='.png') im_names = [os.path.basename(i) for i in images] cam_model = camera_model.CameraModel(cam_model_dir, in_dir) out_dir = os.path.join(dst_folder, 'rect', leftright) if not os.path.exists(out_dir): os.makedirs(out_dir) p = multiprocessing.Pool(num_threads) print('Converting %s...' % leftright) p.map(ParallelImageConverter(cam_model, in_dir, out_dir, im_names), range(len(images))) # K files # Image downscaling affects camera intrinsics f = [0.5 * x for x in cam_model.focal_length] c = [0.5 * x for x in cam_model.principal_point] K = np.array([[f[0], 0, c[0]], [0, f[1], c[1]], [0, 0, 1]]) np.savetxt(os.path.join(dst_folder, '%s_K.txt' % leftright), K) times = [int(i[:16]) for i in im_names] # Get poses everywhere. T_W_C_file = os.path.join(dst_folder, 'T_W_C.txt') if not os.path.exists(T_W_C_file): print('Interpolating poses...') insext = np.loadtxt(os.path.join(sdk_dir, 'extrinsics', 'ins.txt')).tolist() mtx = transform.build_se3_transform(insext) # Cx: Oxford style camera, where x looks into the image plane. T_I_Cx = pose.fromMatrix(np.asarray(mtx)).inverse() T_Cx_C = pose.yRotationDeg(90) * pose.zRotationDeg(90) T_I_C = T_I_Cx * T_Cx_C # T_W_I0: Because in Oxford, z looks down. T_W_I0 = pose.xRotationDeg(180) ins_path = os.path.join(src_folder, 'gps', 'ins.csv') T_I0_I = interpolate_poses.interpolate_ins_poses( ins_path, times, times[0]) T_I0_I = [pose.fromMatrix(np.asarray(i)) for i in T_I0_I] T_W_C = [T_W_I0 * i * T_I_C for i in T_I0_I] T_W_C_serialized = np.array([i.asArray().ravel() for i in T_W_C]) np.savetxt(T_W_C_file, T_W_C_serialized)
def __init__(self, seq_id): seq_folder = os.path.join(symlink('robotcar_cropped_gray'), seq_id) if not os.path.exists(seq_folder): raise Exception('Need to call makeCroppedGraySequence(\'%s\') to ' 'preprocess the raw data!' % seq_id) ims_dir = os.path.join(seq_folder, 'rect') left_ims, right_ims = utils_path.imagesFromSubdirs(ims_dir, extension='.png') assert len(left_ims) == len(right_ims) assert len(left_ims) > 0 k_files = [ os.path.join(seq_folder, '%s_K.txt' % i) for i in ['left', 'right'] ] Ks = [np.loadtxt(k_file) for k_file in k_files] T_W_C_file = os.path.join(seq_folder, 'T_W_C.txt') T_W_C_serialized = np.loadtxt(T_W_C_file) T_W_C = [ pose.fromMatrix(np.reshape(T_W_C_serialized[i, :], (4, 4))) for i in range(T_W_C_serialized.shape[0]) ] base.RectifiedStereoSequence.__init__(self, left_ims, right_ims, Ks[0], Ks[1], T_W_C, 0.24, 'rc', seq_id)
def __init__(self, tvt, use_min=False): if use_min: root = symlink('min_hpatches') if not os.path.exists(root): raise Exception('Create min_hpatches with createMinHpatches()') ext = 'pgm' else: root = symlink('hpatches') ext = 'ppm' folders = split(root, tvt) self.folder_names = [os.path.basename(path) for path in folders] self._seqs = [] for folder in folders: self._seqs.append(fromFolder(folder, ext)) self._pairs_per_seq = 15 print('Hpatches for %s has length %d' % (tvt, len(self)))
def __init__(self, sequence_id): root = os.path.join(symlink('euroc'), sequence_id, 'mav0') lr_images = [None, None] for i in [0, 1]: image_folder = os.path.join(root, 'rect%d' % i) if not os.path.exists(image_folder): raise Exception( 'Need to call euroc.undistort() on this sequence first!') K = np.loadtxt(os.path.join(image_folder, 'K.txt')) image_folder_contents = sorted(os.listdir(image_folder)) if i == 0: image_times = [int(j[:-4]) for j in image_folder_contents if j.endswith('.png')] lr_images[i] = [os.path.join(image_folder, j) for j in image_folder_contents if j.endswith('.png')] assert len(lr_images[0]) == len(lr_images[1]) left_images = lr_images[0] right_images = lr_images[1] gt_path = os.path.join(root, 'state_groundtruth_estimate0') gt_data_file = os.path.join(gt_path, 'data.csv') gt_data = np.loadtxt(gt_data_file, skiprows=1, delimiter=',') # Only interestd in poses at image times: vicon_filter = [] for image_time in image_times: vicon_filter.append(np.argmin( np.abs(gt_data[:, 0] - image_time))) gt_data = gt_data[vicon_filter, :] T_W_GT = [pose.Pose( quat.Quaternion(row[4:8]).rotation_matrix, row[1:4].reshape((3, 1))) for row in gt_data] gt_extr_file = os.path.join(gt_path, 'sensor.yaml') yam = yaml.load(file(gt_extr_file, 'r')) T_B_GT_arr = np.array(yam['T_BS']['data']).reshape((4, 4)) T_B_GT = pose.fromApproximateMatrix(T_B_GT_arr) T_GT_B = T_B_GT.inverse() # TODO: Dataset-dependent? cams = [Cam(root, i) for i in [0, 1]] T_W_C = [i * T_GT_B * cams[0].T_B_C for i in T_W_GT] baseline = (cams[0].T_B_C.inverse() * cams[1].T_B_C).t[0] print(image_folder) base.RectifiedStereoSequence.__init__( self, left_images, right_images, K, K, T_W_C, baseline, 'euroc', sequence_id)
def undistort(sequence_id): root = os.path.join(symlink('euroc'), sequence_id, 'mav0') cams = [Cam(root, i) for i in [0, 1]] assert np.all(cams[0].resolution == cams[1].resolution) T_C1_C0 = cams[1].T_B_C.inverse() * cams[0].T_B_C R1, R2, P1, P2, _, _, _ = cv2.stereoRectify( cams[0].K, cams[0].dist, cams[1].K, cams[1].dist, tuple(cams[0].resolution), T_C1_C0.R, T_C1_C0.t, alpha=0) cams[0].setUndistortRectifyMap(R1, P1) cams[1].setUndistortRectifyMap(R2, P2) new_K = [P1[:3, :3], P2[:3, :3]] assert np.all(new_K[0] == new_K[1]) for i in [0, 1]: image_folder = os.path.join(root, 'cam%d' % i, 'data') out_folder = os.path.join(root, 'rect%d' % i) if not os.path.exists(out_folder): os.makedirs(out_folder) np.savetxt(os.path.join(out_folder, 'K.txt'), new_K[i]) image_folder_contents = sorted(os.listdir(image_folder)) in_images = [os.path.join(image_folder, j) for j in image_folder_contents if j.endswith('.png')] out_images = [os.path.join(out_folder, j) for j in image_folder_contents if j.endswith('.png')] for j in range(len(in_images)): print('%d/%d' % (j, len(in_images))) img = cv2.imread(in_images[j]) img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) undimg = cv2.remap( img, cams[i].map1, cams[i].map2, cv2.INTER_LINEAR) cv2.imwrite(out_images[j], undimg)
def split(root, tvt): all_folders = sorted(os.listdir(symlink(root))) # https://github.com/hpatches/hpatches-benchmark/blob/master/tasks/splits/splits.json test_folders = [ "v_courses", "v_coffeehouse", "v_abstract", "v_feast", "v_woman", "v_talent", "v_tabletop", "v_bees", "v_strand", "v_fest", "v_yard", "v_underground", "v_azzola", "v_eastsouth", "v_yuri", "v_soldiers", "v_man", "v_pomegranate", "v_birdwoman", "v_busstop" ] if tvt == 'testing': return [os.path.join(root, x) for x in test_folders] else: folders = [ os.path.join(root, x) for x in all_folders if x[0] == 'v' and x not in test_folders ] if tvt == 'validation': return folders[:5] else: assert tvt == 'training' return folders[5:]
def __init__(self, seq_i, light_i, half=True): half_full = 'half' if half else 'full' seq_path = os.path.join(symlink('dtu'), half_full, 'SET%03d' % seq_i) im_names = ['Img%03d_%02d.bmp' % (i, light_i) for i in range(1, 120)] im_paths = [os.path.join(seq_path, i) for i in im_names] calib = Calibration() base.RectifiedMonoSequence.__init__(self, im_paths, calib.getK(half), calib.getT_W_C(), 'dtu_%s' % half_full, '%03d_%02d' % (seq_i, light_i)) self.T_C_W = [i.inverse() for i in self.T_W_C] self.points = pointCloud(seq_i) point_ids_file = os.path.join(seq_path, 'point_ids.hkl') if not os.path.exists(point_ids_file): self.point_ids = [ self.calculateObservedPoints(i) for i in range(119) ] hkl.dump(self.point_ids, open(point_ids_file, 'w')) else: self.point_ids = hkl.load(open(point_ids_file, 'r'))
] Ks = [np.loadtxt(k_file) for k_file in k_files] T_W_C_file = os.path.join(seq_folder, 'T_W_C.txt') T_W_C_serialized = np.loadtxt(T_W_C_file) T_W_C = [ pose.fromMatrix(np.reshape(T_W_C_serialized[i, :], (4, 4))) for i in range(T_W_C_serialized.shape[0]) ] base.RectifiedStereoSequence.__init__(self, left_ims, right_ims, Ks[0], Ks[1], T_W_C, 0.24, 'rc', seq_id) # Load extra modules. sdk_dir = os.path.join(symlink('robotcar'), 'robotcar-dataset-sdk') sdk_py_dir = os.path.join(sdk_dir, 'python') cam_model_dir = os.path.join(sdk_dir, 'models') sys.path.append(sdk_py_dir) assert os.path.exists(sdk_py_dir) import camera_model import image import interpolate_poses import transform class ParallelImageConverter(object): def __init__(self, model, in_dir, out_dir, images): self._model = model self._in_dir = in_dir self._out_dir = out_dir
def pointCloud(seq_i): mat_path = os.path.join(symlink('dtu'), 'reconstructions', 'Clean_Reconstruction_%02d.mat' % seq_i) mat = scipy.io.loadmat(mat_path) return np.hstack((mat['pts3D_near'][:3, :], mat['pts3D_far'][:3, :]))
def __init__(self): mat_path = os.path.join(symlink('dtu'), 'calibrationFile.mat') self.mat = scipy.io.loadmat(mat_path)
def __init__(self, seq_id): seq_path = os.path.join(symlink('tum_mono'), 'sequence_%s' % seq_id) images = utils_path.imagesFromDir(os.path.join(seq_path, 'rect')) base.ImageSequence.__init__(self, images, 'tm', seq_id)