[Neuroimaging] Fwd: Re: Apply resolution of one image.nii to another image.nii
Eric Condamine
eric.condamine at univ-grenoble-alpes.fr
Fri Jan 18 11:42:16 EST 2019
Hello nipy experts,
Many thanks to Bertrand Thirion and Matthew Brett for their fast and
suitable answers ! i.e. nilearn.image.resample_to_img() and
nibabel.processing.resample_from_to(), respectively.
I tested these 2 possibilities, in addition to my proposal (skimage) and
using as a reference the results obtained from imcalc (SPM), for the
comparison (I don't know it is the best reference ... but it is mine :-))
1- I definitely not recommend my proposal !!! It was the less good
result obtained ....
2- all these methods (including imcalc) make the header of the resized
image from the header of the reference (especially the affine
parameter). So, to obtain a good result, it is better to use images in
the same space and position (i.e. after coregistration and normalisation).
3- As warned by Matthew, it was sometimes necessary to threshold the
resized image with the nibabel.processing.resample_from_to() option.
Indeed, the background with zero intensity in the original image was no
longer zero in the resized image. This was not observed in all tests and
I didn't take the time to investigate why. This was never necessary with
the nilearn.image.resample_to_img(), in the same tests.
4- Except this non-zero intensity in the background of the resized image
with nibabel.processing.resample_from_to(), and only in some cases, I
obtained with nibabel.processing.resample_from_to() and
nilearn.image.resample_to_img(), results very, very close of what it was
possible to obtain with imcalc of SPM.
Again, thank you very much for your help !
Eric
**************************
Script used:
## *imcalc*
matlabbatch{1}.spm.util.imcalc.input = {
'/home/econdami/Desktop/test_resizeSPM/test1/ref.nii,1'
'/home/econdami/Desktop/test_resizeSPM/test1/mask.nii,1'
};
matlabbatch{1}.spm.util.imcalc.output = 'output_image';
matlabbatch{1}.spm.util.imcalc.outdir =
{'/home/econdami/Desktop/test_resizeSPM/test1/'};
matlabbatch{1}.spm.util.imcalc.expression = 'i2';
matlabbatch{1}.spm.util.imcalc.options.dmtx = 0;
matlabbatch{1}.spm.util.imcalc.options.mask = 0;
matlabbatch{1}.spm.util.imcalc.options.interp = 1;
matlabbatch{1}.spm.util.imcalc.options.dtype = 4;
## *nilearn.image*
from nilearn.image import resample_to_img
import nibabel as nib
output=resample_to_img('/home/econdami/Desktop/test_resize/test_resizeNilearn/mask.nii',
'/home/econdami/Desktop/test_resize/test_resizeNilearn/ref.nii')
nib.save(output,
'/home/econdami/Desktop/test_resize/test_resizeNilearn/output_image')
## *nilearn.image*
import nibabel.processing as nibp
import nibabel as nib
mask =
nib.load('/home/econdami/Desktop/test_resize/test_resizeNibabel/mask.nii')
ref =
nib.load('/home/econdami/Desktop/test_resize/test_resizeNibabel/ref.nii')
ref3D = ref.slicer[:,:,:,0] # ref image data is 4-D array (length,
length, length, time). Takes the first brain volume.
output=nibp.resample_from_to(mask, ref3D)
nib.save(output,
'/home/econdami/Desktop/test_resize/test_resizeNibabel/output_image')
## *skimage.transform*
from skimage.transform import resize
import nibabel as nib
mask = nib.load('/home/econdami/Desktop/test_resize/test_skimage/mask.nii')
ref = nib.load('/home/econdami/Desktop/test_resize/test_skimage/ref.nii')
ref_data = ref.get_fdata()
mask_data = mask.get_fdata()
ref_size = ref_data.shape[:3] # ref image data is a 4-D array (length,
length, length, time). Takes the first brain volume size.
output = resize(mask_data, ref_size, order=1, mode='reflect') # order=1
(Bi-linear) is the default value and it seems that with 3D image,
Bi-linear is equivalent to 'trilinear' of SPM's imcalc function ?
output_ref_mask = nib.Nifti1Image(output, ref.affine, mask.header) #
Here some test was done by using also output_ref_ref =
nib.Nifti1Image(output, ref.affine, ref.header) etc.
nib.save(output_ref_mask,
'/home/econdami/Desktop/test_resize/test_skimage/output_image_ref_mask')
-------- Forwarded Message --------
Date: Fri, 11 Jan 2019 16:00:12 +0000
From: Matthew Brett <matthew.brett at gmail.com>
To: Neuroimaging analysis in Python <neuroimaging at python.org>
Subject: Re: [Neuroimaging] Apply resolution of one image.nii to another
image.nii
Message-ID:
<CAH6Pt5paJru9=XrUkH31tpN+zu9JqBD2Eb8xoR2urJdFn988Sg at mail.gmail.com>
Content-Type: text/plain; charset="UTF-8
Hi,
Did you discover the processing module in nibabel? Maybe
"resample_from_to" would help:
http://nipy.org/nibabel/reference/nibabel.processing.html#nibabel.processing.resample_from_to
Something like: import nibabel.processing as nibp resized =
nibp.resample_from_to(mask, ref)
Does that work? You'd probably have to threshold the image binarize it -
trilinear resampling is the default - or use 0 (nearest neighbor)
resampling.
Cheers, Matthew
-------- Forwarded Message --------
Subject: Re: [Neuroimaging] Apply resolution of one image.nii to
another image.nii
Date: Fri, 11 Jan 2019 17:07:42 +0100 (CET)
From: Bertrand Thirion <bertrand.thirion at inria.fr>
To: eric.condamine at univ-grenoble-alpes.fr
Hi Eric,
you should use
nilearn.image.resample_to_img
Best,
Bertrand
------------------------------------------------------------------------
*De: *"Eric Condamine" <eric.condamine at univ-grenoble-alpes.fr>
*À: *neuroimaging at python.org
*Envoyé: *Vendredi 11 Janvier 2019 16:26:05
*Objet: *[Neuroimaging] Apply resolution of one image.nii to another
image.nii
Hello nipy experts,
I would like to change the resolution of an nifti image (let's say
mask.nii) according to the resolution of one other nifti image
(let's say ref.nii), a process that i was doing with the imcalc
function of spm before i start to switch to python ... i confess
that i do not yet know all the tools currently available under nipy
umbrella ...
One way to do it could be something like that.
"""
from skimage.transform import resize
import nibabel as nib
mask = nib.load(mask.nii)
mask_data = mask.get_fdata()
ref = nib.load(ref.nii)
ref_data = ref.get_fdata()
ref_size = ref_data.shape[:3]
resized_mask_data = resize(mask_data, ref_size, order=1,
mode='reflect') # order=1 (Bi-linear) is the default value and it
seems that with 3D image, Bi-linear is equivalent to 'trilinear' of
SPM's imcalc function ?
mask_final = nib.Nifti1Image(resized_mask_data, xxx, yyy)
nib.save(mask_final, mask_final)
I do not succeed to obtain the wanted mask_final.nii (exactly the
same as mask.nii, ie. same orientation in the same space, with only
a change of the resolution). may be I am wrong with the xxx and yyy
in nib.Nifti1Image().
I tried:
nib.Nifti1Image(resized_mask_data, mask.affine, ref.header)
or
nib.Nifti1Image(resized_mask_data, mask.affine, mask.header)
etc. without success.
So I plan to make a python function in order to make the correct
header for the mask_final.nii after the resize() function.
Because this need seems pretty commun, i guess that a tools to do it
is is already existing in python ?
So before reinvent the wheel, I would like to be sure that I do not
make mistake with xxx and/or yyy ?
May be other function/tool doing the job and that I do not know exist ?
Thank you for all suggests or informations,
All the best.
--
Eric Condamine <http://fr.linkedin.com/pub/eric-condamine/27/529/290>
IRMaGe <https://irmage.ujf-grenoble.fr/>
INSERM US17-CNRS UMS3552-UGA-CHUGA
Unité IRM 3T Recherche
CHU Grenoble - CS 10217
38043 Grenoble Cedex 9
Tel : (00 33) 4 7676 7575 - Poste 63 202
: (00 33) 4 7676 8726
Fax : (00 33) 4 7676 9305
e-mail:eric.condamine at univ-grenoble-alpes.fr <eric.condamine at univ-grenoble-alpes.f>
_______________________________________________
Neuroimaging mailing list
Neuroimaging at python.org
https://mail.python.org/mailman/listinfo/neuroimaging
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/neuroimaging/attachments/20190118/b3353c45/attachment.html>
More information about the Neuroimaging
mailing list