SimpleITK  1.2.4
AdvancedImageReading/AdvancedImageReading.py
1 #!/usr/bin/env python
2 #=========================================================================
3 #
4 # Copyright Insight Software Consortium
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 import numpy as np
24 import SimpleITK as sitk
25 
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(), file_reader.GetSpacing()))
37 # Some files have a rich meta-data dictionary (e.g. DICOM)
38 for key in file_reader.GetMetaDataKeys():
39  print(key + ': ' + file_reader.GetMetaData(key))
40 print('-'*20)
41 
42 # When low on memory, we can incrementally work on sub-images. The following
43 # subtracts two images (ok, the same image) by reading them as multiple sub-images.
44 
45 image1_file_name = sys.argv[1]
46 image2_file_name = sys.argv[1]
47 
48 parts = 5 # Number of sub-regions we use
49 
50 file_reader = sitk.ImageFileReader()
51 file_reader.SetFileName(image1_file_name)
52 file_reader.ReadImageInformation()
53 image_size = file_reader.GetSize()
54 
55 result_img = sitk.Image(file_reader.GetSize(), file_reader.GetPixelID(), file_reader.GetNumberOfComponents())
56 result_img.SetSpacing(file_reader.GetSpacing())
57 result_img.SetOrigin(file_reader.GetOrigin())
58 result_img.SetDirection(file_reader.GetDirection())
59 
60 extract_size = list(file_reader.GetSize())
61 extract_size[-1] = extract_size[-1]//parts
62 current_index = [0]*file_reader.GetDimension()
63 for i in range(parts):
64  if i == (parts-1):
65  extract_size[-1] = image_size[-1] - current_index[-1]
66  file_reader.SetFileName(image1_file_name)
67  file_reader.SetExtractIndex(current_index)
68  file_reader.SetExtractSize(extract_size)
69  sub_image1 = file_reader.Execute()
70 
71  file_reader.SetFileName(image2_file_name)
72  file_reader.SetExtractIndex(current_index)
73  file_reader.SetExtractSize(extract_size)
74  sub_image2 = file_reader.Execute()
75  result_img = sitk.Paste(result_img, sub_image1 - sub_image2, extract_size, [0]*file_reader.GetDimension(), current_index)
76  current_index[-1] += extract_size[-1]
77 del sub_image1
78 del sub_image2
79 
80 # Check that our iterative approach is equivalent to reading the whole images.
81 if np.any(sitk.GetArrayViewFromImage(result_img - sitk.ReadImage(image1_file_name) + sitk.ReadImage(image2_file_name))):
82  print('Subtraction error.')
83  sys.exit(1)
84 sys.exit(0)