Wednesday, 12 November 2014

Viewing medical images in an IPython notebook

I was looking for a simple way to browse medical images within an IPython notebook, and came across the interact function which makes it really easy to work with sliders. The function irtk.imshow returns three orthogonal slices for a given (z,y,x) concatenated in a single image. PIL is used to avoid writing temporary images to disk.


In [1]:
import irtk

patient_id = '2159'
img = irtk.imread( patient_id + "_img.nii.gz" )
seg = irtk.imread( patient_id + "_seg.nii.gz" )
In [2]:
from IPython.html.widgets import interact, interactive, fixed
from IPython.html import widgets
from IPython.display import display

def f(z,y,x):
    display( irtk.imshow(img,seg,index=(z,y,x)) )
 
interact( f,
         z=widgets.IntSliderWidget(min=0,max=img.shape[0]-1,step=1,value=img.shape[0]/2),
         y=widgets.IntSliderWidget(min=0,max=img.shape[1]-1,step=1,value=img.shape[1]/2),
         x=widgets.IntSliderWidget(min=0,max=img.shape[2]-1,step=1,value=img.shape[2]/2) )


The sliders disappear when the notebook is saved as HTML, here are some hints for preserving the interactivity through JavaScript (though this is probably not the best approach for an image viewer).

Ideally, a medical image viewer integrated within the IPython notebook would use XTK, but there is still works to be done before it provides a working solution, see github/fperez and github/richstoner for proofs of concept.