SimpleITK  
Python/NeighborhoodConnectedImageFilter.py
1 """=========================================================================
2  '
3  ' Copyright NumFOCUS
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 
20 import os
21 import sys
22 
23 import SimpleITK as sitk
24 
25 #
26 # Check Command Line
27 #
28 if len(sys.argv) < 7:
29  print(
30  "Usage:",
31  sys.argv[1],
32  "inputImage outputImage lowerThreshold",
33  "upperThreshold seedX seedY [seed2X seed2Y ... ]",
34  )
35  sys.exit(1)
36 
37 #
38 # Read the image
39 #
40 reader = sitk.ImageFileReader()
41 reader.SetFileName(sys.argv[1])
42 image = reader.Execute()
43 
44 #
45 # Blur using CurvatureFlowImageFilter
46 #
47 blurFilter = sitk.CurvatureFlowImageFilter()
48 blurFilter.SetNumberOfIterations(5)
49 blurFilter.SetTimeStep(0.125)
50 image = blurFilter.Execute(image)
51 
52 #
53 # Set up NeighborhoodConnectedImageFilter for segmentation
54 #
55 segmentationFilter = sitk.NeighborhoodConnectedImageFilter()
56 segmentationFilter.SetLower(float(sys.argv[3]))
57 segmentationFilter.SetUpper(float(sys.argv[4]))
58 segmentationFilter.SetReplaceValue(255)
59 
60 radius = [2, 2]
61 segmentationFilter.SetRadius(radius)
62 
63 for i in range(5, len(sys.argv) - 1, 2):
64  seed = [int(sys.argv[i]), int(sys.argv[i + 1])]
65  segmentationFilter.AddSeed(seed)
66  print("Adding seed at: ", seed, " with intensity: ", image.GetPixel(*seed))
67 
68 # Run the segmentation filter
69 image = segmentationFilter.Execute(image)
70 
71 #
72 # Write out the result
73 #
74 writer = sitk.ImageFileWriter()
75 writer.SetFileName(sys.argv[2])
76 writer.Execute(image)
77 
78 if "SITK_NOSHOW" not in os.environ:
79  sitk.Show(image, "NeighborhoodConnectedThreshold")
itk::simple::NeighborhoodConnectedImageFilter
Label pixels that are connected to a seed and lie within a neighborhood.
Definition: sitkNeighborhoodConnectedImageFilter.h:41
itk::simple::Show
void SITKIO_EXPORT Show(const Image &image, const std::string &title="", const bool debugOn=ProcessObject::GetGlobalDefaultDebug())
itk::simple::ImageFileReader
Read an image file and return a SimpleITK Image.
Definition: sitkImageFileReader.h:60
itk::simple::CurvatureFlowImageFilter
Denoise an image using curvature driven flow.
Definition: sitkCurvatureFlowImageFilter.h:71
itk::simple::ImageFileWriter
Write out a SimpleITK image to the specified file location.
Definition: sitkImageFileWriter.h:48