前言:
在医学图像中,重采样是指将医疗图像中大小不同的体素归一化到相同的大小。体素是体积元素(Volume Pixel)的简称,一张3D医学图像可以看成是由若干个体素构成的,体素是一张3D医疗图像在空间上的最小单元。
重采样过程:
对医学图像中体素的理解。Spacing(0.7422, 0.7422, 8.0)表示的是原始图像体素的大小,也可以将Spacing想象成大小为(0.7422, 0.7422, 8.0)的长方体。而原始图像的Size为 (512, 512, 22),表示的是原始在X轴,Y轴,Z轴中体素的个数。原始图像的大小对应的Spacing既可以得到真实3D图像大小(512*0.7422,512*0.7422,8*22 ),在图像重采样只是修改体素的大小,而真实3D图像大小是保持不变的,因此假设我们将Spacing修改成(1.4844, 1.4844, 2.75)的时候,则修改之后其对应的size应该为((512*0.7422)/ 1.4844,(512*0.7422)/ 1.4844,(22*8)/2.75)即(256, 256, 64)
代码展示:
class Resample(object):
"""
Resample the volume in a sample to a given voxel size
Args:
voxel_size (float or tuple): Desired output size.
If float, output volume is isotropic.
If tuple, output voxel size is matched with voxel size
Currently only support linear interpolation method线性插值方法
"""
def __init__(self, voxel_size):
self.name = 'Resample'
assert isinstance(voxel_size, (float, tuple, list))
if isinstance(voxel_size, float):
self.voxel_size = (voxel_size, voxel_size, voxel_size)
else:
assert len(voxel_size) == 3
self.voxel_size = voxel_size
def __call__(self, sample):
image, label = sample['image'], sample['label']
for image_channel in range(len(image)):
old_spacing = image[image_channel].GetSpacing()
old_size = image[image_channel].GetSize()
new_spacing = self.voxel_size
new_size = []
for i in range(3):
new_size.append(int(math.ceil(old_spacing[i]*old_size[i]/new_spacing[i])))
new_size = tuple(new_size)
resampler = sitk.ResampleImageFilter()
resampler.SetInterpolator(sitk.sitkLinear)#图像线性插值
resampler.SetOutputSpacing(new_spacing)
resampler.SetSize(new_size)
# resample on image
resampler.SetOutputOrigin(image[image_channel].GetOrigin())
resampler.SetOutputDirection(image[image_channel].GetDirection())
# print("Resampling image...")
image[image_channel] = resampler.Execute(image[image_channel])
# resample on segmentation
resampler.SetInterpolator(sitk.sitkNearestNeighbor)#标签最近邻插值
resampler.SetOutputOrigin(label.GetOrigin())
resampler.SetOutputDirection(label.GetDirection())
# print("Resampling segmentation...")
label = resampler.Execute(label)
return {'image': image, 'label': label}