SimpleITK  1.2.4
Python/NeighborhoodConnectedImageFilter.py
1 '''=========================================================================
2  '
3  ' Copyright Insight Software Consortium
4  '
5  ' Licensed under the Apache License, Version 2.0 (the "License");
6  ' you may not use this file except in compliance with the License.
7  ' You may obtain a copy of the License at
8  '
9  ' http://www.apache.org/licenses/LICENSE-2.0.txt
10  '
11  ' Unless required by applicable law or agreed to in writing, software
12  ' distributed under the License is distributed on an "AS IS" BASIS,
13  ' WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  ' See the License for the specific language governing permissions and
15  ' limitations under the License.
16  '
17  '========================================================================='''
18 
19 from __future__ import print_function
20 
21 import SimpleITK as sitk
22 import sys
23 import os
24 
25 
26 #
27 # Check Command Line
28 #
29 if len( sys.argv ) < 7:
30  print( "Usage: NeighborhoodConnectedImageFilter inputImage outputImage lowerThreshold upperThreshold seedX seedY [seed2X seed2Y ... ]")
31  sys.exit( 1 )
32 
33 
34 #
35 # Read the image
36 #
37 reader = sitk.ImageFileReader()
38 reader.SetFileName( sys.argv[1] )
39 image = reader.Execute();
40 
41 
42 #
43 # Blur using CurvatureFlowImageFilter
44 #
45 blurFilter = sitk.CurvatureFlowImageFilter()
46 blurFilter.SetNumberOfIterations( 5 )
47 blurFilter.SetTimeStep( 0.125 )
48 image = blurFilter.Execute( image )
49 
50 #
51 # Set up NeighborhoodConnectedImageFilter for segmentation
52 #
53 segmentationFilter = sitk.NeighborhoodConnectedImageFilter()
54 segmentationFilter.SetLower( float(sys.argv[3]) )
55 segmentationFilter.SetUpper( float(sys.argv[4]) )
56 segmentationFilter.SetReplaceValue( 255 )
57 
58 radius = [2,2]
59 segmentationFilter.SetRadius( radius )
60 
61 for i in range( 5, len(sys.argv)-1, 2 ):
62  seed = [int(sys.argv[i]), int(sys.argv[i+1])]
63  segmentationFilter.AddSeed( seed )
64  print( "Adding seed at: ", seed, " with intensity: ", image.GetPixel(*seed) )
65 
66 # Run the segmentation filter
67 image = segmentationFilter.Execute( image )
68 
69 #
70 # Write out the result
71 #
72 writer = sitk.ImageFileWriter()
73 writer.SetFileName( sys.argv[2] )
74 writer.Execute( image )
75 
76 
77 
78 if ( not "SITK_NOSHOW" in os.environ ):
79  sitk.Show( image, "NeighborhoodConnectedThreshold" )