20 from __future__
import print_function
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(
'image size: {0}\nimage spacing: {1}'.format(file_reader.GetSize(),
37 file_reader.GetSpacing()))
39 for key
in file_reader.GetMetaDataKeys():
40 print(key +
': ' + file_reader.GetMetaData(key))
47 image1_file_name = sys.argv[1]
48 image2_file_name = sys.argv[1]
53 file_reader.SetFileName(image1_file_name)
54 file_reader.ReadImageInformation()
55 image_size = file_reader.GetSize()
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())
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):
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()
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]
86 if np.any(sitk.GetArrayViewFromImage(result_img
89 print(
'Subtraction error.')