c++dicomdcmtk

How to read binary data from dicom file?


How to read raw image data from uncompressed DICOM file and dump it to a file. I just use the following code for compressed files. using dcmtk library

dataSet->findAndGetElement(DCM_PixelData, element);
pixDataElem = OFstatic_cast(DcmPixelData*, element);

DcmPixelSequence *pixelSequence = NULL;
E_TransferSyntax tran_Syntax = EXS_Unknown;
const DcmRepresentationParameter *representation = NULL;

// Find the key that is needed to access the right representation of the data within DCMTK
pixDataElem->getOriginalRepresentationKey(tran_Syntax, representation);
//pixDataElem->getCurrentRepresentationKey(tran_Syntax, representation);

// Access original data representation and get result within pixel sequence
pixDataElem->getEncapsulatedRepresentation(tran_Syntax, representation, pixelSequence);


    DcmPixelItem *pixelItem = NULL;
    //Access the First frame by skipping the offset table...
    pixelSequence->getItem(pixelItem, 1);

    Uint8 *pixels = NULL;
    pixDataElem = (DcmPixelData*)pixelItem;
    pixDataElem->getUint8Array(pixels);
    Uint8 *pixels = NULL;
    pixDataElem->getUint8Array(pixels);
    //Writing the Raw data to a file...
    FILE *file;
    file = fopen("D:\\DicomImage.jpeg", "wb");
    fwrite(pixels, sizeof(char), imageSize, file);
    cout << "File write Completed and the File is closed Successfully" << endl;

How can I raw image data from the uncompressed files with many frames in c++ using dcmtk library.....?


Solution

  • Basically, you can use the same code, but without compression (this is actually the easier case...)

    dataSet->findAndGetElement(DCM_PixelData, element);
    pixDataElem = OFstatic_cast(DcmPixelData*, element);
    
    Uint8 *pixels = NULL;
    pixDataElem->getUint8Array(pixels);
    
    //Writing the Raw data to a file...
    FILE *file;
    file = fopen("D:\\DicomImage.raw", "wb");
    // frameSize is the size of a single frame
    fwrite(pixels + frameSize * frameIndex, sizeof(char), frameSize, file);
    cout << "File write Completed and the File is closed Successfully" << endl;
    

    (this is out of my head, so no guarantee for completeness)
    What you get, is the raw binary data. If you want to create an image file like JPG from that, you need the respective image functionality, though that has nothing to do with dcmtk.