| 1 |
import numpy as np |
| 2 |
|
| 3 |
import pycolmap |
| 4 |
|
| 5 |
|
| 6 |
def test_camera_model_id_invalid(): |
| 7 |
assert pycolmap.CameraModelId.INVALID is not None |
| 8 |
|
| 9 |
|
| 10 |
def test_camera_model_id_pinhole(): |
| 11 |
assert pycolmap.CameraModelId.PINHOLE is not None |
| 12 |
|
| 13 |
|
| 14 |
def test_camera_model_id_simple_pinhole(): |
| 15 |
assert pycolmap.CameraModelId.SIMPLE_PINHOLE is not None |
| 16 |
|
| 17 |
|
| 18 |
def test_camera_model_id_simple_radial(): |
| 19 |
assert pycolmap.CameraModelId.SIMPLE_RADIAL is not None |
| 20 |
|
| 21 |
|
| 22 |
def test_camera_model_id_radial(): |
| 23 |
assert pycolmap.CameraModelId.RADIAL is not None |
| 24 |
|
| 25 |
|
| 26 |
def test_camera_model_id_opencv(): |
| 27 |
assert pycolmap.CameraModelId.OPENCV is not None |
| 28 |
|
| 29 |
|
| 30 |
def test_camera_model_id_string_construction(): |
| 31 |
model = pycolmap.CameraModelId("PINHOLE") |
| 32 |
assert model == pycolmap.CameraModelId.PINHOLE |
| 33 |
|
| 34 |
|
| 35 |
def test_camera_default_init(): |
| 36 |
camera = pycolmap.Camera() |
| 37 |
assert camera is not None |
| 38 |
|
| 39 |
|
| 40 |
def test_camera_create_from_model_id(): |
| 41 |
camera = pycolmap.Camera.create_from_model_id( |
| 42 |
1, pycolmap.CameraModelId.PINHOLE, 500.0, 1024, 768 |
| 43 |
) |
| 44 |
assert camera is not None |
| 45 |
assert camera.camera_id == 1 |
| 46 |
|
| 47 |
|
| 48 |
def test_camera_create_from_model_name(): |
| 49 |
camera = pycolmap.Camera.create_from_model_name( |
| 50 |
2, "PINHOLE", 500.0, 1024, 768 |
| 51 |
) |
| 52 |
assert camera is not None |
| 53 |
assert camera.camera_id == 2 |
| 54 |
|
| 55 |
|
| 56 |
def test_camera_camera_id_readwrite(simple_camera): |
| 57 |
simple_camera.camera_id = 10 |
| 58 |
assert simple_camera.camera_id == 10 |
| 59 |
|
| 60 |
|
| 61 |
def test_camera_model_readwrite(simple_camera): |
| 62 |
assert simple_camera.model == pycolmap.CameraModelId.PINHOLE |
| 63 |
simple_camera.model = pycolmap.CameraModelId.SIMPLE_PINHOLE |
| 64 |
assert simple_camera.model == pycolmap.CameraModelId.SIMPLE_PINHOLE |
| 65 |
|
| 66 |
|
| 67 |
def test_camera_width_height_readwrite(simple_camera): |
| 68 |
assert simple_camera.width == 1024 |
| 69 |
assert simple_camera.height == 768 |
| 70 |
simple_camera.width = 2048 |
| 71 |
simple_camera.height = 1536 |
| 72 |
assert simple_camera.width == 2048 |
| 73 |
assert simple_camera.height == 1536 |
| 74 |
|
| 75 |
|
| 76 |
def test_camera_params_readwrite(simple_camera): |
| 77 |
params = simple_camera.params |
| 78 |
assert len(params) > 0 |
| 79 |
new_params = list(params) |
| 80 |
new_params[0] = 600.0 |
| 81 |
simple_camera.params = new_params |
| 82 |
assert simple_camera.params[0] == 600.0 |
| 83 |
|
| 84 |
|
| 85 |
def test_camera_has_prior_focal_length_readwrite(simple_camera): |
| 86 |
simple_camera.has_prior_focal_length = True |
| 87 |
assert simple_camera.has_prior_focal_length is True |
| 88 |
simple_camera.has_prior_focal_length = False |
| 89 |
assert simple_camera.has_prior_focal_length is False |
| 90 |
|
| 91 |
|
| 92 |
def test_camera_focal_length_readwrite(): |
| 93 |
camera = pycolmap.Camera.create_from_model_id( |
| 94 |
1, pycolmap.CameraModelId.SIMPLE_PINHOLE, 500.0, 1024, 768 |
| 95 |
) |
| 96 |
camera.focal_length = 600.0 |
| 97 |
assert camera.focal_length == 600.0 |
| 98 |
|
| 99 |
|
| 100 |
def test_camera_focal_length_x_y(simple_camera): |
| 101 |
assert isinstance(simple_camera.focal_length_x, float) |
| 102 |
assert isinstance(simple_camera.focal_length_y, float) |
| 103 |
|
| 104 |
|
| 105 |
def test_camera_principal_point_x_y(simple_camera): |
| 106 |
assert isinstance(simple_camera.principal_point_x, float) |
| 107 |
assert isinstance(simple_camera.principal_point_y, float) |
| 108 |
|
| 109 |
|
| 110 |
def test_camera_sensor_id_readonly(simple_camera): |
| 111 |
sensor_id = simple_camera.sensor_id |
| 112 |
assert sensor_id is not None |
| 113 |
|
| 114 |
|
| 115 |
def test_camera_model_name_readonly(simple_camera): |
| 116 |
assert simple_camera.model_name == "PINHOLE" |
| 117 |
|
| 118 |
|
| 119 |
def test_camera_params_info_readonly(simple_camera): |
| 120 |
info = simple_camera.params_info |
| 121 |
assert isinstance(info, str) |
| 122 |
|
| 123 |
|
| 124 |
def test_camera_mean_focal_length(simple_camera): |
| 125 |
mean_focal = simple_camera.mean_focal_length() |
| 126 |
assert isinstance(mean_focal, float) |
| 127 |
|
| 128 |
|
| 129 |
def test_camera_focal_length_idxs(simple_camera): |
| 130 |
idxs = simple_camera.focal_length_idxs() |
| 131 |
assert len(idxs) > 0 |
| 132 |
|
| 133 |
|
| 134 |
def test_camera_principal_point_idxs(simple_camera): |
| 135 |
idxs = simple_camera.principal_point_idxs() |
| 136 |
assert len(idxs) > 0 |
| 137 |
|
| 138 |
|
| 139 |
def test_camera_extra_params_idxs(simple_camera): |
| 140 |
idxs = simple_camera.extra_params_idxs() |
| 141 |
assert isinstance(idxs, list) |
| 142 |
|
| 143 |
|
| 144 |
def test_camera_metadata_params_idxs(simple_camera): |
| 145 |
# Perspective models have no metadata parameters. |
| 146 |
assert simple_camera.metadata_params_idxs() == [] |
| 147 |
# Spherical models carry their (w, h) image dimensions as metadata. |
| 148 |
spherical_camera = pycolmap.Camera.create_from_model_name( |
| 149 |
3, "EQUIRECTANGULAR", 0.0, 1024, 512 |
| 150 |
) |
| 151 |
assert spherical_camera.metadata_params_idxs() == [0, 1] |
| 152 |
|
| 153 |
|
| 154 |
def test_camera_calibration_matrix(simple_camera): |
| 155 |
matrix = simple_camera.calibration_matrix() |
| 156 |
assert matrix.shape == (3, 3) |
| 157 |
|
| 158 |
|
| 159 |
def test_camera_cam_from_img_point2d(simple_camera): |
| 160 |
point2d = np.array([512.0, 384.0]) |
| 161 |
result = simple_camera.cam_from_img(point2d) |
| 162 |
assert result is not None |
| 163 |
|
| 164 |
|
| 165 |
def test_camera_cam_from_img_matrix(simple_camera): |
| 166 |
points = np.array([[512.0, 384.0], [100.0, 200.0]]) |
| 167 |
result = simple_camera.cam_from_img(points) |
| 168 |
assert len(result) == 2 |
| 169 |
|
| 170 |
|
| 171 |
def test_camera_cam_ray_from_img_point2d(simple_camera): |
| 172 |
point2d = np.array([512.0, 384.0]) |
| 173 |
result = simple_camera.cam_ray_from_img(point2d) |
| 174 |
assert result is not None |
| 175 |
assert result.shape == (3,) |
| 176 |
assert np.isclose(np.linalg.norm(result), 1.0) |
| 177 |
|
| 178 |
|
| 179 |
def test_camera_cam_ray_from_img_matrix(simple_camera): |
| 180 |
points = np.array([[512.0, 384.0], [100.0, 200.0]]) |
| 181 |
result = simple_camera.cam_ray_from_img(points) |
| 182 |
assert len(result) == 2 |
| 183 |
for ray in result: |
| 184 |
assert np.isclose(np.linalg.norm(ray), 1.0) |
| 185 |
|
| 186 |
|
| 187 |
def test_camera_img_from_cam_point3d(simple_camera): |
| 188 |
point3d = np.array([0.0, 0.0, 1.0]) |
| 189 |
result = simple_camera.img_from_cam(point3d) |
| 190 |
assert result is not None |
| 191 |
|
| 192 |
|
| 193 |
def test_camera_img_from_cam_matrix(simple_camera): |
| 194 |
points = np.array([[0.0, 0.0, 1.0], [0.1, 0.2, 1.0]]) |
| 195 |
result = simple_camera.img_from_cam(points) |
| 196 |
assert len(result) == 2 |
| 197 |
|
| 198 |
|
| 199 |
def test_camera_cam_from_img_threshold(simple_camera): |
| 200 |
threshold = simple_camera.cam_from_img_threshold(1.0) |
| 201 |
assert isinstance(threshold, float) |
| 202 |
|
| 203 |
|
| 204 |
def test_camera_verify_params(simple_camera): |
| 205 |
assert simple_camera.verify_params() |
| 206 |
|
| 207 |
|
| 208 |
def test_camera_is_undistorted(simple_camera): |
| 209 |
result = simple_camera.is_undistorted() |
| 210 |
assert isinstance(result, bool) |
| 211 |
|
| 212 |
|
| 213 |
def test_camera_is_perspective(simple_camera): |
| 214 |
assert simple_camera.is_perspective() is True |
| 215 |
spherical_camera = pycolmap.Camera.create_from_model_name( |
| 216 |
3, "EQUIRECTANGULAR", 0.0, 1024, 512 |
| 217 |
) |
| 218 |
assert spherical_camera.is_perspective() is False |
| 219 |
|
| 220 |
|
| 221 |
def test_camera_is_spherical(simple_camera): |
| 222 |
assert simple_camera.is_spherical() is False |
| 223 |
spherical_camera = pycolmap.Camera.create_from_model_name( |
| 224 |
3, "EQUIRECTANGULAR", 0.0, 1024, 512 |
| 225 |
) |
| 226 |
assert spherical_camera.is_spherical() is True |
| 227 |
|
| 228 |
|
| 229 |
def test_camera_is_perspective_pinhole(simple_camera): |
| 230 |
assert simple_camera.is_perspective_pinhole() is True |
| 231 |
fisheye_camera = pycolmap.Camera.create_from_model_name( |
| 232 |
3, "OPENCV_FISHEYE", 1.0, 1024, 512 |
| 233 |
) |
| 234 |
assert fisheye_camera.is_perspective() is True |
| 235 |
assert fisheye_camera.is_perspective_fisheye() is True |
| 236 |
assert fisheye_camera.is_perspective_pinhole() is False |
| 237 |
spherical_camera = pycolmap.Camera.create_from_model_name( |
| 238 |
4, "EQUIRECTANGULAR", 0.0, 1024, 512 |
| 239 |
) |
| 240 |
assert spherical_camera.is_perspective_pinhole() is False |
| 241 |
|
| 242 |
|
| 243 |
def test_camera_has_bogus_params(simple_camera): |
| 244 |
result = simple_camera.has_bogus_params(0.1, 100.0, 1.0) |
| 245 |
assert isinstance(result, bool) |
| 246 |
|
| 247 |
|
| 248 |
def test_camera_rescale_factor(simple_camera): |
| 249 |
simple_camera.rescale(2.0) |
| 250 |
assert simple_camera.width == 2048 |
| 251 |
assert simple_camera.height == 1536 |
| 252 |
|
| 253 |
|
| 254 |
def test_camera_rescale_dimensions(simple_camera): |
| 255 |
simple_camera.rescale(512, 384) |
| 256 |
assert simple_camera.width == 512 |
| 257 |
assert simple_camera.height == 384 |
| 258 |
|
| 259 |
|
| 260 |
def test_camera_params_to_string(simple_camera): |
| 261 |
params_string = simple_camera.params_to_string() |
| 262 |
assert isinstance(params_string, str) |
| 263 |
assert len(params_string) > 0 |
| 264 |
|
| 265 |
|
| 266 |
def test_camera_set_params_from_string(simple_camera): |
| 267 |
params_string = simple_camera.params_to_string() |
| 268 |
simple_camera.set_params_from_string(params_string) |
| 269 |
assert simple_camera.verify_params() |
| 270 |
|
| 271 |
|
| 272 |
def test_camera_map_empty(): |
| 273 |
camera_map = pycolmap.CameraMap() |
| 274 |
assert len(camera_map) == 0 |
| 275 |
|
| 276 |
|
| 277 |
def test_camera_map_insert_and_access(simple_camera): |
| 278 |
camera_map = pycolmap.CameraMap() |
| 279 |
camera_map[1] = simple_camera |
| 280 |
assert len(camera_map) == 1 |
| 281 |
assert camera_map[1].camera_id == 1 |