(cam_extrinsics, cam_intrinsics, images_folder)
| 104 | |
| 105 | |
| 106 | def readColmapCameras(cam_extrinsics, cam_intrinsics, images_folder): |
| 107 | cam_infos = [] |
| 108 | poses=[] |
| 109 | for idx, key in enumerate(cam_extrinsics): |
| 110 | sys.stdout.write('\r') |
| 111 | # the exact output you're looking for: |
| 112 | sys.stdout.write("Reading camera {}/{}".format(idx+1, len(cam_extrinsics))) |
| 113 | sys.stdout.flush() |
| 114 | |
| 115 | extr = cam_extrinsics[key] |
| 116 | intr = cam_intrinsics[extr.camera_id] |
| 117 | height = intr.height |
| 118 | width = intr.width |
| 119 | |
| 120 | uid = intr.id |
| 121 | R = np.transpose(qvec2rotmat(extr.qvec)) |
| 122 | T = np.array(extr.tvec) |
| 123 | pose = np.block([[R, T.reshape(3, 1)], [np.zeros((1, 3)), 1]]) |
| 124 | poses.append(pose) |
| 125 | |
| 126 | image_path = os.path.join(images_folder, os.path.basename(extr.name)) |
| 127 | image_name = os.path.basename(image_path).split(".")[0] |
| 128 | image = Image.open(image_path) |
| 129 | |
| 130 | if intr.model=="SIMPLE_PINHOLE": |
| 131 | focal_length_x = intr.params[0] |
| 132 | FovY = focal2fov(focal_length_x, height) |
| 133 | FovX = focal2fov(focal_length_x, width) |
| 134 | elif intr.model=="PINHOLE": |
| 135 | focal_length_x = intr.params[0] |
| 136 | focal_length_y = intr.params[1] |
| 137 | FovY = focal2fov(focal_length_y, height) |
| 138 | FovX = focal2fov(focal_length_x, width) |
| 139 | elif intr.model=="SIMPLE_RADIAL": |
| 140 | f, cx, cy, r = intr.params |
| 141 | FovY = focal2fov(f, height) |
| 142 | FovX = focal2fov(f, width) |
| 143 | prcppoint = np.array([cx / width, cy / height]) |
| 144 | # undistortion |
| 145 | image_cv = cv2.cvtColor(np.array(image), cv2.COLOR_RGB2BGR) |
| 146 | K = np.array([[f, 0, cx], [0, f, cy], [0, 0, 1]]) |
| 147 | D = np.array([r, 0, 0, 0]) # Only radial distortion |
| 148 | image_undistorted = cv2.undistort(image_cv, K, D, None) |
| 149 | image_undistorted = cv2.cvtColor(image_undistorted, cv2.COLOR_BGR2RGB) |
| 150 | image = Image.fromarray(image_undistorted) |
| 151 | else: |
| 152 | assert False, "Colmap camera model not handled: only undistorted datasets (PINHOLE or SIMPLE_PINHOLE cameras) supported!" |
| 153 | |
| 154 | cam_info = CameraInfo(uid=uid, R=R, T=T, FovY=FovY, FovX=FovX, image=image, |
| 155 | image_path=image_path, image_name=image_name, width=width, height=height) |
| 156 | cam_infos.append(cam_info) |
| 157 | sys.stdout.write('\n') |
| 158 | return cam_infos, poses |
| 159 | |
| 160 | |
| 161 |
no test coverage detected