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