Commit 8c818f36 authored by Ben Glocker's avatar Ben Glocker
Browse files

initial commit

parents
This diff is collapsed.
This diff is collapsed.
# Copyright 2019, Imperial College London
#
# CO416 - Machine Learning for Imaging
#
# This file: Functions to visualise medical imaging data.
import numpy as np
import SimpleITK as sitk
import matplotlib.pyplot as plt
from ipywidgets import interact, fixed
from IPython.display import display
# Calculate parameters low and high from window and level
def wl_to_lh(window, level):
low = level - window/2
high = level + window/2
return low,high
def display_image(img, x=None, y=None, z=None, window=None, level=None, colormap='gray', crosshair=False):
# Convert SimpleITK image to NumPy array
img_array = sitk.GetArrayFromImage(img)
# Get image dimensions in millimetres
size = img.GetSize()
spacing = img.GetSpacing()
width = size[0] * spacing[0]
height = size[1] * spacing[1]
depth = size[2] * spacing[2]
if x is None:
x = np.floor(size[0]/2).astype(int)
if y is None:
y = np.floor(size[1]/2).astype(int)
if z is None:
z = np.floor(size[2]/2).astype(int)
if window is None:
window = np.max(img_array) - np.min(img_array)
if level is None:
level = window / 2 + np.min(img_array)
low,high = wl_to_lh(window,level)
# Display the orthogonal slices
fig, (ax1, ax2, ax3) = plt.subplots(1, 3, figsize=(10, 4))
ax1.imshow(img_array[z,:,:], cmap=colormap, clim=(low, high), extent=(0, width, height, 0))
ax2.imshow(img_array[:,y,:], origin='lower', cmap=colormap, clim=(low, high), extent=(0, width, 0, depth))
ax3.imshow(img_array[:,:,x], origin='lower', cmap=colormap, clim=(low, high), extent=(0, height, 0, depth))
# Additionally display crosshairs
if crosshair:
ax1.axhline(y * spacing[1], lw=1)
ax1.axvline(x * spacing[0], lw=1)
ax2.axhline(z * spacing[2], lw=1)
ax2.axvline(x * spacing[0], lw=1)
ax3.axhline(z * spacing[2], lw=1)
ax3.axvline(y * spacing[1], lw=1)
plt.show()
def interactive_view(img):
size = img.GetSize()
img_array = sitk.GetArrayFromImage(img)
interact(display_image,img=fixed(img),
x=(0, size[0] - 1),
y=(0, size[1] - 1),
z=(0, size[2] - 1),
window=(0,np.max(img_array) - np.min(img_array)),
level=(np.min(img_array),np.max(img_array)));
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment