ITK  5.2.0
Insight Toolkit
SphinxExamples/src/Filtering/ImageFeature/DetectEdgesWithCannyFilter/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 # 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 import sys
18 import itk
19 
20 if len(sys.argv) != 6:
21  print("Usage: " + sys.argv[0] + " <InputImage> <OutputImage> "
22  "<Variance> <LowerThreshold> <UpperThreshold>")
23  sys.exit(1)
24 
25 inputImage = sys.argv[1]
26 outputImage = sys.argv[2]
27 variance = float(sys.argv[3])
28 lowerThreshold = float(sys.argv[4])
29 upperThreshold = float(sys.argv[5])
30 
31 InputPixelType = itk.F
32 OutputPixelType = itk.UC
33 Dimension = 2
34 
35 InputImageType = itk.Image[InputPixelType, Dimension]
36 OutputImageType = itk.Image[OutputPixelType, Dimension]
37 
38 reader = itk.ImageFileReader[InputImageType].New()
39 reader.SetFileName(inputImage)
40 
42  InputImageType,
43  InputImageType].New()
44 cannyFilter.SetInput(reader.GetOutput())
45 cannyFilter.SetVariance(variance)
46 cannyFilter.SetLowerThreshold(lowerThreshold)
47 cannyFilter.SetUpperThreshold(upperThreshold)
48 
50  InputImageType,
51  OutputImageType].New()
52 rescaler.SetInput(cannyFilter.GetOutput())
53 rescaler.SetOutputMinimum(0)
54 rescaler.SetOutputMaximum(255)
55 
56 writer = itk.ImageFileWriter[OutputImageType].New()
57 writer.SetFileName(outputImage)
58 writer.SetInput(rescaler.GetOutput())
59 
60 writer.Update()
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:87
itk::CannyEdgeDetectionImageFilter
This filter is an implementation of a Canny edge detector for scalar-valued images.
Definition: itkCannyEdgeDetectionImageFilter.h:88
itk::RescaleIntensityImageFilter
Applies a linear transformation to the intensity levels of the input Image.
Definition: itkRescaleIntensityImageFilter.h:154
itk::Image
Templated n-dimensional image class.
Definition: itkImage.h:86