gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-29 16:53:16 28 4
gpt4 key购买 nike

谁能告诉我如何在 python 中读取包含 .mhd/.raw 文件的数据集?

最佳答案

最简单的方法是使用 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

关于python - 在 python 中读取 *.mhd/*.raw 格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37290631/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com