ITK  5.4.0
Insight Toolkit
SphinxExamples/src/Filtering/BinaryMathematicalMorphology/ThinImage/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 import itk
18 import argparse
19 
20 parser = argparse.ArgumentParser(description="Thin Image.")
21 parser.add_argument("input_image", nargs="?")
22 args = parser.parse_args()
23 
24 PixelType = itk.UC
25 Dimension = 2
26 ImageType = itk.Image[PixelType, Dimension]
27 
28 if args.input_image:
29  image = itk.imread(args.input_image)
30 
31 else:
32  # Create an image
33  start = itk.Index[Dimension]()
34  start.Fill(0)
35 
36  size = itk.Size[Dimension]()
37  size.Fill(100)
38 
39  region = itk.ImageRegion[Dimension]()
40  region.SetIndex(start)
41  region.SetSize(size)
42 
43  image = ImageType.New(Regions=region)
44  image.Allocate()
45  image.FillBuffer(0)
46 
47  # Draw a 5 pixel wide line
48  image[50:55, 20:80] = 255
49 
50  # Write Image
51  itk.imwrite(image, "Input.png")
52 
53 image = itk.binary_thinning_image_filter(image)
54 
55 # Rescale the image so that it can be seen (the output is 0 and 1, we want 0 and 255)
56 image = itk.rescale_intensity_image_filter(image, output_minimum=0, output_maximum=255)
57 
58 # Write Image
59 itk.imwrite(image, "OutputPython.png")
itk::Index
Represent a n-dimensional index in a n-dimensional image.
Definition: itkIndex.h:70
itk::Size
Represent a n-dimensional size (bounds) of a n-dimensional image.
Definition: itkSize.h:71
itk::ImageRegion
An image region represents a structured region of data.
Definition: itkImageRegion.h:80
itk::Image
Templated n-dimensional image class.
Definition: itkImage.h:88