Search code examples
c#fo-dicom

Why is failing to retrieve DICOM tag values?


I am using the following code to retrieve values from a DICOM file. All values contained string data types:

var item1 = dicomDataset.GetSingleValueOrDefault<string>(DicomTag.MediaStorageSOPInstanceUID, "");
if (item1 == "")
{
    item1 = "(not set)";
}
Console.WriteLine($"SOPInstanceUID: {item1}");  //DOES NOT WORK


var sopUID = dicomDataset.GetSingleValueOrDefault<string>(DicomTag.SOPInstanceUID, "");
if (sopUID == "")
{
    sopUID = "(not set)";
}
Console.WriteLine($"SOPInstanceUID: {sopUID}"); //DOES NOT WORK


var stuUID = dicomDataset.GetSingleValueOrDefault<string>(DicomTag.StudyInstanceUID, "");
if (stuUID == "")
{
    stuUID = "(not set)";
}
Console.WriteLine($"StudyInstanceUID: {stuUID}");



var serInst = dicomDataset.GetSingleValueOrDefault<string>(DicomTag.SeriesInstanceUID, "");
if (serInst == "")
{
    serInst = "(not set)";
    }
    Console.WriteLine($"SeriesInstanceUID: {serInst}");
    
    
    var forUID = dicomDataset.GetSingleValueOrDefault<string>(DicomTag.FrameOfReferenceUID, "");
    if (forUID == "")
    {
        forUID = "(not set)";
    }
    Console.WriteLine($"FrameOfReferenceUID: {forUID}");

There are two values that cannot be retrieved (DicomTag.MediaStorageSOPInstanceUID and SOPInstanceUID); however, the rest of the values are returned successfully. Any ideas what is going on here? Many thanks in advance.


Solution

  • Usually, you should not find the MediaStorageSOPInstanceUID in the dicomdataset. The MediaStorageSOPInstanceUID is stored in Group 0002, which is a kind of header of DicomFiles.

    So if the sourc is a DicomFile, then you will find the MediaStorageSOPInstanceUID in dicomFile.FileMeataInfo.GetSingleValueOrDefault<string>(DicomTag.MediaStorageSOPInstanceUID, "") and all the other tags in dicomFile.Dataset.GetSingleValueOrDefault.....

    If the sourc is a Dataset received via network in a StoreSCP service, then you will not find the MediaStorageSOPInstanceUID at all. Fo-dicom will add it when you store the dataset to a file.

    The way, you access the SOPInstanceUID is perfectly correct. The only explanation is, that your dicomdataset is not a valid dicom file.