[Neuroimaging] [Nibabel] up/downsample image in Python
valabregue
romain.valabregue at upmc.fr
Mon Aug 21 05:09:20 EDT 2017
Hello
I also notice that if you just apply a zoom you end up with an affine
with the exact same translation.
If I now compare with the reslice done by mrtrix ( mrresize img1.nii
-vox 2 img2.nii) I get almost the same except it changes translation of
the affine by 1mm. I tried to understand what exact shift one need to
add but I can find the solution. (affter testing different resampling 2
3 4 mn, I do not get what is exactly done)
It may have something to do with the 'world space coordinate' which
refer to the center of the voxel (not sure what the convention is). if
this is the center it makes sens that on need to apply a shift to keep
the data align. however I can not figure the correct shift to apply
Here is the code I use to apply zoom and translation
img=ni.load('img1.nii')
translations, rotations, zooms, shears
=transforms3d.affines.decompose44(img.affine)
newzooms=zooms*2
shift = (newzooms-zooms)/2
#shift = (newzooms)/2
#neither of the previous 2 shifts gives the one use by mrtirx
newtranslations=translations+shift
newaff=transforms3d.affines.compose(newtranslations,rotations,newzooms,shears)
ii=nip.resample_from_to(img,(np.array(img.shape)/2,newaff))
ii.to_filename('lowres.nii')
cheers
Romain
On 08/18/2017 10:52 PM, Michael Waskom wrote:
> Hi Matthew/Chris/others,
>
> Building on my previous question, I would also like to implement a
> pure-python downsampling of my images. I have done:
>
> import nibabel as nib
> from scipy import ndimage
> img = nib.load("brain.nii")
> data = img.get_data()
> lowres_data = ndimage.zoom(data, .5)
> zoom_xfm = np.eye(4)
> zoom_xfm[:3, :3] /= .5
> lowres_affine = img.affine.dot(zoom_xfm)
> lowres_img = nib.Nifti1Image(lowres_data, lowres_affine, img.header)
> lowres_img.to_fileame("lowres_brain.nii")
>
> This is very close, but when I view the resulting image in Freeview
> (my downsampled image is also slightly stretched relative to the
> anatomical).
>
> I can also compare to the result from mri_convert, i.e. mri_convert
> brain.nii -vs 2 2 2 lowres_brain.nii. The resulting image is not
> stretched relative to the hires brain, so it's not an artifact on
> Freeview's side. The images also don't have an identical affine (it is
> off by 0.5 in the third row of the final column.
>
> What am I missing about how to alter to affine of a downsampled image?
> Alternative, is ndimage.zoom not the right Python function to use here?
>
> Best,
> Michael
>
>
> _______________________________________________
> 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/20170821/a5c7572c/attachment.html>
More information about the Neuroimaging
mailing list