这个表达式看起来就是点的平移公式。
1 # -*- coding : UTF-8 -*-
2 # @file : resample_change_direction.py
3 # @Time : 2022-02-14 16:19
4 # @Author : wmz
5 import os
6 import numpy as np
7 import SimpleITK as sitk
8
9
10 def getFiles(path, suffix):
11 return [os.path.join(root, file) for root, dirs, files in os.walk(path) for file in files if file.endswith(suffix)]
12
13
14 def resampleVolume(outspacing, vol):
15 """
16 将体数据重采样的指定的spacing大小\n
17 paras:
18 outpacing:指定的spacing,例如[1,1,1]
19 vol:sitk读取的image信息,这里是体数据\n
20 return:重采样后的数据
21 """
22 outsize = [0, 0, 0]
23 # 读取文件的size和spacing信息
24 inputsize = vol.GetSize()
25 inputspacing = vol.GetSpacing()
26
27 transform = sitk.Transform()
28 transform.SetIdentity()
29 # 计算改变spacing后的size,用物理尺寸/体素的大小
30 outsize[0] = round(inputsize[0] * inputspacing[0] / outspacing[0])
31 outsize[1] = round(inputsize[1] * inputspacing[1] / outspacing[1])
32 outsize[2] = round(inputsize[2] * inputspacing[2] / outspacing[2])
33
34 # 设定重采样的一些参数
35 resampler = sitk.ResampleImageFilter()
36 resampler.SetTransform(transform)
37 resampler.SetInterpolator(sitk.sitkLinear)
38 resampler.SetOutputOrigin(vol.GetOrigin())
39 resampler.SetOutputSpacing(outspacing)
40 resampler.SetOutputDirection(vol.GetDirection())
41 resampler.SetSize(outsize)
42 newvol = resampler.Execute(vol)
43 return newvol
44
45
46 def resample_direction(itk_img, direction=tuple([1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0])):
47 """
48 将体数据重采样的指定的spacing大小\n
49 paras:
50 outpacing:指定的spacing,例如[1,1,1]
51 vol:sitk读取的image信息,这里是体数据\n
52 return:重采样后的数据
53 """
54 outsize = [0, 0, 0]
55 # 读取文件的size和spacing信息
56 img_size = itk_img.GetSize()
57 spacing = itk_img.GetSpacing()
58
59 transform = sitk.Transform()
60 transform.SetIdentity()
61 # 计算改变spacing后的size,用物理尺寸/体素的大小
62 outsize[0] = round(img_size[0])
63 outsize[1] = round(img_size[1])
64 outsize[2] = round(img_size[2])
65 origin = itk_img.GetOrigin()
66 new_origin = tuple([origin[0] - spacing[0] * img_size[0], origin[1] - spacing[1] * img_size[1], origin[2]])
67
68 # 设定重采样的一些参数
69 resampler = sitk.ResampleImageFilter()
70 # resampler.SetReferenceImage(itk_img)
71 resampler.SetTransform(transform)
72 resampler.SetInterpolator(sitk.sitkLinear)
73 resampler.SetSize(outsize)
74 resampler.SetOutputOrigin(new_origin)
75 resampler.SetOutputDirection(direction)
76 resampler.SetOutputSpacing(spacing)
77 new_img = resampler.Execute(itk_img)
78 out_arr = sitk.GetArrayFromImage(new_img)
79 return new_img
80
81
82 def resample_image(itk_image, out_spacing,out_direction, is_label=False):
83 original_size = itk_image.GetSize()
84 original_spacing = itk_image.GetSpacing()
85 out_size = [
86 int(np.round(original_size[0] * (original_spacing[0] / out_spacing[0]))),
87 int(np.round(original_size[1] * (original_spacing[1] / out_spacing[1]))),
88 int(np.round(original_size[2] * (original_spacing[2] / out_spacing[2])))
89 ]
90 resample = sitk.ResampleImageFilter()
91 resample.SetOutputSpacing(out_spacing)
92 resample.SetSize(out_size)
93 resample.SetOutputDirection(out_direction)
94 resample.SetOutputOrigin(itk_image.GetOrigin())
95
96
97 if is_label:
98 resample.SetDefaultPixelValue(0) # 没有图像的地方填充值
99 resample.SetInterpolator(sitk.sitkNearestNeighbor)
100 else:
101 resample.SetDefaultPixelValue(-10) # -10是我调整的窗宽窗外
102 resample.SetInterpolator(sitk.sitkBSpline)
103
104 return resample.Execute(itk_image)
105
106
107 if __name__ == '__main__':
108 img_fath = r"D:\Dataset\landmark\Data_train_scaled_2"
109 filelist = getFiles(img_fath, "nii.gz")
110 dst_path = r"D:\Dataset\landmark\resample_direction_train"
111
112 for imgfile in filelist:
113 org_img = sitk.Image(sitk.ReadImage(imgfile))
114 direct = org_img.GetDirection()
115 direction = tuple([1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0])
116 spacing = org_img.GetSpacing()
117 img_size = org_img.GetSize()
118 img_arr = sitk.GetArrayFromImage(org_img)
119 out_img = resample_direction(org_img, direction)
120 # out_img = resample_image(org_img, spacing, direction)
121 # out_img = sitk.GetImageFromArray(sitk.GetArrayFromImage(org_img))
122 # out_arr = sitk.GetArrayFromImage(out_img)
123 # out_arr = np.flip(out_arr, [1, 2])
124 # out_img = sitk.GetImageFromArray(out_arr)
125 # # setup other image characteristics
126 # origin = org_img.GetOrigin()
127 # new_origin = tuple([origin[0] - spacing[0] * img_size[0], origin[1] - spacing[1] * img_size[1], origin[2]])
128 # # out_img.SetOrigin(org_img.GetOrigin())
129 # out_img.SetOrigin(new_origin)
130 # out_img.SetSpacing(org_img.GetSpacing())
131 # set to RAI
132 out_img.SetDirection(tuple([1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0]))
133
134 out_arr = sitk.GetArrayFromImage(out_img)
135 dst_file = os.path.join(dst_path, imgfile.rsplit("\\")[-1])
136 sitk.WriteImage(out_img, dst_file)
137 print("saved file: ", dst_file)