Reputation: 9
I have found a way using vtk to display dcm image. But vtk is too much for what I want, I only want to display a dcm image. The dcmtk will process the dcm image for me. So is there an easy way for me to display dcm image? Thanks in advance.
Upvotes: 0
Views: 854
Reputation: 9
@john elemans
If I use the code that you gave me, It seems to be worng. My image's pixel format is UINT16, so the program will execute the following sentences.
unsigned char *ubuffer = new unsigned char[dimX*dimY*3];
unsigned char *pubuffer = ubuffer;
for(unsigned int i = 0; i < dimX*dimY; i++)
{
*pubuffer++ = *buffer;
*pubuffer++ = *buffer;
*pubuffer++ = *buffer++;
}
imageQt = new QImage(ubuffer, dimX, dimY, QImage::Format_RGB888);
But after the converting, the result is not right.
The original image is like this:before
And this is the image which has been converted:after
The result proves that the code isn't right. I also have tried other ways. One of the codes that I have tried is this:
short *buffer16 = (short*)buffer;
unsigned char *ubuffer = new unsigned char[dimX*dimY*3];
unsigned char *pubuffer = ubuffer;
for (unsigned int i = 0; i < dimX*dimY; i++)
{
*pubuffer++ = *buffer16;
*pubuffer++ = *buffer16;
*pubuffer++ = *buffer16;
buffer16++;
}
imageQt = new QImage(ubuffer, dimX, dimY, QImage::Format_RGB888);
After I used this code to convert the image, I almost believed that I have succeeded. But the result is like this: sorry, I don't have the enough reputation the post more than 2 links.
I only want to convert DICOM image to bitmap, but I don't know how.
At last, thank you for your help.
Upvotes: 0
Reputation: 2676
The smallest learning curve and code requirement will likely be to use Grass Roots DICOM. (http://gdcm.sourceforge.net/wiki/index.php/Main_Page) This library will link to Qt and give you a quick way to load an image. The only thing to remember is that a DICOM image file does not contain an image that Qt (or anything else) can display directly. You have to load the DICOM data and convert the image to display it.
These are the lines to add to the project file. Note that the paths will have to match your machine and versions, not mine;
#INCLUDEPATH += /usr/local/include/gdcm-2.4/
#LIBS += -L"/usr/local/lib/" -lgdcmCommon -lgdcmDICT -lgdcmDSED -lgdcmIOD -lgdcmMEXD -lgdcmMSFF -lgdcmjpeg12 -lgdcmjpeg16 -lgdcmopenjpeg -lgdcmjpeg8
#LIBS += -L"/usr/local/lib/" -lgdcmcharls -lexpat -lgdcmzlib
This is an example converter, once you have the dicom image loaded, it will convert it to a Qt QImage.
bool imageConverters::convertToFormat_RGB888(gdcm::Image const & gimage, char *buffer, QImage* &imageQt)
{
unsigned int dimX;
unsigned int dimY;
int photoInterp;
const unsigned int* dimension = gimage.GetDimensions();
if (dimension == 0)
{
dimX = 800;
dimY = 600;
}
else
{
dimX = dimension[0];
dimY = dimension[1];
}
gimage.GetBuffer(buffer);
photoInterp = gimage.GetPhotometricInterpretation();
qDebug() << "photo interp = " << photoInterp;
qDebug() << "pixel format = " << gimage.GetPixelFormat();
// Let's start with the easy case:
if( photoInterp == gdcm::PhotometricInterpretation::RGB )
{
if( gimage.GetPixelFormat() != gdcm::PixelFormat::UINT8 )
{
return false;
}
unsigned char *ubuffer = (unsigned char*)buffer;
// QImage::Format_RGB888 13 The image is stored using a 24-bit RGB format (8-8-8).Format_RGB888 Format_ARGB32
imageQt = new QImage((unsigned char *)ubuffer, dimX, dimY, 3*dimX, QImage::Format_RGB888);
//imageQt = &imageQt->rgbSwapped();
}
else
if( photoInterp == gdcm::PhotometricInterpretation::MONOCHROME2 ||
photoInterp == gdcm::PhotometricInterpretation::MONOCHROME1
)
{
if( gimage.GetPixelFormat() == gdcm::PixelFormat::UINT8 || gimage.GetPixelFormat() == gdcm::PixelFormat::INT8
|| gimage.GetPixelFormat() == gdcm::PixelFormat::UINT16)
{
// We need to copy each individual 8bits into R / G and B:
unsigned char *ubuffer = new unsigned char[dimX*dimY*3];
unsigned char *pubuffer = ubuffer;
for(unsigned int i = 0; i < dimX*dimY; i++)
{
*pubuffer++ = *buffer;
*pubuffer++ = *buffer;
*pubuffer++ = *buffer++;
}
imageQt = new QImage(ubuffer, dimX, dimY, QImage::Format_RGB888);
}
else
if( gimage.GetPixelFormat() == gdcm::PixelFormat::INT16 )
{
// We need to copy each individual 16bits into R / G and B (truncate value)
short *buffer16 = (short*)buffer;
unsigned char *ubuffer = new unsigned char[dimX*dimY*3];
unsigned char *pubuffer = ubuffer;
for(unsigned int i = 0; i < dimX*dimY; i++)
{
// Scalar Range of gdcmData/012345.002.050.dcm is [0,192], we could simply do:
// *pubuffer++ = *buffer16;
// *pubuffer++ = *buffer16;
// *pubuffer++ = *buffer16;
// instead do it right:
*pubuffer++ = (unsigned char)std::min(255, (32768 + *buffer16) / 255);
*pubuffer++ = (unsigned char)std::min(255, (32768 + *buffer16) / 255);
*pubuffer++ = (unsigned char)std::min(255, (32768 + *buffer16) / 255);
buffer16++;
}
imageQt = new QImage(ubuffer, dimX, dimY, QImage::Format_RGB888);
}
else
{
std::cerr << "Pixel Format is: " << gimage.GetPixelFormat() << std::endl;
return false;
}
}
else
{
std::cerr << "Unhandled PhotometricInterpretation: " << gimage.GetPhotometricInterpretation() << std::endl;
return false;
}
return true;
}
Upvotes: 3