SimpleITK  1.2.4
Python/BoarderSegmentation.py
1 #!/usr/bin/env python
2 #=========================================================================
3 #
4 # Copyright Insight Software Consortium
5 #
6 # Licensed under the Apache License, Version 2.0 (the "License");
7 # you may not use this file except in compliance with the License.
8 # You may obtain a copy of the License at
9 #
10 # http://www.apache.org/licenses/LICENSE-2.0.txt
11 #
12 # Unless required by applicable law or agreed to in writing, software
13 # distributed under the License is distributed on an "AS IS" BASIS,
14 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 # See the License for the specific language governing permissions and
16 # limitations under the License.
17 #
18 #=========================================================================
19 
20 from __future__ import print_function
21 
22 import sys
23 import SimpleITK as sitk
24 import os
25 
26 # verify that we have the correct number of arguments
27 if ( len( sys.argv ) != 5 ):
28  sys.stderr.write( "Usage: prog inputFile outputFile replaceValue upperThreshold\n" )
29  exit( 1 )
30 
31 # copy the arguments in to variables
32 inputFileName = sys.argv[1]
33 outputFileName = sys.argv[2]
34 replaceValue = int( sys.argv[3] )
35 upperThreshold = float( sys.argv[4] )
36 
37 # Read the file into an sitkImage
38 image = sitk.ReadImage( inputFileName )
39 
40 # Threshold the value [0,2), results in values inside the range 1, 0 otherwise
41 boundary = sitk.BinaryThreshold( image, 0, upperThreshold, 1, 0 )
42 
43 boundary = sitk.BinaryMorphologicalClosing( boundary, 1 )
44 
45 # Remove any label pixel not connected to the boarder
46 boundary = sitk.BinaryGrindPeak( boundary )
47 
48 
49 
50 boundary = sitk.Cast( boundary, image.GetPixelID() )
51 
52 # Multiply, the input image by not the boarder.
53 # This will multiply the image by 0 or 1, where 0 is the
54 # boarder. Making the board 0
55 image = image * ~boundary
56 
57 # add the replace value to the pixel on the board
58 image = image + ( boundary * replaceValue )
59 
60 
61 if ( not "SITK_NOSHOW" in os.environ ):
62  sitk.Show( image, "Boarder Segmentation" )