ENH: Resampling Image
muschellij2 opened this issue · 4 comments
muschellij2 commented
So I think it should be straightforward to simply resample an image into a new voxel size using RNiftyReg
, right? I tried my hand in this, but it seemed to fail:
library(RNifti)
library(RNiftyReg)
#>
#> Attaching package: 'RNiftyReg'
#> The following object is masked from 'package:RNifti':
#>
#> readNifti
fname = system.file("extdata", "epi_t2.nii.gz", package="RNiftyReg")
source <- readNifti(fname)
hdr = niftiHeader(source)
hdr
#> NIfTI-1 header
#> sizeof_hdr: 348
#> dim_info: 0
#> dim: 3 96 96 60 1 1 1 1
#> intent_p1: 0
#> intent_p2: 0
#> intent_p3: 0
#> intent_code: 0 (Unknown)
#> datatype: 64 (FLOAT64)
#> bitpix: 64
#> slice_start: 0
#> pixdim: -1.0 2.5 2.5 2.5 0.0 0.0 0.0 0.0
#> vox_offset: 352
#> scl_slope: 0
#> scl_inter: 0
#> slice_end: 0
#> slice_code: 0 (Unknown)
#> xyzt_units: 10
#> cal_max: 2503
#> cal_min: 0
#> slice_duration: 0
#> toffset: 0
#> descrip: FSL5.0
#> aux_file:
#> qform_code: 2 (Aligned Anat)
#> sform_code: 2 (Aligned Anat)
#> quatern_b: 0
#> quatern_c: 1
#> quatern_d: 0
#> qoffset_x: 122.0339
#> qoffset_y: -95.18523
#> qoffset_z: -55.03814
#> srow_x: -2.5000 0.0000 0.0000 122.0339
#> srow_y: 0.00000 2.50000 0.00000 -95.18523
#> srow_z: 0.00000 0.00000 2.50000 -55.03814
#> intent_name:
#> magic: n+1
pdim = hdr$pixdim[2:4]
hdr$pixdim[2:4] = 1
hdr$dim[2:4] = pdim * hdr$dim[2:4]
hdr$srow_x[1:3] = hdr$srow_x[1:3] / pdim[1]
hdr$srow_y[1:3] = hdr$srow_y[1:3] / pdim[2]
hdr$srow_z[1:3] = hdr$srow_z[1:3] / pdim[3]
arr = array(0, dim = hdr$dim[2:4])
target = asNifti(arr, reference = hdr)
target
#> Image array of mode "double" (65.9 Mb)
#> - 240 x 240 x 150 voxels
#> - 1 x 1 x 1 mm per voxel
niftiHeader(target)
#> NIfTI-1 header
#> sizeof_hdr: 348
#> dim_info: 0
#> dim: 3 240 240 150 1 1 1 1
#> intent_p1: 0
#> intent_p2: 0
#> intent_p3: 0
#> intent_code: 0 (Unknown)
#> datatype: 64 (FLOAT64)
#> bitpix: 64
#> slice_start: 0
#> pixdim: -1 1 1 1 0 0 0 0
#> vox_offset: 352
#> scl_slope: 0
#> scl_inter: 0
#> slice_end: 0
#> slice_code: 0 (Unknown)
#> xyzt_units: 10
#> cal_max: 2503
#> cal_min: 0
#> slice_duration: 0
#> toffset: 0
#> descrip: FSL5.0
#> aux_file:
#> qform_code: 2 (Aligned Anat)
#> sform_code: 2 (Aligned Anat)
#> quatern_b: 0
#> quatern_c: 1
#> quatern_d: 0
#> qoffset_x: 122.0339
#> qoffset_y: -95.18523
#> qoffset_z: -55.03814
#> srow_x: -1.0000 0.0000 0.0000 122.0339
#> srow_y: 0.00000 1.00000 0.00000 -95.18523
#> srow_z: 0.00000 0.00000 1.00000 -55.03814
#> intent_name:
#> magic: n+1
affine <- buildAffine(source = source, target = target)
affine
#> NiftyReg affine matrix:
#> 1 0 0 0
#> 0 1 0 0
#> 0 0 1 0
#> 0 0 0 1
#> Source origin: (49.81, 39.07, 23.02)
#> Target origin: (123.03, 96.19, 56.04)
out = RNiftyReg::niftyreg.linear(source, target,
scope = "affine",
init = affine, nLevels = 0)
#> Error in RNiftyReg::niftyreg.linear(source, target, scope = "affine", : Source image should have 2, 3 or 4 dimensions
Created on 2020-09-16 by the reprex package (v0.3.0)
Session info
devtools::session_info()
#> ─ Session info ───────────────────────────────────────────────────────────────
#> setting value
#> version R version 4.0.2 (2020-06-22)
#> os macOS Catalina 10.15.6
#> system x86_64, darwin17.0
#> ui X11
#> language (EN)
#> collate en_US.UTF-8
#> ctype en_US.UTF-8
#> tz America/New_York
#> date 2020-09-16
#>
#> ─ Packages ───────────────────────────────────────────────────────────────────
#> package * version date lib source
#> assertthat 0.2.1 2019-03-21 [2] CRAN (R 4.0.0)
#> backports 1.1.9 2020-08-24 [2] CRAN (R 4.0.0)
#> callr 3.4.4 2020-09-07 [1] CRAN (R 4.0.2)
#> cli 2.0.2 2020-02-28 [2] CRAN (R 4.0.0)
#> crayon 1.3.4 2017-09-16 [2] CRAN (R 4.0.0)
#> desc 1.2.0 2020-06-01 [2] Github (muschellij2/desc@b0c374f)
#> devtools 2.3.1.9000 2020-08-25 [2] Github (r-lib/devtools@df619ce)
#> digest 0.6.25 2020-02-23 [2] CRAN (R 4.0.0)
#> ellipsis 0.3.1 2020-05-15 [2] CRAN (R 4.0.0)
#> evaluate 0.14 2019-05-28 [2] CRAN (R 4.0.0)
#> fansi 0.4.1 2020-01-08 [2] CRAN (R 4.0.0)
#> fs 1.5.0 2020-07-31 [2] CRAN (R 4.0.2)
#> glue 1.4.2 2020-08-27 [1] CRAN (R 4.0.2)
#> highr 0.8 2019-03-20 [2] CRAN (R 4.0.0)
#> htmltools 0.5.0 2020-06-16 [2] CRAN (R 4.0.0)
#> knitr 1.29 2020-06-23 [2] CRAN (R 4.0.2)
#> lifecycle 0.2.0 2020-03-06 [2] CRAN (R 4.0.0)
#> magrittr 1.5 2014-11-22 [2] CRAN (R 4.0.0)
#> memoise 1.1.0 2017-04-21 [2] CRAN (R 4.0.0)
#> ore 1.6.3 2019-11-02 [2] CRAN (R 4.0.0)
#> pkgbuild 1.1.0 2020-07-13 [2] CRAN (R 4.0.2)
#> pkgload 1.1.0 2020-05-29 [2] CRAN (R 4.0.0)
#> prettyunits 1.1.1 2020-01-24 [2] CRAN (R 4.0.0)
#> processx 3.4.4 2020-09-03 [1] CRAN (R 4.0.2)
#> ps 1.3.4 2020-08-11 [2] CRAN (R 4.0.2)
#> purrr 0.3.4 2020-04-17 [2] CRAN (R 4.0.0)
#> R6 2.4.1 2019-11-12 [2] CRAN (R 4.0.0)
#> Rcpp 1.0.5 2020-07-06 [2] CRAN (R 4.0.0)
#> remotes 2.2.0 2020-07-21 [2] CRAN (R 4.0.2)
#> rlang 0.4.7.9000 2020-09-09 [1] Github (r-lib/rlang@60c0151)
#> rmarkdown 2.3 2020-06-18 [2] CRAN (R 4.0.0)
#> RNifti * 1.2.0 2020-08-25 [2] CRAN (R 4.0.2)
#> RNiftyReg * 2.6.8 2020-04-01 [2] CRAN (R 4.0.0)
#> rprojroot 1.3-2 2018-01-03 [2] CRAN (R 4.0.0)
#> sessioninfo 1.1.1 2018-11-05 [2] CRAN (R 4.0.0)
#> stringi 1.5.3 2020-09-09 [1] CRAN (R 4.0.2)
#> stringr 1.4.0 2019-02-10 [2] CRAN (R 4.0.0)
#> testthat 2.99.0.9000 2020-08-25 [2] Github (r-lib/testthat@6a24275)
#> usethis 1.6.1.9001 2020-08-25 [2] Github (r-lib/usethis@860c1ea)
#> withr 2.2.0 2020-04-20 [2] CRAN (R 4.0.0)
#> xfun 0.17 2020-09-09 [1] CRAN (R 4.0.2)
#> yaml 2.2.1 2020-02-01 [2] CRAN (R 4.0.0)
#>
#> [1] /Users/johnmuschelli/Library/R/4.0/library
#> [2] /Library/Frameworks/R.framework/Versions/4.0/Resources/library
jonclayden commented
Hi John. I think the rescale()
function should do what you want, viz.
library(RNiftyReg)
source <- readNifti(system.file("extdata", "epi_t2.nii.gz", package="RNiftyReg"))
source
# Image array of mode "double" (4.2 Mb)
# - 96 x 96 x 60 voxels
# - 2.5 x 2.5 x 2.5 mm per voxel
result <- rescale(source, c(2.5,2.5,2.5))
result
# Image array of mode "double" (65.9 Mb)
# - 240 x 240 x 150 voxels
# - 1 x 1 x 1 mm per voxel
As for the "Source image should have 2, 3 or 4 dimensions" error, that's probably due to the slightly complicated rollout of RNifti
v1.2.x. I'd suggest updating RNifti
and RNiftyReg
to their latest versions before retrying.
jonclayden commented
I assume this is resolved, but do reopen if not.
muschellij2 commented
Yup - Tried to map some things out at
https://johnmuschelli.com/neuroc/resample_image/index.html
Best,
John
…On Thu, Sep 24, 2020 at 5:21 PM Jon Clayden ***@***.***> wrote:
Closed #13 <#13>.
—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
<#13 (comment)>, or
unsubscribe
<https://github.com/notifications/unsubscribe-auth/AAIGPLTZZPXVGDB33RSZ67TSHOZ7HANCNFSM4ROG4SKQ>
.
jonclayden commented
Nice – that's a very handy summary! Also possibly a demonstration of how confusing it is to have all these different data structures flying around... 😬