https://blog.csdn.net/YYY_77/article/details/125547278
导入 SimpleITK
注意大小写
1
| import SimpleITK as sitk
|
图像常见属性
图像大小
获取图像的大小,size 为图像的每一个维度的长度,即每个维度像素点的个数。
在 SimpleITK 中,读取的顺序为先宽度,后高度,再深度,对应的三维医学图像中为:先矢状位,后冠状位,再横断位
图像的原点坐标
读取顺序同 GetSize () 方法
1
| print(image.GetOrigin())
|
体素间距
image.GetSpacing()
获取每个维度上像素或体素之间的间距,单位 mm。
1
| print(image.GetSpacing())
|
图像方向
获取图像的方向,即图像坐标系相对世界坐标系的角度,角度采用的是方向余弦矩阵
1
| print(image.GetDirection())
|
数组互换
在 SimpleITK 中,各术语对应如下
Width: 宽度,X 轴,矢状面(Sagittal)
Height: 高度,Y 轴,冠状面(Coronal)
Depth: 深度, Z 轴,横断面(Axial)
SimpleITK 图像顺序是 x,y,z 三个方向的大小(在第一节中也讲过),而 numpy 矩阵的顺序是 z,y,x 三个方向的大小,要注意索引位置。
图像重采样
重采样目的是将医疗图像中不同的体素归一化到相同的大小,可分为两种方案,方案一:按目标 Spacing 缩放,方案二:按目标 Size 缩放。
这两种方案具体操作分为三个步骤:
- 使用 SimpleITK 读取数据,获得原始图像的 Spacing 以及 Size;
- 如果是方案一,则图像原始 Size 乘以原始 Spacing 除以新 Spacing 得到新 Size,如果是方案二,则图像原始 Size 乘以原始 Spacing 除以新 Size 得到新 Spacing;
- 最后将新 Spacing 和新 Size 赋值到读取的数据即可。
1 2 3 4 5 6
| resampler = sitk.ResampleImageFilter() resampler.SetReferenceImage(itkimage) resampler.SetSize(newSize.tolist()) resampler.SetTransform(sitk.Transform(3, sitk.sitkIdentity)) resampler.SetInterpolator(sitk.sitkNearestNeighbor) itkimgResampled = resampler.Execute(itkimage)
|
下面以指定 Spacing 大小,对原始数据进行重采样,例子如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
| def resampleImage(image, targetSpacing, resamplemethod=sitk.sitkNearestNeighbor): """ 将体数据重采样的指定的spacing大小 paras: image:sitk读取的image信息,这里是体数据 targetSpacing:指定的spacing,例如[1,1,1] resamplemethod:插值类型 return:重采样后的数据 """ targetsize = [0, 0, 0] ori_size = image.GetSize() ori_spacing = image.GetSpacing() transform = sitk.Transform() transform.SetIdentity() targetsize[0] = round(ori_size[0] *ori_spacing[0] / targetsize[0]) targetsize[1] = round(ori_size[1]* ori_spacing[1] / targetsize[1]) targetsize[2] = round(ori_size[2] * ori_spacing[2] / targetsize[2]) resampler = sitk.ResampleImageFilter() resampler.SetTransform(transform) resampler.SetSize(targetsize) resampler.SetOutputOrigin(image.GetOrigin()) resampler.SetOutputSpacing(targetSpacing) resampler.SetOutputDirection(image.GetDirection()) resampler.SetInterpolator(resamplemethod) if resamplemethod=sitk.sitkNearestNeighbor: resampler.SetOutputPixelType(sitk.sitkUInt8) else: resampler.SetOutputPixelType(sitk.sitkFloat32) newImage = resampler.Execute(image) return newImage
|