SimpleITK  2.0.0
AdvancedImageReading/AdvancedImageReading.py
1 #!/usr/bin/env python
2 # =========================================================================
3 #
4 # Copyright NumFOCUS
5 #
6 # Licensed under the Apache License, Version 2.0 (the "License");
7 # you may not use this file except in compliance with the License.
8 # You may obtain a copy of the License at
9 #
10 # http://www.apache.org/licenses/LICENSE-2.0.txt
11 #
12 # Unless required by applicable law or agreed to in writing, software
13 # distributed under the License is distributed on an "AS IS" BASIS,
14 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 # See the License for the specific language governing permissions and
16 # limitations under the License.
17 #
18 # =========================================================================
19 
20 from __future__ import print_function
21 
22 import sys
23 
24 import SimpleITK as sitk
25 import numpy as np
26 
27 if len(sys.argv) < 2:
28  print('Wrong number of arguments.', file=sys.stderr)
29  print('Usage: ' + __file__ + ' image_file_name', file=sys.stderr)
30  sys.exit(1)
31 
32 # Read image information without reading the bulk data.
33 file_reader = sitk.ImageFileReader()
34 file_reader.SetFileName(sys.argv[1])
35 file_reader.ReadImageInformation()
36 print('image size: {0}\nimage spacing: {1}'.format(file_reader.GetSize(),
37  file_reader.GetSpacing()))
38 # Some files have a rich meta-data dictionary (e.g. DICOM)
39 for key in file_reader.GetMetaDataKeys():
40  print(key + ': ' + file_reader.GetMetaData(key))
41 print('-' * 20)
42 
43 # When low on memory, we can incrementally work on sub-images. The following
44 # subtracts two images (ok, the same image) by reading them as multiple'
45 # sub-images.
46 
47 image1_file_name = sys.argv[1]
48 image2_file_name = sys.argv[1]
49 
50 parts = 5 # Number of sub-regions we use
51 
52 file_reader = sitk.ImageFileReader()
53 file_reader.SetFileName(image1_file_name)
54 file_reader.ReadImageInformation()
55 image_size = file_reader.GetSize()
56 
57 result_img = sitk.Image(file_reader.GetSize(), file_reader.GetPixelID(),
58  file_reader.GetNumberOfComponents())
59 result_img.SetSpacing(file_reader.GetSpacing())
60 result_img.SetOrigin(file_reader.GetOrigin())
61 result_img.SetDirection(file_reader.GetDirection())
62 
63 extract_size = list(file_reader.GetSize())
64 extract_size[-1] = extract_size[-1] // parts
65 current_index = [0] * file_reader.GetDimension()
66 for i in range(parts):
67  if i == (parts - 1):
68  extract_size[-1] = image_size[-1] - current_index[-1]
69  file_reader.SetFileName(image1_file_name)
70  file_reader.SetExtractIndex(current_index)
71  file_reader.SetExtractSize(extract_size)
72  sub_image1 = file_reader.Execute()
73 
74  file_reader.SetFileName(image2_file_name)
75  file_reader.SetExtractIndex(current_index)
76  file_reader.SetExtractSize(extract_size)
77  sub_image2 = file_reader.Execute()
78  idx = [slice(None,None)]*file_reader.GetDimension()
79  idx[-1] = current_index[-1]
80  result_img[idx] = sub_image1 - sub_image2
81  current_index[-1] += extract_size[-1]
82 del sub_image1
83 del sub_image2
84 
85 # Check that our iterative approach is equivalent to reading the whole images.
86 if np.any(sitk.GetArrayViewFromImage(result_img
87  - sitk.ReadImage(image1_file_name)
88  + sitk.ReadImage(image2_file_name))):
89  print('Subtraction error.')
90  sys.exit(1)
91 sys.exit(0)
itk::simple::Image
The Image class for SimpleITK.
Definition: sitkImage.h:75
itk::simple::ImageFileReader
Read an image file and return a SimpleITK Image.
Definition: sitkImageFileReader.h:60
itk::simple::ReadImage
SITKIO_EXPORT Image ReadImage(const std::vector< std::string > &fileNames, PixelIDValueEnum outputPixelType=sitkUnknown, const std::string &imageIO="")
ReadImage is a procedural interface to the ImageSeriesReader class which is convenient for most image...