有一个 SimpleITK 功能允许您执行此操作,该功能通常用于 DICOM 和 nifti 文件格式。我用它来读取 MHD 文件而不将原始数据拉入内存:
import pathlib
import SimpleITK as sitk
#Use pathlib so it works on whatever OS
mha_dir = pathlib.Path("/you/files/location")
mha_file = str(mha_dir.joinpath(mha_file))
#Set up the reader and get the file information
reader = sitk.ImageFileReader()
reader.SetFileName(mha_file) # Give it the mha file as a string
reader.LoadPrivateTagsOn() # Make sure it can get all the info
reader.ReadImageInformation() # Get just the information from the file
# From here you can just parse out whatever you want, just like a SimpleITK image
xdim, ydim, zdim = reader.GetSize() # If you want the x, y, z
xres, yres, zres = reader.GetSpacing() # If you want the image resolution, etc.
meta_keys = reader.GetMetaDataKeys()
for key in meta_keys:
print(key)
print(reader.GetMetaData(f'{key}'))