我只想从.mha文件中读取标题。理想情况下,它会给我同样的感觉,就像只使用元数据读取.mhd文件一样。
我正在使用SimpleITK进行阅读,虽然它可以很好地读取图像,但我特别希望创建一个元数据字典。
.mha基本上是带有原始映像的.mhd,但是我找不到任何关于如何将它们分开的规范。我在ITK docs 这里中找到的
若要跳过图像数据文件中的头字节,请使用
HeaderSize = X其中X是读取图像数据之前在文件开头要跳过的字节数。如果您知道没有尾随字节(文件末尾的额外字节),则可以指定HeaderSize = -1MetaImage将自动计算数据文件中提取的字节数,假设这些字节位于数据文件的开头,并在开始读取图像数据之前自动跳过这些字节。
但是我在SimpleITK中找不到这个功能。
发布于 2021-04-28 18:33:45
有一个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}'))https://stackoverflow.com/questions/67009389
复制相似问题