Strange rendering of image data when planar configuration 1!

All other questions regarding DCMTK

Moderator: Moderator Team

Post Reply
Message
Author
faisal_tum
Posts: 33
Joined: Fri, 2013-01-11, 15:37

Strange rendering of image data when planar configuration 1!

#1 Post by faisal_tum »

I'm visualizing a single frame of a dicom image in a GUI combining Qt and Dcmtk. The visualization works perfectly when the image is of Planar configuration 0, but it gives absurd visualization when the image is of planar configuration 1.

Below is an image of a single frame dicom image with planar configuration when the dicom image is viewed in Sante Dicom viewer.

Image

But when I use the following code to get the first frame(in this case there's only one frame) in an array and then use that array to visualize it it gives an absurd result. The code is as below:

Code: Select all

               DcmFileFormat fileformat;

               string filename = "D:/file.dcm";
                if(fileformat.loadFile(filename.c_str()).good())
                {
                    DcmDataset* dataset = fileformat.getDataset();
                    OFCondition rowCheck=dataset->findAndGetUint16(DCM_Rows, rows);
                    if(EC_Normal == rowCheck)
                    {
                        OFCondition colCheck=dataset->findAndGetUint16(DCM_Columns, cols);
                        if(EC_Normal == colCheck)
                        {
                            DJDecoderRegistration::registerCodecs();
                            DcmElement * element = NULL;
                            OFCondition elemError = dataset->findAndGetElement(DCM_PixelData, element);
                            if(EC_Normal == elemError)
                            {
                                Uint32 sizeF = 0;
                                OFCondition UCSizeE = element->getUncompressedFrameSize(dataset, sizeF);
                                if(EC_Normal == UCSizeE)
                                {
                                    OFString decompressedColorModel = NULL;
                                    OFCondition gDcCME = element->getDecompressedColorModel(dataset, decompressedColorModel);
                                    if(EC_Normal == gDcCME)
                                    {
                                        Uint8* temp = new Uint8[int(sizeF)];
                                        DcmFileCache * cache = NULL;
                                        Uint32 startFragment = 0;
                                        OFCondition getImgE = element->getUncompressedFrame(dataset, 0, startFragment, temp, sizeF, decompressedColorModel, cache);
                                        if(EC_Normal == getImgE)
                                        {
                                            //then I just show the image using the image data stored in temp.
                                
                                          }
                                     }
                                }
                           }
                      }
                 }
           }
The image I get after I visualize by using the image data stored in buffer

Code: Select all

temp
is as below:

Image

I don't understand why it should be happening like that. Do you think I'm doing something wrong? If so, could you please instruct what to improve?

J. Riesmeier
DCMTK Developer
Posts: 2501
Joined: Tue, 2011-05-03, 14:38
Location: Oldenburg, Germany
Contact:

Re: Strange rendering of image data when planar configuratio

#2 Post by J. Riesmeier »

Why don't you use the DicomImage class?

faisal_tum
Posts: 33
Joined: Fri, 2013-01-11, 15:37

Re: Strange rendering of image data when planar configuratio

#3 Post by faisal_tum »

J. Riesmeier wrote:Why don't you use the DicomImage class?
Could you please elaborate me why I should use DicomImage class? Do you mean I will create a .bmp file using the DicomImage class and visualize it using that .bmp file?

As for the reason behind not using DicomImage class is that the app actually loads multiple dicom images(multiframe or single frame) and then take the image data(in case of multiframe I just take the image of first frame) and visualize all the images in a table with each image being an small icon. Then the user selects some images from that table, and tells the app to erase some portion of the selected images by choosing a rectangular region on the image. The rectangular region is the portion that is to be erased.

Using the code I showed here, the whole process works perfectly with compressed or uncompressed Dicom images when the images are of Planar Configuration 0, but it's not working for planar configuration 1. The main aim of this app is to help the user to anonymize the patient info that is written on the image data.

If you don't mean me to other approach, without creating .bmp image file, using DicomImage class, I think I'm not understanding, could you please explain further?

J. Riesmeier
DCMTK Developer
Posts: 2501
Joined: Tue, 2011-05-03, 14:38
Location: Oldenburg, Germany
Contact:

Re: Strange rendering of image data when planar configuratio

#4 Post by J. Riesmeier »

Could you please elaborate me why I should use DicomImage class? Do you mean I will create a .bmp file using the DicomImage class and visualize it using that .bmp file?
No, I was not thinking of a BMP file. The DicomImage is made for rendering arbitrary DICOM images and to output the result of the rendering pipeline to various formats, including a pixel array that can be used for display. See DicomImage::getOutputData() methods.

If you don't want to use the DicomImage class, you have to implement the rendering on your own, which is a quite complex task as you can see from the amount of code for the dcmimgle/dcmimage module(s).

faisal_tum
Posts: 33
Joined: Fri, 2013-01-11, 15:37

Re: Strange rendering of image data when planar configuratio

#5 Post by faisal_tum »

J. Riesmeier wrote:
Could you please elaborate me why I should use DicomImage class? Do you mean I will create a .bmp file using the DicomImage class and visualize it using that .bmp file?
No, I was not thinking of a BMP file. The DicomImage is made for rendering arbitrary DICOM images and to output the result of the rendering pipeline to various formats, including a pixel array that can be used for display. See DicomImage::getOutputData() methods.

If you don't want to use the DicomImage class, you have to implement the rendering on your own, which is a quite complex task as you can see from the amount of code for the dcmimgle/dcmimage module(s).

Thanks for the answer. I was actually guessing that I might have to use the method getOutputData. One more question, will the OutPutdata give the whole pixel data where the buffer size would exactly be row*column*sample-per-pixel*number-of-frames?

J. Riesmeier
DCMTK Developer
Posts: 2501
Joined: Tue, 2011-05-03, 14:38
Location: Oldenburg, Germany
Contact:

Re: Strange rendering of image data when planar configuratio

#6 Post by J. Riesmeier »

In fact, there is an example on how to use getOutputData() in the documentation of the module...
One more question, will the OutPutdata give the whole pixel data where the buffer size would exactly be row*column*sample-per-pixel*number-of-frames?
This is also explained in the API documentation. The method getOutputData() gives you the rendered output of a single frame. The size of the buffer is returned by getOutputDataSize().

So, my general advice would be: Read the documentation and have a look at the examples :)

