loli / medpy

Medical image processing in Python
http://loli.github.io/medpy/
GNU General Public License v3.0
559 stars 136 forks source link

Document loading images and masks for deep learning applications #88

Closed gkaissis closed 4 years ago

gkaissis commented 5 years ago

It would be great if some documentation could be provided for how to load a stack of dicoms or a .nii file alongside a segmentation (.nii) and output a cropped n-dimensional array with padding ready for use in deep learning applications.

loli commented 5 years ago

I'm not sure if I understand what you want. Something like this?

import numpy as np
from medpy.io import load
from scipy.ndimage import find_objects

def fancyload(fimg, fseg):
  seg, segh = load(fseg)
  seg = seg.astype(np.bool)

  img, imgh = load(fimg)
  img = img.astype(np.float)

  objslices = find_objects(seg)[0]
  # your can add your padding to the returned slice

  return img[objslices], seg[objslices]

If not, please elaborate. Cropped to what? Padded by what?

PS: Untested, might contain some typos or bugs

gkaissis commented 5 years ago

Say I want to perform a deep learning analysis on a tumor. I open the .nii in ITK-SNAP or some other segmentation software and draw a mask, which I save as a .nii overlay. If I want to enter the image into a deep neural network, I don't want the whole image in there, just the tumor. Therefore I want the program to load the original image, load the mask and throw away everything except the tumor, pad the tumor with zeros and then return an n-dimensional numpy array to put into TensorFlow. I didn't find an explicit mention of such a feature in the documentation but it would be extremely valuable. Thank you!