|
a |
|
b/CLI/MusculoskeletalAnalysisCLITools/reader.py |
|
|
1 |
"""Converts slicer image and segmentation volumes into numpy format.""" |
|
|
2 |
|
|
|
3 |
|
|
|
4 |
def readImg(inputImg): |
|
|
5 |
"""Reads an image file as input, returns the image as a numpy array.""" |
|
|
6 |
import SimpleITK as sitk |
|
|
7 |
|
|
|
8 |
imgReader = sitk.ImageFileReader() |
|
|
9 |
imgReader.SetFileName(inputImg) |
|
|
10 |
image = imgReader.Execute() |
|
|
11 |
image = sitk.DICOMOrient(image, 'SPL') |
|
|
12 |
imgData = sitk.GetArrayFromImage(image) |
|
|
13 |
return imgData |
|
|
14 |
|
|
|
15 |
|
|
|
16 |
def readMask(inputMask): |
|
|
17 |
"""Reads an nrrd file as input, returns the image as a binary numpy array.""" |
|
|
18 |
from nrrd import read |
|
|
19 |
|
|
|
20 |
maskReader = read(inputMask) |
|
|
21 |
maskHeader = maskReader[1:] |
|
|
22 |
maskData = maskReader[0].astype('bool') |
|
|
23 |
return maskHeader, maskData |