faisal_tum
Posts: 33
Joined: Fri, 2013-01-11, 15:37

Re: Strange rendering of image data when planar configuratio

#7 Post by faisal_tum »

J. Riesmeier wrote:In fact, there is an example on how to use getOutputData() in the documentation of the module...
One more question, will the OutPutdata give the whole pixel data where the buffer size would exactly be row*column*sample-per-pixel*number-of-frames?
This is also explained in the API documentation. The method getOutputData() gives you the rendered output of a single frame. The size of the buffer is returned by getOutputDataSize().

So, my general advice would be: Read the documentation and have a look at the examples :)
Thanks, it's been great help.

faisal_tum
Posts: 33
Joined: Fri, 2013-01-11, 15:37

Re: Strange rendering of image data when planar configuratio

#8 Post by faisal_tum »

J. Riesmeier wrote:In fact, there is an example on how to use getOutputData() in the documentation of the module...
One more question, will the OutPutdata give the whole pixel data where the buffer size would exactly be row*column*sample-per-pixel*number-of-frames?
This is also explained in the API documentation. The method getOutputData() gives you the rendered output of a single frame. The size of the buffer is returned by getOutputDataSize().

So, my general advice would be: Read the documentation and have a look at the examples :)
I did use getOutputData method now, but nothing improved, it is still has 9 small images in place of one, in fact it got worse as the images are much much darker. I think the issue could be that internally the getOutputData method doesn't quite change its functionality for planar configuration 1. I see that there is a protected method togglePlanarConfiguration8 in DJCoderEncoder, but I can't use to toggle the pixel data either. Are you sure getOutputData and getUncompressedFrame methods are working correctly for planar configuration 1? Or is it that I'm making some mistake when using the methods?

