ITK  5.4.0
Insight Toolkit
SphinxExamples/src/Segmentation/Watersheds/SegmentWithWatershedImageFilter/Code.py
1 #!/usr/bin/env python
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 # https://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 # Run with:
18 # ./WatershedImageFilter.py <InputFileName> <OutputFileName> <Threshold> <Level>
19 # e.g.
20 # ./WatershedImageFilter.py BrainProtonDensitySlice.png OutBrainWatershed.png 0.005 .5
21 # (A rule of thumb is to set the Threshold to be about 1 / 100 of the Level.)
22 #
23 # threshold: absolute minimum height value used during processing.
24 # Raising this threshold percentage effectively decreases the number of local minima in the input,
25 # resulting in an initial segmentation with fewer regions.
26 # The assumption is that the shallow regions that thresholding removes are of of less interest.
27 # level: parameter controls the depth of metaphorical flooding of the image.
28 # That is, it sets the maximum saliency value of interest in the result.
29 # Raising and lowering the Level influences the number of segments
30 # in the basic segmentation that are merged to produce the final output.
31 # A level of 1.0 is analogous to flooding the image up to a
32 # depth that is 100 percent of the maximum value in the image.
33 # A level of 0.0 produces the basic segmentation, which will typically be very oversegmented.
34 # Level values of interest are typically low (i.e. less than about 0.40 or 40%),
35 # since higher values quickly start to undersegment the image.
36 
37 import itk
38 import argparse
39 
40 parser = argparse.ArgumentParser(description="Segment With Watershed Image Filter.")
41 parser.add_argument("input_image")
42 parser.add_argument("output_image")
43 parser.add_argument("threshold", type=float)
44 parser.add_argument("level", type=float)
45 args = parser.parse_args()
46 
47 
48 Dimension = 2
49 
50 FloatPixelType = itk.ctype("float")
51 FloatImageType = itk.Image[FloatPixelType, Dimension]
52 
53 reader = itk.ImageFileReader[FloatImageType].New()
54 reader.SetFileName(args.input_image)
55 
56 gradientMagnitude = itk.GradientMagnitudeImageFilter.New(Input=reader.GetOutput())
57 
58 watershed = itk.WatershedImageFilter.New(Input=gradientMagnitude.GetOutput())
59 
60 threshold = args.threshold
61 level = args.level
62 watershed.SetThreshold(threshold)
63 watershed.SetLevel(level)
64 
65 LabeledImageType = type(watershed.GetOutput())
66 
67 PixelType = itk.ctype("unsigned char")
68 RGBPixelType = itk.RGBPixel[PixelType]
69 RGBImageType = itk.Image[RGBPixelType, Dimension]
70 
71 ScalarToRGBColormapFilterType = itk.ScalarToRGBColormapImageFilter[
72  LabeledImageType, RGBImageType
73 ]
74 colormapImageFilter = ScalarToRGBColormapFilterType.New()
75 colormapImageFilter.SetColormap(
76  itk.ScalarToRGBColormapImageFilterEnums.RGBColormapFilter_Jet
77 )
78 colormapImageFilter.SetInput(watershed.GetOutput())
79 colormapImageFilter.Update()
80 
81 WriterType = itk.ImageFileWriter[RGBImageType]
82 writer = WriterType.New()
83 writer.SetFileName(args.output_image)
84 writer.SetInput(colormapImageFilter.GetOutput())
85 writer.Update()
itk::WatershedImageFilter::New
static Pointer New()
itk::RGBPixel
Represent Red, Green and Blue components for color images.
Definition: itkRGBPixel.h:58
itk::ScalarToRGBColormapImageFilter
Implements pixel-wise intensity->rgb mapping operation on one image.
Definition: itkScalarToRGBColormapImageFilter.h:130
itk::ImageFileReader
Data source that reads image data from a single file.
Definition: itkImageFileReader.h:75
itk::ImageFileWriter
Writes image data to a single file.
Definition: itkImageFileWriter.h:88
itk::GradientMagnitudeImageFilter::New
static Pointer New()
itk::Image
Templated n-dimensional image class.
Definition: itkImage.h:88
New
static Pointer New()