Search code examples
pythonmedicalsimpleitk

SimpleITK, read metadata without loading image array


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?


Solution

  • 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())));