医学图像预处理----重采样(Resample)

前言

在医学图像中,重采样是指将医疗图像中大小不同的体素归一化到相同的大小。体素是体积元素(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}

  • 3
    点赞
  • 26
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值