ITK  5.4.0
Insight Toolkit
SphinxExamples/src/Core/Common/SetPixelValueInOneImage/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="Set Pixel Value In One Image.")
21 parser.add_argument("output_image")
22 args = parser.parse_args()
23 
24 Dimension = 2
25 
26 PixelType = itk.UC
27 
28 ImageType = itk.Image[PixelType, Dimension]
29 
30 region = itk.ImageRegion[Dimension]()
31 
32 start = itk.Index[Dimension]()
33 start.Fill(0)
34 
35 region.SetIndex(start)
36 
37 size = itk.Size[Dimension]()
38 size[0] = 200
39 size[1] = 300
40 
41 region.SetSize(size)
42 
43 image = ImageType.New()
44 image.SetRegions(region)
45 image.Allocate()
46 image.FillBuffer(itk.NumericTraits[PixelType].ZeroValue())
47 
48 pixelIndex = itk.Index[Dimension]()
49 
50 for r in range(50):
51  pixelIndex[0] = 4 * r
52  pixelIndex[1] = 4 * r
53 
54  image.SetPixel(pixelIndex, 128)
55 
56  pixelIndex[0] = 4 * r
57  pixelIndex[1] = 200 - 4 * r
58 
59  image.SetPixel(pixelIndex, 255)
60 
61 for r in range(25):
62  pixelIndex[0] = 8 * r
63  pixelIndex[1] = 200 + 4 * r
64 
65  image.SetPixel(pixelIndex, 128)
66 
67  pixelIndex[0] = 8 * r
68  pixelIndex[1] = 250
69 
70  image.SetPixel(pixelIndex, 180)
71 
72 pixelIndex[0] = 95
73 pixelIndex[1] = 150
74 
75 image.SetPixel(pixelIndex, 200)
76 
77 pixelIndex[0] = 100
78 pixelIndex[1] = 150
79 
80 image.SetPixel(pixelIndex, 200)
81 
82 pixelIndex[0] = 105
83 pixelIndex[1] = 150
84 
85 image.SetPixel(pixelIndex, 200)
86 
87 pixelIndex[0] = 100
88 pixelIndex[1] = 155
89 
90 image.SetPixel(pixelIndex, 200)
91 
92 pixelIndex[0] = 100
93 pixelIndex[1] = 145
94 
95 image.SetPixel(pixelIndex, 200)
96 
97 itk.imwrite(image, args.output_image)
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::NumericTraits
Define additional traits for native types such as int or float.
Definition: itkNumericTraits.h:59
itk::Image
Templated n-dimensional image class.
Definition: itkImage.h:88