如何解决SimpleITK - 冠状/矢状视图大小问题
我正在尝试使用 SimpleItk 库从 DICOM 格式的 CTA 中提取所有三个视图(轴向、矢状和冠状)。
我可以从给定目录中正确读取该系列:
...
import SimpleITK as sitk
...
reader = sitk.ImageSeriesReader()
dicom_names = reader.GetGDCMSeriesFileNames(input_dir)
reader.SetFileNames(dicom_names)
# Execute the reader
image = reader.Execute()
...
然后,使用 this questions 中所述的 numpy 数组,我能够提取并保存 3 个视图。
...
image_array = sitk.GetArrayFromImage(image)
...
for i in range(image_array.shape[0]):
output_file_name = axial_out_dir + 'axial_' + str(i) + '.png'
logging.debug('Saving image to ' + output_file_name)
imageio.imwrite(output_file_name,convert_img(image_array[i,:,:],axial_min,axial_max),format='png')
...
另外两个是通过保存image_array[:,i,:]
和image_array[:,i]
得到的,而convert_img(..)
是一个只转换数据类型的函数,所以它不会改变任何形状。
然而,冠状和矢状视图被拉伸、旋转并带有宽黑带(在某些切片中它们非常宽)。
这是来自 Slicer3d 的屏幕截图:
轴向
矢状面
冠状
图像形状为 512x512x1723,这导致轴向 png 为 512x512 像素,冠状和矢状为 512x1723,因此这似乎是正确的。
我应该尝试使用 PermuteAxes 过滤器吗?问题是我找不到关于它在 python 中使用的任何文档(由于文档页面中的 404,没有其他语言)
还有提高对比度的方法吗?我使用了 simpleitk 的 AdaptiveHistogramEqualization 过滤器,但它比 Slicer3D 可视化效果差得多,除了速度非常慢。
感谢任何帮助,谢谢!
解决方法
当您将 SimpleITK 图像转换为 NumPy 数组时,所有像素间距信息都将丢失(如上面的评论所示)。如果您在 SimpleITK 中执行所有操作,它会保留该间距信息。
使用 python 的数组切片从 SimpleITK 中的图像中提取 X、Y 和 Z 中的切片非常容易:
import SimpleITK as sitk
# a blank test image
img = sitk.Image([100,101,102],sitk.sitkUInt8)
# non-uniform spacing,for illustration
img.SetSpacing([1.0,1.1,1.2])
# select the 42nd Z slice
zimg = img[:,:,42]
#select the 0th X slice
ximg = img[0,:]
#select the 100th Y slice
yimg = img[:,100,:]
#print the spacing to show it's retained
print(yimg.GetSpacing())
,
如果有人需要,请回答我自己的问题。
鉴于我需要在深度学习框架中使用切片并进行数据增强,我需要以新的间距对它们重新采样,即 (1.0,1.0,1.0)
。
用这个函数解决了:
def resample_image(itk_image,out_spacing=(1.0,1.0)):
"""
Resample itk_image to new out_spacing
:param itk_image: the input image
:param out_spacing: the desired spacing
:return: the resampled image
"""
# get original spacing and size
original_spacing = itk_image.GetSpacing()
original_size = itk_image.GetSize()
# calculate new size
out_size = [
int(np.round(original_size[0] * (original_spacing[0] / out_spacing[0]))),int(np.round(original_size[1] * (original_spacing[1] / out_spacing[1]))),int(np.round(original_size[2] * (original_spacing[2] / out_spacing[2])))
]
# instantiate resample filter with properties and execute it
resample = sitk.ResampleImageFilter()
resample.SetOutputSpacing(out_spacing)
resample.SetSize(out_size)
resample.SetOutputDirection(itk_image.GetDirection())
resample.SetOutputOrigin(itk_image.GetOrigin())
resample.SetTransform(sitk.Transform())
resample.SetDefaultPixelValue(itk_image.GetPixelIDValue())
resample.SetInterpolator(sitk.sitkNearestNeighbor)
return resample.Execute(itk_image)
然后使用原始问题中所述的 numpy arrays
进行保存。
我可能会迟到,但您可以使用 Torchio。我认为适合您的情况的一个很好的解决方案是使用与 TorchIO 一起安装的 CLI 工具:
$ tiohd your_image.nii.gz
ScalarImage(shape: (1,512,1723); spacing: (0.50,0.50,1.00); orientation: RAS+; memory: 1.7 GiB; dtype: torch.ShortTensor)
$ torchio-transform your_image.nii.gz Resample one_iso.nii.gz
$ tiohd one_iso.nii.gz
ScalarImage(shape: (1,256,1723); spacing: (1.00,1.00,1.00); orientation: RAS+; memory: 430.8 MiB; dtype: torch.ShortTensor)
这是可行的,因为 1 毫米是 Resample
变换的默认目标分辨率。
当然,您也可以使用 TorchIO 的普通 Python 接口来操作您的图像。
免责声明:我是 TorchIO 的主要开发者。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。