[Neuroimaging] [Nibabel] up/downsample image in Python
bthirion
bertrand.thirion at inria.fr
Sat Aug 19 05:01:25 EDT 2017
I would indeed try nilearn.image.resample_img()
B
On 18/08/2017 23:28, Christopher Markiewicz wrote:
> Hi Michael,
>
> At first glance this looks reasonable to me, so if I were to try to
> figure out the issue, I'd probably end up digging through nilearn's
> image resampling, which uses ndimage.affine_transform under the hood
> [0]. It's a much more flexible function that what you're looking for,
> but I think you should be able to find what you need in all of that.
>
> I'm also pretty sure that, with a nipy image, you can do this with
> `img[::2, ::2, ::2]`, but not sure quite where to point you to in that
> codebase.
>
> Chris
>
> [0]
> https://github.com/nilearn/nilearn/blob/master/nilearn/image/resampling.py#L221-L271
>
> On Fri, Aug 18, 2017 at 4:52 PM, Michael Waskom <mwaskom at nyu.edu
> <mailto:mwaskom at nyu.edu>> 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 <mailto:Neuroimaging at python.org>
> https://mail.python.org/mailman/listinfo/neuroimaging
> <https://mail.python.org/mailman/listinfo/neuroimaging>
>
>
>
>
> _______________________________________________
> 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/20170819/f1321c85/attachment.html>
More information about the Neuroimaging
mailing list