<< Back to the main page

Inspecting DICOM dataset

In DICOM data set each tag is identified by its key. The key is composed of two 16-bit values: group number, and element number. QDCM stores DICOM tags using QVariant type:

DcmDataset dataset;
// ...
QVariant v = dataset.tagValue(DcmTagKey(0x0002, 0x0010));

If you know the data type of a tag, you may convert it directly from QVariant:

QString patientName = dataset.tagValue(DcmTagKey(0x0010, 0x0010)).toString();

If the tag you try to access is missing in the dataset a void QVariant value is returned.

You may also access DICOM tag by its names instead of using group, element pair. QDCM has a build-in dictionary for known DICOM tags that maps tag keys with their names:

QString patientName = dataset.tagValue("PatientName").toString();
Or even:
QString patientName = dataset["PatientName"].toString();

Some DICOM tags may have value multiplicity higher than 1. These tags usually have string representation, and multiple values are separated by '\' character. In order to access such tags you need use tagValues() method that returns QVariantList:

QVariantList pixelSpacing = dataset.tagValues("PixelSpacing");

When parsing DICOM data QDCM does not preserve original tags. For instance QDCM always inserts group tags even if they were missing in the original DICOM source. Grouping tags allows faster access when searching for a particular tag.

You can also get a pointer to a tag stored in a dataset. This allows you exploring the tag in details (e.g. getting value representation):

DcmTag *tagPtr = dataset.findTag("RescaleSlope");
if (tagPtr && tagPtr->vr() == DcmVr::DS) {
    DcmTagDS *tagDSPtr = dynamic_cast<DcmTagDS*>(tagPtr);
    double slope = tagDSPtr->asDouble();
}

Accessing sequence tags

Sequence tags can be accessed by a pointer. A sequence tag itself is a list of item tags, which in its turn is a list of tag groups.

        
// Display all tags from a sequence (1234, 5678)        
DcmTag *tagPtr = dataset.findTag(DcmTagKey(0x1234, 0x5678));
if (tagPtr && tagPtr->vr() == DcmVr::SQ) {
    DcmTagSQ *tagSQPtr = dynamic_cast<DcmTagSQ*>(tagPtr);
    DcmTagList items = tagSQPtr->items();
    // Iterate over items
    foreach (DcmTag *itemTagPtr, items.list()) {
        DcmTagItem *itemPtr = dynamic_cast<DcmTagItem*>(itemTagPtr);
        DcmTagList itemGroups = itemPtr->tagGroups();
        // Iterate over groups in an item
        foreach (DcmTag *tagGroupPtr, itemGroups.list()) {
            DcmTagGroup *groupPtr = dynamic_cast<DcmTagGroup*>(tagGroupPtr);
            // Iterate over tags in a group
            foreach (DcmTag *innerTagPtr, groupPtr->tags()) {
                qDebug() << innerTagPtr->tagKey().toString()
                         << innerTagPtr->vr().toString()
                         << innerTagPtr->value();
            }
        }
    }
}


©2012 by Arthur Benilov

Project Web Hosted by
SourceForge.net