Adding or populate header in nrrd or seg.nrrd

Is there any interface to add a custom header to a nrrd from Slicer, i.e: quality:=incomplete, or populate a predefined existing header to a Segmentation image (seg.nrrd)?

If not with GUI, with slicer python would be ok too.
As a reference on how is done in ITK: How to write a custom Tag to an image (NRRD or other) header - Beginner Questions - ITK

I would appreciate any insight/suggestion. Thanks!

Each segment has a state (not started, in progress, completed, flagged) that you can see and modify using the GUI and also saved in the .seg.nrrd file.

If this is not sufficient then you can add arbitrary number of custom properties (tag:value pairs) to any segment using Python scripting (using vtkSegment::SetTag method). These custom tags are also stored in the .seg.nrrd file.

You can read all these states and other properties in .seg.nrrd files using slicerio Python package (in any Python environment, not just in Slicer).

1 Like

For completion, this is the way to do it with proper itk.

    import itk
    meta_dict = my_image.GetMetaDataDictionary()
    meta_dict.Set("Segment0_Name", itk.MetaDataObject[str].New(MetaDataObjectValue="horse"))
    meta_dict.Set("Segment0_ID", itk.MetaDataObject[str].New(MetaDataObjectValue="Segment_1"))
    meta_dict.Set("Segment0_Color", itk.MetaDataObject[str].New(MetaDataObjectValue="0.5300 0.6700 0.3700")) # Greenish
    meta_dict.Set("Segment0_LabelValue", itk.MetaDataObject[str].New(MetaDataObjectValue="1"))
    meta_dict.Set("Segment0_Layer", itk.MetaDataObject[str].New(MetaDataObjectValue="0"))

    meta_dict.Set("Segment1_Name", itk.MetaDataObject[str].New(MetaDataObjectValue="dog"))
    meta_dict.Set("Segment1_ID", itk.MetaDataObject[str].New(MetaDataObjectValue="Segment_2"))
    meta_dict.Set("Segment1_Color", itk.MetaDataObject[str].New(MetaDataObjectValue="0.40625 0.2187 0.6796")) # Purplish
    meta_dict.Set("Segment1_LabelValue", itk.MetaDataObject[str].New(MetaDataObjectValue="2"))
    meta_dict.Set("Segment1_Layer", itk.MetaDataObject[str].New(MetaDataObjectValue="0"))

    itk.imwrite(my_image, str(output_dir/ "my_image.nrrd"))
1 Like