Search code examples
c++opencvdicommatdcmtk

Read DICOM in C++ and convert to OpenCV


I would like to read DICOM images in C++ and manipulate them using opencv.

I managed to read a dicom image using DCMTK however, I am unsure how to convert it to an opencv Mat.

The following is what I have so far:

DicomImage DCM_image("test.dcm");
cv::Mat image(int(DCM_image.getWidth()), int(DCM_image.getHeight()), CV_8U, (uchar*)DCM_image.getOutputData(8));

which results in the following:

enter image description here

In a DICOM viewer, it looks as follows:

enter image description here

After normalising, the grayed image appears as follows:

enter image description here

Any help would be greatly appreciated.


Solution

  • The main problem I can see now is the difference of pixel value ranges (depths).

    AFAIK, DICOM can have a rather big depth (16 bit), and you are trying to fit in into CV_8U, which is only 8 bit. You can get DicomImage instance's depth using DicomImage::getDepth(), and then create a cv::Mat with appropriate depth to hold your image data.

    You may also need to normalize the data to maximally utilize you available range, so that the display with cv::imshow() would look as expected.

    So:

    • Do DicomImage::getDepth() on your DCM_image
    • Create a cv::Mat with sufficient depth to hold your data
    • Scale, if necessary