Skip to content

Instantly share code, notes, and snippets.

@notmatthancock
Created October 1, 2017 12:10
Show Gist options
  • Save notmatthancock/3524d42edf923bb0ab8513a808678650 to your computer and use it in GitHub Desktop.
Save notmatthancock/3524d42edf923bb0ab8513a808678650 to your computer and use it in GitHub Desktop.
pylidc volume mask
import pylidc as pl
import numpy as np
import matplotlib.pyplot as plt
from skimage.viewer import CollectionViewer
# Load scan, convert to volume.
scan = pl.query(pl.Scan).filter(pl.Scan.patient_id == "LIDC-IDRI-0001").first()
vol = scan.to_volume(verbose=False)
# mask_vol is a boolean, indicator volume for the first annotation of the scan.
mask_vol = np.zeros(vol.shape, dtype=np.bool)
# Load dicom files and obtain z-coords for each slice, so
# we can index into them.
dicoms = scan.load_all_dicom_images(verbose=False)
zs = [float(img.ImagePositionPatient[2]) for img in dicoms]
mask,bbox = scan.annotations[0].get_boolean_mask(return_bbox=True)
# Obtain indexes of `mask` into `mask_vol`.
i1,i2 = bbox[0].astype(np.int)
j1,j2 = bbox[1].astype(np.int)
k1 = zs.index(bbox[2,0])
k2 = zs.index(bbox[2,1])
mask_vol[i1:i2+1, j1:j2+1, k1:k2+1] = mask
# Send vol to range [0,1] for CollectionViewer.
vol = (vol-vol.min()) / (vol.max() - vol.min())
# Gather slices augmented by mask_vol.
slices = [(mask_vol[:,:,i]*0.8+0.1)*vol[:,:,i] for i in range(vol.shape[2])]
# View them.
CollectionViewer(slices).show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment