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:
In a DICOM viewer, it looks as follows:
After normalising, the grayed image appears as follows:
Any help would be greatly appreciated.
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:
DicomImage::getDepth()
on your DCM_imagecv::Mat
with sufficient depth to hold your data