J. Riesmeier
DCMTK Developer
Posts: 2501
Joined: Tue, 2011-05-03, 14:38
Location: Oldenburg, Germany
Contact:

Re: Strange rendering of image data when planar configuratio

#9 Post by J. Riesmeier »

Are you sure getOutputData and getUncompressedFrame methods are working correctly for planar configuration 1?
Yes, I am quite sure. However, please note that getOutputData() is a high-level function that returns a rendered image and getUncompressedFrame() is a low-level function that just returns the raw pixel data. I hope you've considered this difference in your code.
Or is it that I'm making some mistake when using the methods?
Yes, this is what I suspect. Of course, there could also be an issue with the sample file you are using...

faisal_tum
Posts: 33
Joined: Fri, 2013-01-11, 15:37

Re: Strange rendering of image data when planar configuratio

#10 Post by faisal_tum »

J. Riesmeier wrote:
Are you sure getOutputData and getUncompressedFrame methods are working correctly for planar configuration 1?
Yes, I am quite sure. However, please note that getOutputData() is a high-level function that returns a rendered image and getUncompressedFrame() is a low-level function that just returns the raw pixel data. I hope you've considered this difference in your code.
Or is it that I'm making some mistake when using the methods?
Yes, this is what I suspect. Of course, there could also be an issue with the sample file you are using...
I think for my purpose getUncompressedFrame method is required because I need access to the raw data. So, what I tried is to change to planar configuration on the run by mimicking the following code that I found in the source of djcodece.cc.

Code: Select all

OFCondition DJCodecEncoder::togglePlanarConfiguration8(
  Uint8 *pixelData,
  const unsigned long numValues,
  const Uint16 samplesPerPixel,
  const Uint16 oldPlanarConfig)
{
  if ( (pixelData == NULL) || (numValues%samplesPerPixel != 0) )
    return EC_IllegalParameter;
  // allocate target buffer
  Uint8* px8 = new Uint8[numValues];
  if (!px8)
    return EC_MemoryExhausted;
  unsigned long numPixels = numValues / samplesPerPixel;
  if (oldPlanarConfig == 1)   // change from "by plane" to "by pixel"
  {
    for (unsigned long n=0; n < numPixels; n++)
    {
        for (Uint16 s=0; s < samplesPerPixel; s++)
          px8[n*samplesPerPixel+s]   = pixelData[n+numPixels*s];
    }
  }
  else  //change from "by pixel" to "by plane"
  {
    for (unsigned long n=0; n < numPixels; n++)
    {
        for (Uint16 s=0; s < samplesPerPixel; s++)
          px8[n+numPixels*s]   = pixelData[n*samplesPerPixel+s];
    }
  }
  // copy filled buffer to pixel data and free memory
  memcpy(pixelData, px8, OFstatic_cast(size_t, numValues));
  delete[] px8;
  return EC_Normal;
}
After the modifying the pixel data I also changed the planar configuration using the method putAndInsertUint16, but when I'm calling the method getDecompressedColorModel of DcmElement class, it gives "Illegal parameters" error. So, now my question is, once I change the planarconfiguration of the pixel data using the code I showed here, is changing the planar configuration enough, or I have to make further changes?

J. Riesmeier
DCMTK Developer
Posts: 2501
Joined: Tue, 2011-05-03, 14:38
Location: Oldenburg, Germany
Contact:

Re: Strange rendering of image data when planar configuratio

#11 Post by J. Riesmeier »

I think for my purpose getUncompressedFrame method is required because I need access to the raw data.
No, you don't need raw data. You need rendered data.

Maybe, you should reread what you wrote in your first posting. Your first sentence was: "I'm visualizing a single frame of a dicom image in a GUI combining Qt and Dcmtk." -- Got it? :)

Post Reply

Who is online

Users browsing this forum: Google [Bot] and 1 guest