Advertisement
Guest User

Untitled

a guest
Dec 1st, 2015
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.13 KB | None | 0 0
  1. # Segment ventricles and return volume in cubic millimetres (= millilitres)
  2. # ADD IMPLEMENTATION of segmentation and volume calculation below
  3.  
  4. from nipy import load_image, save_image
  5. from nipy.core.image.image_spaces import (make_xyz_image,xyz_affine)
  6. from nipy.algorithms.segmentation.brain_segmentation import BrainT1Segmentation
  7.  
  8. def my_segmentation_func (fullTrainingImageName,maskname,segSaveDir='./data/seg/'):
  9.    
  10.     # Segmentation name
  11.     fullSegmentationName  = segSaveDir + fullTrainingImageName.split('/')[-1]
  12.    
  13.     # Load the mask
  14.     # See https://sites.google.com/site/mrilateralventricle/ for more details
  15.     maskimage = sitk.ReadImage(maskname)
  16.     maskarray = sitk.GetArrayFromImage(maskimage)
  17.    
  18.     # Check if segmentation is already saved to disk
  19.     if not os.path.isfile(fullSegmentationName):
  20.        
  21.         # Read the image
  22.         img_sitk = sitk.ReadImage(fullTrainingImageName)
  23.         img      = load_image(fullTrainingImageName)
  24.         mask     = img.get_data() > 0 # ingoring background pixels
  25.  
  26.         # Perform tissue classification
  27.         # ADD IMPLEMENTATION - store segmentation in labelimage
  28.         S = BrainT1Segmentation(img.get_data(),
  29.                         mask=mask,       # the algorithm ignores the background pixels for faster comp.
  30.                         model='5k',
  31.                         niters=int(50),  # number of iterations
  32.                         beta=float(0.4), # Markov regularization parameter
  33.                         ngb_size=int(6)) # neighborhood size of MRF
  34.         xyz_image_array = make_xyz_image(S.label, xyz_affine(img), 'scanner').get_data()
  35.         masked_image_array = xyz_image_array * maskarray
  36.         labelimage = sitk.GetImageFromArray(masked_image_array)
  37.  
  38.         # Save the segmentation to disk to avoid recomputation
  39.         sitk.WriteImage(labelimage,fullSegmentationName)
  40.  
  41.     # Load the segmentation
  42.     labelimage = sitk.ReadImage(fullSegmentationName)
  43.        
  44.     # Compute the volume
  45.     # ADD IMPLEMENTATION - store volume in vol
  46.     labelarray = sitk.GetArrayFromImage(labelimage)
  47.     vol = sum(labelarray == ????)
  48.     return vol
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement