<< Back to the main page

Dealing with DICOM images

Currently QDCM does not support compressed images. So if you have to deal with compressed data, you will need to decompress a pixel data yourself.

The following example demonstrates DICOM image loading:

DcmFile file("dicom.dcm");

DcmDataset dataset = file.read();
if (!file.isError()) {
    DcmImage image(dataset);
    
    qDebug() << "width:" << image.width();
    qDebug() << "height:" << image.height();
    qDebug() << "frames:" << image.frames();
    qDebug() << "bits allocated:" << image.bitsAllocated();
    qDebug() << "bits stored:" << image.bitsStored();
    qDebug() << "PI:" << image.photometricInterpretation().toString();
    qDebug() << "Compressed:" << (image.pixelData->isEncapsulated() ? "YES" : "NO");
}

Monochrome images

Monochrome images are handled via DcmMonochromeImage class. An image photometric interpretation must be MONOCHROME1 or MONOCHROME2 in order to be considered as monochrome.

Here is an example showing how monochrome image pixels can be accessed:

DcmFile file("dicom.dcm");

DcmDataset dataset = file.read();
if (!file.isError()) {
    DcmImage image(dataset);
    if (image.photometricInterpretation().isGrayscale() {
        DcmMonochromeImage monoImage(dataset);
        
        int x = 10; // pixel coordinates
        int y = 20;
        
        DcmUnsignedShort pixelValue = monoImage.rawPixel(x, y);
        double rescaledPixelValue = monoImage.rescaledPixel(x, y);
    }
}
Rescaled pixel value is calculated using RescaleSlope and RescaleIntercept tags in a dataset.

In order to transform DICOM grayscale image in to a QImage you will have to provide a transfer function. Transfer function maps DICOM pixel data value (which may take up to 16 bits) into a QColor. You may define a transfer function by specifying reference points that associate pixel value to a specific color. Intermediate values will be linearly interpolated. Usually DICOM images contain WindowCenter and WindowWidth tags that can be used to construct a transfer function for adapted view:

DcmFile file("dicom.dcm");

DcmDataset dataset = file.read();
if (!file.isError()) {
    DcmImage image(dataset);
    if (image.photometricInterpretation().isGrayscale() {
        DcmMonochromeImage monoImage(dataset);
        
        DcmImageTransferFunction tf;

        double windowCenter = monoImage.windowCenter();
        double windowWidth = monoImage.windowWidth();

        tf.addReferencePoint(windowCenter - windowWidth / 2, QColor(0, 0, 0));
        tf.addReferencePoint(windowCenter + windowWidth / 2, QColor(255, 255, 255));
        
        // Getting Qt image...
        QImage qImage = monoImage.toQImage(tf);
        
        // ...and saving it as a PNG file
        qImage.save("dicom.png");
    }
}


©2012 by Arthur Benilov

Project Web Hosted by
SourceForge.net