My school team’s practice module project has to deal with 3D volumetric CT scan images to perform automated covid-19 lung segmentation. Due to this urgency to visualize the CT slices to understand more about our dataset and to observe whether the segmentation done right, I need to find a suitable tool that is usable in google Colab. One of the free solution available in github is ImageSliceViewer3D and the sample notebook available here
ImageSliceViewer3D is for viewing volumetric image slices in jupyter or
ipython notebooks.
User can interactively change the slice plane selection for the image and
the slice plane being viewed.
Arguments:
Volume = 3D input image
figsize = default(8,8), to set the size of the figure
cmap = default(‘gray’), string for the matplotlib colormap. You can find
more matplotlib colormaps on the following link:
https://matplotlib.org/users/colormaps.html
Please note ImageSliceViewer3D requires numpy, matplotlib and ipywidgets.
In this guide, I will demonstrate how to set up and upload your own CT scan volume so you can visualize your the CT slice in 3D
- Use below script to create a ImageSliceViewer class
from os.path import dirname, join
from pprint import pprintimport numpy as np
import ipywidgets as ipyw
import matplotlib.pyplot as plt
import nibabel as nibclass ImageSliceViewer3D:
"""
ImageSliceViewer3D is for viewing volumetric image slices in jupyter or
ipython notebooks.
User can interactively change the slice plane selection for the image and
the slice plane being viewed.Arguments:
Volume = 3D input image
figsize = default(8,8), to set the size of the figure
cmap = default('gray'), string for the matplotlib colormap. You can find
more matplotlib colormaps on the following link:
https://matplotlib.org/users/colormaps.html
"""
def __init__(self, volume, figsize=(100,100), cmap='gray'):
self.volume = volume
self.figsize = figsize
self.cmap = cmap
self.v = [np.min(volume), np.max(volume)]
# Call to select slice plane
ipyw.interact(self.views)
def views(self):
self.vol1 = np.transpose(self.volume, [1,2,0])
self.vol2 = np.rot90(np.transpose(self.volume, [2,0,1]), 3) #rotate 270 degrees
self.vol3 = np.transpose(self.volume, [0,1,2])
maxZ1 = self.vol1.shape[2] - 1
maxZ2 = self.vol2.shape[2] - 1
maxZ3 = self.vol3.shape[2] - 1
ipyw.interact(self.plot_slice,
z1=ipyw.IntSlider(min=0, max=maxZ1, step=1, continuous_update=False,
description='Axial:'),
z2=ipyw.IntSlider(min=0, max=maxZ2, step=1, continuous_update=False,
description='Coronal:'),
z3=ipyw.IntSlider(min=0, max=maxZ3, step=1, continuous_update=False,
description='Sagittal:'))def plot_slice(self, z1, z2, z3):
# Plot slice for the given plane and slice
f,ax = plt.subplots(1,3, figsize=self.figsize)
#print(self.figsize)
#self.fig = plt.figure(figsize=self.figsize)
#f(figsize = self.figsize)
ax[0].imshow(self.vol1[:,:,z1], cmap=plt.get_cmap(self.cmap),
vmin=self.v[0], vmax=self.v[1])
ax[1].imshow(self.vol2[:,:,z2], cmap=plt.get_cmap(self.cmap),
vmin=self.v[0], vmax=self.v[1])
ax[2].imshow(self.vol3[:,:,z3], cmap=plt.get_cmap(self.cmap),
vmin=self.v[0], vmax=self.v[1])
plt.show()
2. Indicate the CT image you need to visualise, in this case I uploaded the file into Colab temporary content folder.
3. Load the image with nibabel nib.load and feed to ImageSliceViewer3D as below, and hit run!
IMG_FILE = '/content/coronacases_010.nii.gz'ImageSliceViewer3D(nib.load(IMG_FILE).slicer[:,:,:].get_fdata())
Done!
You are now able to move the slider and see the CT image from 3 different angle. Happy learning!