ITK  6.0.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 # 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 numpy as np
18 import itk
19 import argparse
20 
21 parser = argparse.ArgumentParser(description="Get Image Size.")
22 parser.add_argument("input_image")
23 args = parser.parse_args()
24 
25 image = itk.imread(args.input_image, itk.UC)
26 
27 region = image.GetLargestPossibleRegion()
28 size = region.GetSize()
29 
30 print(size)
31 
32 # Equivalently
33 size = itk.size(image)
34 
35 print(size)
36 
37 # Corresponds to the NumPy ndarray shape. Note that the ordering is reversed.
38 print(np.asarray(image).shape)
39 
40 # An example image had w = 200 and h = 100
41 # (it is wider than it is tall). The above output
42 # 200 x 100
43 # so w = GetSize()[0]
44 # and h = GetSize()[1]
45 
46 # A pixel inside the region
47 indexInside = itk.Index[2]()
48 indexInside[0] = 150
49 indexInside[1] = 50
50 print(region.IsInside(indexInside))
51 
52 # A pixel outside the region
53 indexOutside = itk.Index[2]()
54 indexOutside[0] = 50
55 indexOutside[1] = 150
56 print(region.IsInside(indexOutside))
57 
58 # This means that the [0] component of the index is referencing the
59 # left to right (x) value and the [1] component of Index is referencing
60 # the top to bottom (y) value
itk::Index
Represent a n-dimensional index in a n-dimensional image.
Definition: itkIndex.h:68