Skip to contents

Low-level implementation to sample a '3D' volume into a given orientation and shape using nearest-neighbor, trilinear, or cubic B-spline interpolation.

Usage

resample_3d_volume(
  x,
  new_dim,
  vox2ras_old,
  vox2ras_new = vox2ras_old,
  na_fill = NA,
  interpolation = c("nearest", "trilinear", "bspline")
)

Arguments

x

image (volume) to be sampled: dim(x) must have length of 3

new_dim

target dimension, integers of length 3

vox2ras_old

from volume index (column-row-slice) to 'RAS' (right-anterior-superior) transform: the volume index starts from 0 (C-style) instead of 1 (R-style) to comply with 'NIfTI' transform.

vox2ras_new

the targeting transform from volume index to 'RAS'

na_fill

default numbers to fill if a pixel is out of bound; default is NA or as.raw(0) if input x is raw type

interpolation

interpolation method: 'nearest' (default), 'trilinear', or 'bspline' (cubic Catmull-Rom). Trilinear and B-spline are only applied when x is a double volume; other storage types silently fall back to nearest-neighbor.

Value

A newly sampled volume that aligns with x in the anatomical 'RAS' coordinate system. The underlying storage mode is the same as x

Examples



# up-sample and rotate image
x <- array(0, c(9, 9, 9))
x[4:6, 4:6, 4:6] <- 1
vox2ras <- matrix(nrow = 4, byrow = TRUE, c(
  0.7071, -0.7071, 0, 0,
  0.7071, 0.7071, 0, -5.5,
  0, 0, 1, -4,
  0, 0, 0, 1
))

new_vox2ras <- matrix(nrow = 4, byrow = TRUE, c(
  0, 0.5, 0, -4,
  0, 0, -0.5, 4,
  0.5, 0, 0, -4,
  0, 0, 0, 1
))

y <- resample_3d_volume(
  x,
  c(17, 17, 17),
  vox2ras_old = vox2ras,
  vox2ras_new = new_vox2ras,
  na_fill = 0
)


image(y[9,,])