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(), file_reader.GetSpacing()))
38 for key
in file_reader.GetMetaDataKeys():
39 print(key +
': ' + file_reader.GetMetaData(key))
45 image1_file_name = sys.argv[1]
46 image2_file_name = sys.argv[1]
51 file_reader.SetFileName(image1_file_name)
52 file_reader.ReadImageInformation()
53 image_size = file_reader.GetSize()
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())
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):
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()
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]
82 print(
'Subtraction error.')