The aim of this module is to provide a simple and pythonic interface to IRTK (http://www.doc.ic.ac.uk/~dr/software/) by considering a medical image as a subclass of a numpy.ndarray containing information headers (dimension, orientation, origin and pixel size). By overloading __getitem__, slicing the array automatically updates the dimension and origin information. Images are manipulated in C++ as 4D images (IRTK behaviour), but flat dimensions are removed from Python thanks to an automated call to numpy.squeeze.
Indices are ordered TZYX, eg img[t,z,y,x], which corresponds to the raw pixel array of IRTK but differs from the img(x,y,z,t) C++ interface. Nonetheless, the order in the information header still correspond to the C++ order, that is to say img.header['dim'][0] corresponds to img.GetX() and is equal to img.shape[-1], img.header['pixelSize'][0] corresponds to img.GetXSize(), img.header['orientation'][0] corresponds to xaxis, and finally img.header['origin'][0] correspond to the X origin.
Below is a basic example:
# load the module
import irtk
# read an image
img = irtk.imread("5066_4.nii", dtype="float32")
print img.min(), img.max(), img.std() # methods inherited from numpy.ndarray
# write a PNG thumbnail after saturation, displaying a segmentation by thresholding
irtk.imshow(img.saturate(0.01,0.99), img > 2000, opacity=0.4, filename="thumbnail.png")
Below is a more complex example where we read several scans, register them all to the first one and compute an average volume:
import sys
import irtk
filenames = sys.argv[1:]
img1 = irtk.imread(filenames[0], dtype='float32')
img1 = img1.rescale()
weights = (img1 > 0).astype('int')
for f in filenames[1:]:
img2 = irtk.imread( f, dtype='float32').rescale()
t = img2.register(img1) # rigid registration
img2 = img2.transform(t,target=img1) # linear interpolation by default
img1 += img2
weights += img2 > 0
img1 /= weights.astype('float32') + 0.000001 # avoid division by zero
# write image to disk
irtk.imwrite( "average.nii", img1)
# save thumbnail
irtk.imshow( img1, filename="average.png" )
This module is a wrapper around IRTK (http://www.doc.ic.ac.uk/~dr/software/) written using Cython and a script to simulate templated code.
A subclass of numpy.ndarray holding the information from the NIFTI headers.
BEWARE: images are indexed as img[t,z,y,x], so for instance:
Examples
>>> tx,ty,tz = img.ImageToWorld( [(img.shape[2]-1)/2,(img.shape[1]-1)/2,(img.shape[0]-1)/2] )
Returns integers
constructor
Anisotropic diffusion
Force 3D shape
Force 4D shape
Bounding box.
Returns a deep copy of the object.
Dice and overlap metrics
Gaussian blurring
Gaussian blurring
Return pixel data.
Safe copy of the headers.
Mask image (after transforming the mask if necessary).
Return number of pixels.
Return True if neurological order.
Return either “radiological” or “neurological”.
Return orientation.
Return origin as a 3D point.
Return True if radiological order.
Perform image registration.
Resample.
Resample 2D slices.
Stretch contrast.
Reset header.
Resize image with an affine transformation to force it to a given shape.
Saturate pixel intensities using numpy.percentile.
Set the headers from a new dict.
Create a thumbnail.
Transform image.
Reads an image in a format understandable by IRTK.
Parameters: | filename : str
dtype : str, optional (default: None)
|
---|---|
Returns: | img : irtk.Image
|
Examples
>>> img = irtk.imread( "input.nii", dtype="float32" )
>>> print img[0:2,0:2,0:2]
Image data:
array([[[ 455.95440674, 452.3605957 ],
[ 424.96868896, 419.69546509]],
[[ 462.80618286, 477.69018555],
[ 433.81433105, 455.43170166]]], dtype=float32)
Image shape: (2, 2, 2)
Header:
{ 'dim': array([2, 2, 2, 1], dtype=int32),
'orientation': array([[ 0.18479464, -0.98071011, 0.06370761],
[ 0.34170858, 0.12489687, 0.93146984],
[ 0.92145872, 0.15036121, -0.35819733]]),
'origin': array([ 6.61570985, 35.65394667, -74.69886408, 0. ]),
'pixelSize': array([ 0.75, 0.75, 0.75, 1. ])}
Write image.
Display a 2D image
Inline viewer for IPython Notebook
Similar to numpy.zeros, but with given headers.
Similar to numpy.ones, but with given headers.
Create new header.
Create an image from a set of 3D points.
Draw a sphere in a given image.
Create an image with a filled sphere.
Conditional Random Field.
Graphcut.
Largest connected component
Landmarks to spheres
Rigid transformation class.
Apply.
Return parameters.
Invert.
Return matrix.
Save to disk.
Affine transformation class.
Apply.
Return parameters.
Invert.
Return matrix.
Save to disk.
Read points from file.
Point registration.
Point registration.
See fsl/src/newimage/newimagefns.cc : raw_affine_transform()
flirt_matrix = np.loadtxt(r) https://gitlab.doc.ic.ac.uk/sk1712/irtk/blob/develop/packages/applications/flirt2dof.cc
Generic wrapper for command line applications using medical images.
2D disk for morphological operations.