如何解决从 .mha 文件中读取头信息
我只想从 .mha
文件中读取标题。理想情况下,它会给我带来与仅使用元数据读取 .mhd
文件一样的效果。
我正在使用 SimpleITK 进行阅读,虽然它可以很好地读取图像,但我特别想创建一个元数据字典。.mha
基本上是 .mhd
与原始图像相结合,但我找不到关于如何分离它们的任何规范。我在 ITK 文档中找到 here:
要跳过图像数据文件中的头字节,请使用HeaderSize = X
其中 X 是读取图像数据之前在文件开头跳过的字节数。如果您知道没有尾随字节(文件末尾的额外字节),您可以指定HeaderSize = -1
MetaImage 会自动计算数据文件中提取的字节数,假设这些字节在数据文件的头部,并在开始读取图像数据之前自动跳过它们。
但我在 SimpleITK 中找不到这个功能。
解决方法
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}'))
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。