depth_fm.data.image_processing¶
image_processing ¶
Image-processing helpers used by the DepthFM HiRISE adapter.
Originally lived in depth_fm/data/adapter.py (1908 LOC, mostly these
helpers). Extracted into focused submodules so the adapter file itself stays
small and each algorithm is locatable.
Public API:
mask_ops—erode_valid_maskvoid_filling—fill_invalid_nearest_neighbor,fill_voids_kriging,fill_dtm_smart_diffusion,fill_voids_gmrfseam_detection—SeamResult,detect_seam_artifact,is_tin_artifact,compute_piecewise_linearity,compute_artifact_multipliers,compute_spatial_isolationsun_vector—estimate_sun_vector_ols,estimate_sun_vector_irlsterrain—compute_topographic_residual
SeamResult
dataclass
¶
SeamResult(seam_score: float, ortho_score: float, dtm_score: float, cohens_d: float, best_angle_rad: float, best_y: int, best_x: int, line_length: int, num_angles: int, span: float, sparsity: float, composite_score: float, is_seam: bool, seam_heatmap: Optional[ndarray] = None, cohens_d_heatmap: Optional[ndarray] = None, per_angle_max: Optional[ndarray] = None, diag_hot_mask: Optional[ndarray] = None, diag_closed_components: Optional[ndarray] = None, diag_hough_lines: Optional[ndarray] = None, diag_isolation_profile: Optional[tuple] = None)
line_endpoints ¶
Return (y1, x1, y2, x2) pixel endpoints of the best-scoring line.
Source code in src/depth_fm/data/image_processing/seam_detection.py
erode_valid_mask ¶
Erode a binary mask to trim noisy boundary pixels.
Source code in src/depth_fm/data/image_processing/mask_ops.py
fill_invalid_nearest_neighbor ¶
Fill invalid regions using nearest-neighbor propagation via EDT.
Supports (H, W), (C, H, W), and (B, C, H, W) tensors.
Source code in src/depth_fm/data/image_processing/void_filling.py
fill_voids_kriging ¶
fill_voids_kriging(image: Tensor, dtm: Tensor, valid_mask: Tensor, *, erode_radius: int = 2, max_training_points: int = 2500, variogram_model: str = 'linear', seed: int = 42) -> tuple[torch.Tensor, torch.Tensor]
Fill voids in an orthoimage and DTM with a single kriging pass per channel.
Optimised for 512x512 tiles: subsamples valid pixels for training, then predicts every void pixel at once. No iteration, no per-void labelling, no diffusion loop.
Source code in src/depth_fm/data/image_processing/void_filling.py
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 | |
fill_dtm_smart_diffusion ¶
fill_dtm_smart_diffusion(tensor: Tensor, valid_mask: Tensor, iterations: int = 64, erode_radius: int = 2) -> torch.Tensor
Fill invalid regions using Laplacian diffusion (heat equation).
Source code in src/depth_fm/data/image_processing/void_filling.py
fill_voids_gmrf ¶
fill_voids_gmrf(image: Tensor, dtm: Tensor, valid_mask: Tensor, *, erode_radius: int = 2, connectivity: int = 4, tau: float = 1.0, nugget: float = 1e-06) -> tuple[torch.Tensor, torch.Tensor, torch.Tensor]
Fill voids in an orthoimage and DTM using GMRF conditional distribution.
One sparse linear solve per channel. No iteration. O(n^{3/2}) on 2D grids.
Returns (filled_image, filled_dtm, eroded_mask). The eroded mask records which pixels were considered trustworthy (1) vs infilled (0) — all pixels are valid in the filled outputs.
Source code in src/depth_fm/data/image_processing/void_filling.py
299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 | |
detect_seam_artifact ¶
detect_seam_artifact(ortho: Tensor, elevation: Tensor, valid_mask: Tensor, line_length: int = 41, num_angles: int = 12, side_offset: int = 2, min_valid_ratio: float = 0.6, ortho_weight: float = 1.0, dtm_weight: float = 0.3, erosion_kernel: int = 9, seam_threshold: float = 2.4, return_diagnostics: bool = False) -> SeamResult
Detect seam artifacts (mosaicking discontinuities) in a HiRISE patch.
Returns a SeamResult. With return_diagnostics=True, the result also
carries seam_heatmap, cohens_d_heatmap, and per_angle_max, used by
the visualization and refinement UI to show where and along which
angle the detector fired.
Source code in src/depth_fm/data/image_processing/seam_detection.py
330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 | |
is_tin_artifact ¶
Scale-invariant TIN-artifact detection using localized maximum density.
Returns the maximum local fraction of zero-curvature pixels within any
kernel_size × kernel_size window. Patches with stretched-triangle TIN
artifacts produce values near 1.0; natural terrain produces values <0.5.
Source code in src/depth_fm/data/image_processing/seam_detection.py
compute_piecewise_linearity ¶
compute_piecewise_linearity(seam_heatmap: ndarray, threshold_ratio: float = 0.3) -> tuple[float, np.ndarray]
Score how piecewise-linear the high-scoring pixels are (handles corners).
Returns (linearity_score, line_mask) where linearity is in [0.0, 1.0]: 1.0 = highly structured/linear, 0.0 = curved/messy.
Source code in src/depth_fm/data/image_processing/seam_detection.py
compute_artifact_multipliers ¶
compute_artifact_multipliers(seam_heatmap: ndarray, valid_mask: ndarray, threshold: float = 0.2) -> tuple[float, float, np.ndarray, np.ndarray]
Compute structural multipliers that distinguish seams from natural features.
Returns (span_ratio, sparsity, hot_mask, labels): * span_ratio — defeats short craters; true seams cross the whole tile. * sparsity — defeats dense dunes; true seams are a singular line.
Source code in src/depth_fm/data/image_processing/seam_detection.py
compute_spatial_isolation ¶
compute_spatial_isolation(score_map: Tensor, valid_mask: Tensor, best_x: int, best_y: int, angle_rad: float, profile_length: int = 50, exclusion_zone: int = 12) -> tuple[float, Optional[tuple]]
Sample a perpendicular slice across the seam.
Returns the ratio of the central peak to the surrounding parallel background, plus the raw profile data arrays for visualization.
Source code in src/depth_fm/data/image_processing/seam_detection.py
estimate_sun_vector_ols ¶
estimate_sun_vector_ols(dtm: Tensor, ortho: Tensor, valid_mask: Tensor) -> tuple[torch.Tensor, torch.Tensor, torch.Tensor]
Estimates the sun vector [sx, sy, sz] using Ordinary Least Squares.
Handles shapes (C, H, W) or (1, C, H, W). Batch size must be 1.
Source code in src/depth_fm/data/image_processing/sun_vector.py
estimate_sun_vector_irls ¶
estimate_sun_vector_irls(dtm: Tensor, ortho: Tensor, valid_mask: Tensor, max_iter: int = 15, tol: float = 0.0001) -> tuple[torch.Tensor, torch.Tensor, torch.Tensor]
Decoupled IRLS sun-vector estimator.
Separates ambient light estimation from the linear system to prevent the Nz / bias collinearity trap from inverting the sun vector. Returns (sun_vec, intensity, ambient).
Source code in src/depth_fm/data/image_processing/sun_vector.py
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 | |
compute_topographic_residual ¶
Fit a 2D plane to elevation and return the RMS residual.
Removes macroscopic slopes so the returned scalar isolates true topographic roughness (used as a manifest filter to reject overly-flat patches).