20 """ An SimpleITK example demonstrating advanced image reading techniques. """
24 import SimpleITK
as sitk
28 print(
"Wrong number of arguments.", file=sys.stderr)
29 print(
"Usage: " + __file__ +
" image_file_name", file=sys.stderr)
34 file_reader.SetFileName(sys.argv[1])
35 file_reader.ReadImageInformation()
36 print(f
"image size: {file_reader.GetSize()}")
37 print(f
"image spacing: {file_reader.GetSpacing()}")
40 for key
in file_reader.GetMetaDataKeys():
41 print(key +
": " + file_reader.GetMetaData(key))
48 image1_file_name = sys.argv[1]
49 image2_file_name = sys.argv[1]
54 file_reader.SetFileName(image1_file_name)
55 file_reader.ReadImageInformation()
56 image_size = file_reader.GetSize()
59 file_reader.GetSize(),
60 file_reader.GetPixelID(),
61 file_reader.GetNumberOfComponents(),
63 result_img.SetSpacing(file_reader.GetSpacing())
64 result_img.SetOrigin(file_reader.GetOrigin())
65 result_img.SetDirection(file_reader.GetDirection())
67 extract_size = list(file_reader.GetSize())
68 extract_size[-1] = extract_size[-1] // parts
69 current_index = [0] * file_reader.GetDimension()
70 for i
in range(parts):
72 extract_size[-1] = image_size[-1] - current_index[-1]
73 file_reader.SetFileName(image1_file_name)
74 file_reader.SetExtractIndex(current_index)
75 file_reader.SetExtractSize(extract_size)
76 sub_image1 = file_reader.Execute()
78 file_reader.SetFileName(image2_file_name)
79 file_reader.SetExtractIndex(current_index)
80 file_reader.SetExtractSize(extract_size)
81 sub_image2 = file_reader.Execute()
82 idx = [slice(
None,
None)] * file_reader.GetDimension()
83 idx[-1] = current_index[-1]
84 result_img[idx] = sub_image1 - sub_image2
85 current_index[-1] += extract_size[-1]
91 sitk.GetArrayViewFromImage(
95 print(
"Subtraction error.")