0

我只想从.mha文件中读取标题。理想情况下,它会给我.mhd与仅读取带有元数据的文件相同的效果。
我正在使用 SimpleITK 进行阅读,虽然它可以很好地读取图像,但我特别想创建一个元数据字典。
.mha基本上是.mhd与原始图像相结合,但我找不到任何关于如何分离它们的规范。我在这里的 ITK 文档中找到:

要跳过图像数据文件中的标头字节,请使用
HeaderSize = X
其中 X 是在读取图像数据之前要在文件开头跳过的字节数。如果您知道没有尾随字节(文件末尾的额外字节),您可以指定
HeaderSize = -1
MetaImage 将自动计算数据文件中提取字节的数量,假设这些字节位于数据文件的开头,并且在开始读取图像数据之前自动跳过它们。

但我在 SimpleITK 中找不到这个功能。

4

1 回答 1

1

有一个 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}'))


于 2021-04-28T18:33:45.867 回答