-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathsimpleitk_example.py
50 lines (29 loc) · 1.19 KB
/
simpleitk_example.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
#!/usr/bin/env python
from __future__ import print_function
import SimpleITK as sitk
import sys
import os
if len ( sys.argv ) < 2:
print( "Usage: N4BiasFieldCorrection inputImage " + \
"outputImage [shrinkFactor] [maskImage] [numberOfIterations] " +\
"[numberOfFittingLevels]" )
sys.exit ( 1 )
inputImage = sitk.ReadImage( sys.argv[1] )
if len ( sys.argv ) > 4:
maskImage = sitk.ReadImage( sys.argv[4] )
else:
maskImage = sitk.OtsuThreshold( inputImage, 0, 1, 200 )
if len ( sys.argv ) > 3:
inputImage = sitk.Shrink( inputImage, [ int(sys.argv[3]) ] * inputImage.GetDimension() )
maskImage = sitk.Shrink( maskImage, [ int(sys.argv[3]) ] * inputImage.GetDimension() )
inputImage = sitk.Cast( inputImage, sitk.sitkFloat32 )
corrector = sitk.N4BiasFieldCorrectionImageFilter();
numberFilltingLevels = 4
if len ( sys.argv ) > 6:
numberFilltingLevels = int( sys.argv[6] )
if len ( sys.argv ) > 5:
corrector.SetMaximumNumberOfIterations( [ int( sys.argv[5] ) ] *numberFilltingLevels )
output = corrector.Execute( inputImage, maskImage )
sitk.WriteImage( output, sys.argv[2] )
if ( not "SITK_NOSHOW" in os.environ ):
sitk.Show( output, "N4 Corrected" )