{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "%matplotlib inline" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n# Brain segmentation with median_otsu\n\nWe show how to extract brain information and mask from a b0 image using DIPY_'s\n``segment.mask`` module.\n\nFirst import the necessary modules:\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "import numpy as np\nfrom dipy.data import get_fnames\nfrom dipy.io.image import load_nifti, save_nifti\nfrom dipy.segment.mask import median_otsu" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Download and read the data for this tutorial.\n\nThe ``scil_b0`` dataset contains different data from different companies and\nmodels. For this example, the data comes from a 1.5 Tesla Siemens MRI.\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "data_fnames = get_fnames('scil_b0')\ndata, affine = load_nifti(data_fnames[1])\ndata = np.squeeze(data)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Segment the brain using DIPY's ``mask`` module.\n\n``median_otsu`` returns the segmented brain data and a binary mask of the\nbrain. It is possible to fine tune the parameters of ``median_otsu``\n(``median_radius`` and ``num_pass``) if extraction yields incorrect results\nbut the default parameters work well on most volumes. For this example,\nwe used 2 as ``median_radius`` and 1 as ``num_pass``\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "b0_mask, mask = median_otsu(data, median_radius=2, numpass=1)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Saving the segmentation results is very easy. We need the ``b0_mask``, and the\nbinary mask volumes. The affine matrix which transform the image's coordinates\nto the world coordinates is also needed. Here, we choose to save both images\nin ``float32``.\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "fname = 'se_1.5t'\nsave_nifti(fname + '_binary_mask.nii.gz', mask.astype(np.float32), affine)\nsave_nifti(fname + '_mask.nii.gz', b0_mask.astype(np.float32), affine)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Quick view of the results middle slice using ``matplotlib``.\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "import matplotlib.pyplot as plt\nfrom dipy.core.histeq import histeq\n\nsli = data.shape[2] // 2\nplt.figure('Brain segmentation')\nplt.subplot(1, 2, 1).set_axis_off()\nplt.imshow(histeq(data[:, :, sli].astype('float')).T,\n cmap='gray', origin='lower')\n\nplt.subplot(1, 2, 2).set_axis_off()\nplt.imshow(histeq(b0_mask[:, :, sli].astype('float')).T,\n cmap='gray', origin='lower')\n\nplt.savefig('median_otsu.png')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ ".. figure:: median_otsu.png\n :align: center\n\n An application of median_otsu for brain segmentation.\n\n``median_otsu`` can also automatically crop the outputs to remove the largest\npossible number of background voxels. This makes outputted data significantly\nsmaller. Auto-cropping in ``median_otsu`` is activated by setting the\n``autocrop`` parameter to ``True``.\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "b0_mask_crop, mask_crop = median_otsu(data, median_radius=4, numpass=4,\n autocrop=True)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Saving cropped data using nibabel as demonstrated previously.\n\n.. include:: ../links_names.inc\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "save_nifti(fname + '_binary_mask_crop.nii.gz', mask_crop.astype(np.float32),\n affine)\nsave_nifti(fname + '_mask_crop.nii.gz', b0_mask_crop.astype(np.float32),\n affine)" ] } ], "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 }