SpatialResize
Overview
SpatialResize is the DeepLearning-internal pixel-center coordinate API used by all channel encoders and decoders when mapping between source_image_size / target_image_size and tensor (H, W).
Header: src/DeepLearning/spatial/SpatialResize.hpp
Tests: tests/DeepLearning/spatial/SpatialResize.test.cpp
This module is not linked outside DeepLearning. Mask raster resize for the rest of the application remains in mask_utils, which uses the same convention via map_dest_to_source.
Convention
Integer pixel indices are pixel centers at (i + 0.5) in continuous space (align_corners=false, PyTorch / OpenCV grid).
| Operation | Function | Formula (one axis) |
|---|---|---|
| Continuous image → tensor | continuous_image_to_tensor |
t = (x + 0.5) * T / I - 0.5 |
| Continuous tensor → image | continuous_tensor_to_image |
x = (t + 0.5) * I / T - 0.5 |
| Discrete mask encode | discrete_image_to_tensor |
lround(t_cont), clamped |
| Discrete mask decode | discrete_dest_to_source |
same as map_dest_to_source |
| Rasterize float geometry | discrete_tensor_nearest |
lround(t_cont), clamped |
Discrete indexing uses std::lround (half away from zero), not truncation.
Regression probe points
Canonical fixture: 640×480 image, 256×256 tensor, no crop.
| Image | Continuous tensor | Discrete tensor | Round-trip shift |
|---|---|---|---|
| (297, 161) | (118.50, 85.63) | (119, 86) | (0, 0) |
| (301, 178) | (120.10, 94.70) | (120, 95) | (0, 0) |
| (228, 230) | (90.90, 122.43) | (91, 122) | (0, 0) |
| (209, 245) | (83.30, 130.43) | (83, 130) | (0, 0) |
| (281, 188) | (112.10, 100.03) | (112, 100) | (0, 0) |
Before this refactor, decode and encode used mismatched grids; see the handoff doc for the prior behavior table.