在python中读取* .mhd / *。raw格式

时间:2016-05-18 04:57:49

标签: python image-processing computer-vision

有人可以告诉我在python中读取包含 .mhd / .raw文件的数据集的方法吗?

4 个答案:

答案 0 :(得分:14)

最简单的方法是使用SimpleITK(MedPy也将ITK用于.mhd / .raw文件)。命令

pip install SimpleITK

适用于许多python版本。对于阅读.mhd / .raw,您可以使用此代码from kaggle

import SimpleITK as sitk
import numpy as np
'''
This funciton reads a '.mhd' file using SimpleITK and return the image array, origin and spacing of the image.
'''

def load_itk(filename):
    # Reads the image using SimpleITK
    itkimage = sitk.ReadImage(filename)

    # Convert the image to a  numpy array first and then shuffle the dimensions to get axis in the order z,y,x
    ct_scan = sitk.GetArrayFromImage(itkimage)

    # Read the origin of the ct_scan, will be used to convert the coordinates from world to voxel and vice versa.
    origin = np.array(list(reversed(itkimage.GetOrigin())))

    # Read the spacing along each dimension
    spacing = np.array(list(reversed(itkimage.GetSpacing())))

    return ct_scan, origin, spacing

答案 1 :(得分:9)

安装SimpleITK后,使用skimage可能会更容易

import skimage.io as io
img = io.imread('file.mhd', plugin='simpleitk')

这将为您提供z,y,x排序的numpy数组。

答案 2 :(得分:2)

在上述帖子上,您可以从here下载CT-Scan .mhd文件开始,并使用以下代码显示/保存29张图像(假设您同时拥有标头和原始文件下载到当前目录中):

import SimpleITK as sitk
import matplotlib.pylab as plt
ct_scans = sitk.GetArrayFromImage(sitk.ReadImage("training_001_ct.mhd", sitk.sitkFloat32))
plt.figure(figsize=(20,16))
plt.gray()
plt.subplots_adjust(0,0,1,1,0.01,0.01)
for i in range(ct_scans.shape[0]):
    plt.subplot(5,6,i+1), plt.imshow(ct_scans[i]), plt.axis('off')
    # use plt.savefig(...) here if you want to save the images as .jpg, e.g.,
plt.show()

enter image description here

以下是与SimpleITK一起读取并经过动画处理的相同的CT扫描.mhd文件: enter image description here

答案 3 :(得分:0)

您可以尝试使用MedPy或此mhd_utils script