ITK  5.2.0
Insight Toolkit
SphinxExamples/src/Core/Common/GetImageSize/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) != 2:
21  print('Usage: ' + sys.argv[0] + ' <inputImageFile>')
22  sys.exit(1)
23 
24 inputImageFile = sys.argv[1]
25 
26 image = itk.imread(inputImageFile, itk.UC)
27 
28 region = image.GetLargestPossibleRegion()
29 size = region.GetSize()
30 
31 print(size)
32 
33 # Equivalently
34 size = itk.size(image)
35 
36 print(size)
37 
38 # Corresponds to the NumPy ndarray shape. Note that the ordering is reversed.
39 print(image.shape)
40 
41 # An example image had w = 200 and h = 100
42 # (it is wider than it is tall). The above output
43 # 200 x 100
44 # so w = GetSize()[0]
45 # and h = GetSize()[1]
46 
47 # A pixel inside the region
48 indexInside = itk.Index[2]()
49 indexInside[0] = 150
50 indexInside[1] = 50
51 print(region.IsInside(indexInside))
52 
53 # A pixel outside the region
54 indexOutside = itk.Index[2]()
55 indexOutside[0] = 50
56 indexOutside[1] = 150
57 print(region.IsInside(indexOutside))
58 
59 # This means that the [0] component of the index is referencing the
60 # left to right (x) value and the [1] component of Index is referencing
61 # the top to bottom (y) value
itk::Index
Represent a n-dimensional index in a n-dimensional image.
Definition: itkIndex.h:66