pub fn resample_labels_onto<T: Copy + PartialEq + Default>(
labels: &[T],
src_dims: (usize, usize, usize),
src_affine: &[f64; 16],
dst_dims: (usize, usize, usize),
dst_affine: &[f64; 16],
) -> Result<Vec<T>, InputError>Expand description
Resample a label image (a segmentation, a parcellation) from one grid onto another.
Each destination voxel takes the label with the largest total trilinear weight among the
eight source voxels around it: equivalently, each label’s indicator is interpolated
linearly and the largest wins. Ties go to the nearest voxel’s label. Outside the source the
output is T::default() (0, background).
The output only ever holds labels that are in the input. Trilinear interpolation
(resample_onto) would invent values between label ids at every boundary; plain nearest
neighbour does not, but on a rotated grid it takes whichever single voxel the rounding
lands on, so boundaries come out staircased and a structure one or two voxels thick can be
dropped or doubled depending on where the grids happen to line up. The vote moves each
boundary to where the interpolated label fraction crosses one half, as a linearly
interpolated mask thresholded at 0.5 would, and costs the same eight lookups as trilinear
interpolation. On the identity mapping it returns the input unchanged.
Works for any label type: u8/u16/u32 ids, or integer-valued f64 as read from a
NIfTI.
§Errors
If labels is not src_dims in size, or the source affine is singular.