Drops a dimension of an image that has one-dimension and sets respective values to 0 in pixdim or 1 in dim.

dropImageDimension(img, onlylast = TRUE, warn = TRUE)

drop_img_dim(img, onlylast = TRUE, warn = TRUE)

Arguments

img

nifti object

onlylast

is a logical variable (default = TRUE). Drop the dimension only if it is the last dimension. For example, if dim is 10x10x1x10 then no dimension is dropped, but if dim is 10x10x10x1 then it will be changed to 10x10x10.

warn

produces a text output if the number of dimensions is under three.

Value

Object of class nifti

Examples


nim <- nifti(array(rnorm(10^3), dim = rep(10, 3)))
nim2 <- nifti(array(rnorm(10^3), dim = c(10, 10, 1, 10)))
dropImageDimension(nim2)
#> NIfTI-1 format
#>   Type            : nifti
#>   Data Type       : 2 (UINT8)
#>   Bits per Pixel  : 8
#>   Slice Code      : 0 (Unknown)
#>   Intent Code     : 0 (None)
#>   Qform Code      : 0 (Unknown)
#>   Sform Code      : 0 (Unknown)
#>   Dimension       : 10 x 10 x 1 x 10
#>   Pixel Dimension : 1 x 1 x 1 x 1
#>   Voxel Units     : Unknown
#>   Time Units      : Unknown
dropImageDimension(nim2, onlylast = FALSE)
#> NIfTI-1 format
#>   Type            : nifti
#>   Data Type       : 2 (UINT8)
#>   Bits per Pixel  : 8
#>   Slice Code      : 0 (Unknown)
#>   Intent Code     : 0 (None)
#>   Qform Code      : 0 (Unknown)
#>   Sform Code      : 0 (Unknown)
#>   Dimension       : 10 x 10 x 10
#>   Pixel Dimension : 1 x 1 x 1
#>   Voxel Units     : Unknown
#>   Time Units      : Unknown
nim3 <- nifti(array(rnorm(10^3), dim = c(10, 10, 10, 1)))
dropImageDimension(nim3)
#> NIfTI-1 format
#>   Type            : nifti
#>   Data Type       : 2 (UINT8)
#>   Bits per Pixel  : 8
#>   Slice Code      : 0 (Unknown)
#>   Intent Code     : 0 (None)
#>   Qform Code      : 0 (Unknown)
#>   Sform Code      : 0 (Unknown)
#>   Dimension       : 10 x 10 x 10
#>   Pixel Dimension : 1 x 1 x 1
#>   Voxel Units     : Unknown
#>   Time Units      : Unknown
dropImageDimension(nim3, onlylast = FALSE) # the same as above
#> NIfTI-1 format
#>   Type            : nifti
#>   Data Type       : 2 (UINT8)
#>   Bits per Pixel  : 8
#>   Slice Code      : 0 (Unknown)
#>   Intent Code     : 0 (None)
#>   Qform Code      : 0 (Unknown)
#>   Sform Code      : 0 (Unknown)
#>   Dimension       : 10 x 10 x 10
#>   Pixel Dimension : 1 x 1 x 1
#>   Voxel Units     : Unknown
#>   Time Units      : Unknown
nim4 <- nifti(array(rnorm(10^3), dim = c(10, 10, 10, 1, 10)))
dim(nim4[,,,1,])
#> [1] 10 10 10 10
dim(nim4[,,,1,,drop=TRUE])
#> [1] 10 10 10 10
dropImageDimension(nim4)
#> NIfTI-1 format
#>   Type            : nifti
#>   Data Type       : 2 (UINT8)
#>   Bits per Pixel  : 8
#>   Slice Code      : 0 (Unknown)
#>   Intent Code     : 0 (None)
#>   Qform Code      : 0 (Unknown)
#>   Sform Code      : 0 (Unknown)
#>   Dimension       : 10 x 10 x 10 x 1 x 10
#>   Pixel Dimension : 1 x 1 x 1 x 1 x 1
#>   Voxel Units     : Unknown
#>   Time Units      : Unknown

nim5 <- nifti(array(rnorm(10^4), dim = c(1, 10, 10, 10, 1, 10)))
dropImageDimension(nim5)
#> NIfTI-1 format
#>   Type            : nifti
#>   Data Type       : 2 (UINT8)
#>   Bits per Pixel  : 8
#>   Slice Code      : 0 (Unknown)
#>   Intent Code     : 0 (None)
#>   Qform Code      : 0 (Unknown)
#>   Sform Code      : 0 (Unknown)
#>   Dimension       : 1 x 10 x 10 x 10 x 1 x 10
#>   Pixel Dimension : 1 x 1 x 1 x 1 x 1 x 1
#>   Voxel Units     : Unknown
#>   Time Units      : Unknown
dropImageDimension(nim5, onlylast = FALSE)
#> NIfTI-1 format
#>   Type            : nifti
#>   Data Type       : 2 (UINT8)
#>   Bits per Pixel  : 8
#>   Slice Code      : 0 (Unknown)
#>   Intent Code     : 0 (None)
#>   Qform Code      : 0 (Unknown)
#>   Sform Code      : 0 (Unknown)
#>   Dimension       : 10 x 10 x 10 x 10
#>   Pixel Dimension : 1 x 1 x 1 x 1
#>   Voxel Units     : Unknown
#>   Time Units      : Unknown

nim6 <- nifti(array(rnorm(10^3), dim = c(1, 10, 10, 10, 1, 1)))
dropImageDimension(nim6)
#> NIfTI-1 format
#>   Type            : nifti
#>   Data Type       : 2 (UINT8)
#>   Bits per Pixel  : 8
#>   Slice Code      : 0 (Unknown)
#>   Intent Code     : 0 (None)
#>   Qform Code      : 0 (Unknown)
#>   Sform Code      : 0 (Unknown)
#>   Dimension       : 1 x 10 x 10 x 10
#>   Pixel Dimension : 1 x 1 x 1 x 1
#>   Voxel Units     : Unknown
#>   Time Units      : Unknown
if (FALSE) {
## 27 scans of Colin Holmes (MNI) brain co-registered and averaged
## NIfTI two-file format
URL <- "http://imaging.mrc-cbu.cam.ac.uk/downloads/Colin/colin_1mm.tgz"
urlfile <- file.path(tempdir(), "colin_1mm.tgz")
download.file(URL, dest=urlfile, quiet=TRUE)
untar(urlfile, exdir=tempdir())
colin <- readNIfTI(file.path(tempdir(), "colin_1mm"))
dim(colin)
dim_(colin)
pixdim(colin)
# this will error
writeNIfTI(colin, filename = tempfile())
colin <- dropImageDimension(colin)
writeNIfTI(colin, filename = tempfile())
}