Medical image processing in Python
MedPy is a library and script collection for medical image processing in Python, providing basic functionalities for reading, writing and manipulating large images of arbitrary dimensionality. Its main contributions are n-dimensional versions of popular image filters, a collection of image feature extractors, ready to be used with scikit-learn, and an exhaustive n-dimensional graph-cut package.
Troubles? Feel free to write me with any questions / comments / suggestions: firstname.lastname@example.org
Found a bug? https://github.com/loli/medpy/issues
Too many depoendencies? Try our docker images https://registry.hub.docker.com/u/loli/medpy/ (release) and https://registry.hub.docker.com/u/loli/medpy-autobuilds/ (development)
sudo apt-get install python-pip python-numpy python-scipy libboost-python-dev build-essential
sudo pip install nibabel pydicom medpy
Done. More installation instructions can be found in the documentation.
If you already have one, whose format is support (see in the documentation.), then good. Otherwise navigate to http://www.nitrc.org/projects/inia19, click on the Download Now button, unpack and look for the inia19-t1.nii file. Open it in your favorite medical image viewer (I personally fancy itksnap) and beware a the INIA19 primate brain atlas.
Load the image
>>> from medpy.io import load >>> image_data, image_header = load('/path/to/image.xxx')
The data is stored in a numpy ndarray, the header is an object containing additional metadata, such as the voxel-spacing. No lets take a look at some of the image metadata
>>> image_data.shape (168, 206, 128) >>> image_data.dtype dtype(float32)
And the header gives us
>>> from medpy.io import header >>> header.get_pixel_spacing(image_header) (0.5, 0.5, 0.5) >>> header.get_offset(image_header) (0.0, 0.0, 0.0)
Now lets apply one of the MedPy filter, more exactly the Otsu thresholding, which can be used for automatic background removal
>>> from medpy.filter import otsu >>> threshold = otsu(image_data) >>> output_data = image_data > threshold
And save the binary image, marking the foreground
>>> from medpy.io import save >>> save(output_data, '/path/to/otsu.xxx', image_header)
After taking a look at it, you might want to dive deeper with the documentation.
Get an image as described above. Now:
will give you some details about the image. With:
medpy_diff.py /path/to/image1.xxx /path/to/image2.xxx
you can compare two image. And:
medpy_anisotropic_diffusion.py /path/to/image.xxx /path/to/output.xxx
lets you apply an edge preserving anisotropic diffusion filter. For a list of all scripts, see the documentation.
MedPy builds on 3rd party modules to load and save images. Currently implemented are the usages of
, each of which supports the following formats.
NiBabel enables support for:
PyDicom enables support for:
ITK enables support for:
For some functionalities, which are collected in the medpy.itkvtk package ITK is also required.
You can find our sources and single-click downloads:
MedPy comes with a number of dependencies and optional functionality that can require you to install additional packages.
MedPy is distributed under the GNU General Public License, a version of which can be found in the LICENSE.txt file.