-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMha2Aim_py3.py
68 lines (55 loc) · 1.72 KB
/
Mha2Aim_py3.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
"""Converts MHA to AIM files.
Description
converts Mhd/Mha file to Aim
History:
2016.07.06 Bryce Besler Created
2016.08.18 Andres Kroker Adjust skalar type to be SCANCO compatible
Notes:
- None
"""
# Imports
import vtk
import vtkbone
import argparse
# Parse input arguments
parser = argparse.ArgumentParser()
parser.add_argument("inputImage",
type=str,
help="The input mha image")
parser.add_argument("outputImage",
type=str,
help="The output AIM image")
args = parser.parse_args()
inputImage = args.inputImage
outputImage = args.outputImage
# Read in the image
print("Reading {0}".format(inputImage))
reader = vtk.vtkMetaImageReader()
reader.SetFileName(inputImage)
reader.Update()
inputScalarType = reader.GetOutput().GetScalarType()
if (inputScalarType == vtk.VTK_BIT or
inputScalarType == vtk.VTK_CHAR or
inputScalarType == vtk.VTK_SIGNED_CHAR or
inputScalarType == vtk.VTK_UNSIGNED_CHAR):
scalarRange = reader.GetOutput().GetScalarRange()
if scalarRange[0] >= vtk.VTK_SHORT_MIN and scalarRange[1] <= vtk.VTK_SHORT_MAX:
outputScalarType = vtk.VTK_CHAR
else:
outputScalarType = vtk.VTK_CHAR
else:
outputScalarType = vtk.VTK_SHORT
# Cast
print("Converting to {0}".format(vtk.vtkImageScalarTypeNameMacro(outputScalarType)))
caster = vtk.vtkImageCast()
caster.SetOutputScalarType(outputScalarType)
caster.SetInputConnection(reader.GetOutputPort())
caster.ReleaseDataFlagOff()
caster.Update()
# Write the image out
print("Writing to {0}".format(outputImage))
writer = vtkbone.vtkboneAIMWriter()
writer.SetFileName(outputImage)
writer.SetInputData(caster.GetOutput())
writer.NewProcessingLogOn()
writer.Write()