4

I'm using SimpleITK to read MetaImage data.

Sometimes I need to access only the metadata (which is stored in a key=value .mhd file) but the only way I found to do it is to call ReadImage which is pretty slow as it loads the whole array into memory.

import SimpleITK as sitk

mhd = sitk.ReadImage(filename)
origin = mhd.GetOrigin()
spacing = mhd.GetSpacing()
direction = mhd.GetDirection()

Is there a way to access origin spacing and direction without loading the full image?

filippo
  • 5,197
  • 2
  • 21
  • 44

1 Answers1

7

ITK itself does support this feature, but SimpleITK does not.

Please create a feature request with the project: https://github.com/SimpleITK/SimpleITK/issues

UPDATE:

This new feature has been added to the SimpleITK master branch for the 1.1 release.

Here is an example of the new interface:

if len ( sys.argv ) < 2:
    print( "Usage: DicomImagePrintTags <input_file>" )
    sys.exit ( 1 )

reader = sitk.ImageFileReader()

reader.SetFileName( sys.argv[1] )
reader.LoadPrivateTagsOn();

reader.ReadImageInformation();

for k in reader.GetMetaDataKeys():
    v = reader.GetMetaData(k)
    print("({0}) = = \"{1}\"".format(k,v))

print("Image Size: {0}".format(reader.GetSize()));
print("Image PixelType: {0}".format(sitk.GetPixelIDValueAsString(reader.GetPixelID())));
blowekamp
  • 1,401
  • 7
  • 7
  • 1
    For reference the issue has been created here: https://github.com/SimpleITK/SimpleITK/issues/356 – blowekamp Dec 21 '17 at 15:03
  • Right, sorry, I forgot to update the question here. I'd say let's keep the question open and update it when the feature will be merged in SimpleITK. – filippo Dec 22 '17 at 04:40