Search code examples
fo-dicom

How to remove one Item from nested tag sequence using fo-dicom?


DICOM files contain many group of tags defined as (fffe,e000).

  • The “Reference Beam Sequence” contains 3 Items; (item#1, Item#2, Item#3).

  • All Items have the tag (fffe,e000).

  • Each Item (fffe,e000) contains additional tags inside.

  • How to delete one of the Items (fffe,e000).

How can I remove the entire (fffe,e000) item#1 nested group?

I've tried this:

for (int i = 0; i < beams; i++)
{
    var items = dicomDataset.GetSequence(DicomTag.BeamSequence).Items[i];
     
    var beamNumber = items.GetSingleValueOrDefault<string>(DicomTag.BeamNumber, "");

    //remove the first seq
    if(beamNumber=="1")
    {
        dicomDataset.Remove(x => x.Tag.Group == 2);
    }
}

But no luck.


Solution

  • First, let's clarify the terminology. A DICOM dataset has a list of DICOM tags. One type of tags are sequences, e.g. tags with the VR (value representation) SQ. A sequence is a special kind of tag that contains 0-n items, which are numbered by their index. They are not tags, but whole (nested) datasets, each containing their own list of DICOM tags (possibly including nested sequence tags).

    The item representation uses delimiters like (fffe,e000) internally, but that is a detail that is handled by the DICOM framework and usually shall not bother you.

    In fo-dicom, a sequence tag value (e.g. a DICOM sequence) is represented by the DicomSequence class, which allows to access its sequence items via the Items property, representing a list of DicomDataset objects. If you want to delete one of the items, you can just work on that list:

    var datafile = DicomFile.Open("my_dicom_file.dcm");
    var dataset = datafile.Dataset;
    
    // error handling omitted here, you have to check for existence
    // of sequence and items
    var items = dataset.GetSequence(DicomTag.BeamSequence).Items;
    
    // removes the first sequence item
    items.RemoveAt(0);
    
    datafile.Save("my_dicom_file_without_first_beam_sequence_item.dcm");
    

    You can modify nested sequences in a similar fashion by first getting the sequence tag from the needed item, and continue the same way as with a sequence in the dataset root (as shown above).