Updating only header information for MetaImage/NRRD

I’m using MetaImage and NRRD file types with custom header fields to store additional information about my volume. I am currently interested in updating the values of these custom fields, but I have only had success with writing the updated header field which also rewrites the image data.

I’ve currently been using SimpleITK to update the fields as I was unable to find an option in Slicer to save the custom fields.

import SimpleITK as sitk
image = sitk.ReadImage("path/to/image.mhd")
image.SetMetaData("MyCustomField", "MyCustomValue")
writer = sitk.ImageFileWriter()
writer.SetFileName("same/path/to/image.mhd")
writer.Execute(image)  # also writes out the image raw file

I understand that likely only updating the header is possible when using split file formats like mhd+raw or nhdr+raw. Or is there a method with MHA and NRRD that can update the header fields and shift the bits so not to write the image data again?

Does anyone know of a method of only updating a field for the header file without the obvious opening of the file manually and writing it as simple text.

I don’t know any file IO API that would support shift operation. A common technique to work around this limitation is to write a placeholder field with the maximum allowed length (e.g., some value with lots of padding spaces) and overwrite that field when the final length is known. Maybe the segmentation storage file format discussion today at 11am EST would be interesting for you.

Being that these images are captured using PlusToolkit I have also tried the EditSequenceFile command line tool for updating the custom header field however it also writes the image data file (RAW) or at least I notice that the image data file has a new “Date Modified” time which makes me think it has been written down again.

EditSequenceFile.exe
--source-seq-file="C:\Users\JamesButler\Documents\FusedVolume.mhd"
--output-seq-file="C:\Users\JamesButler\Documents\FusedVolume.mhd"
--operation=UPDATE_FIELD_VALUE --field-name=MyCustomHeaderFieldn --updated-field-value=MyCustomValue
--maintain-custom-headers MyCustomHeaderField

Maybe all these methods SimpleITK/EditSequenceFile all depend on the same ITK writer which doesn’t currently support writing just the header file?

We had to reimplement sequence file writers to allow streamed compressed writing, so we don’t rely on what ITK writer can do (for mha for sure, but I think for nrrd as well). If it currently rewrites the RAW file then it should not be hard to fix it.

Those custom implementations are in IGSIO now.

Updates created:

and a related bug identified: