{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "%matplotlib inline" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n# Reslice diffusion datasets\n\n## Overview\nOften in imaging it is common to reslice images in different resolutions.\nEspecially in dMRI we usually want images with isotropic voxel size as they\nfacilitate most tractography algorithms. In this example we show how you\ncan reslice a dMRI dataset to have isotropic voxel size.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "import nibabel as nib\n\nfrom dipy.align.reslice import reslice\nfrom dipy.data import get_fnames\nfrom dipy.io.image import load_nifti, save_nifti" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We use here a very small dataset to show the basic principles but you can\nreplace the following line with the path of your image.\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "fimg = get_fnames('aniso_vox')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We load the image, the affine of the image and the voxel size. The affine is\nthe transformation matrix which maps image coordinates to world (mm)\ncoordinates. Then, we print the shape of the volume\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "data, affine, voxel_size = load_nifti(fimg, return_voxsize=True)\nprint(data.shape)\nprint(voxel_size)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "``(58, 58, 24)``\n``(4.0, 4.0, 5.0)``\n\nSet the required new voxel size.\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "new_voxel_size = (3., 3., 3.)\nprint(new_voxel_size)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "``(3.0, 3.0, 3.0)``\n\nStart resampling (reslicing). Trilinear interpolation is used by default.\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "data2, affine2 = reslice(data, affine, voxel_size, new_voxel_size)\nprint(data2.shape)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "``(77, 77, 40)``\n\nSave the result as a new Nifti file.\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "save_nifti('iso_vox.nii.gz', data2, affine2)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Or as analyze format or any other supported format.\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "img3 = nib.Spm2AnalyzeImage(data2, affine2)\nnib.save(img3, 'iso_vox.img')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Done. Check your datasets. As you may have already realized the same\ncode can be used for general reslicing problems not only for dMRI data.\n\n\n" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.9.13" } }, "nbformat": 4, "nbformat_minor": 0 }