Search code examples
c#dicomgdcm

Apply LUT using GDCM on DICOM File


I have an RLE compressed DICOM file that has PALETTE_COLOR photometric interpretation. Using GDCM, I can get the fragment using this code:

gdcm.ImageReader imagereader = new gdcm.ImageReader();
imagereader.SetFileName(fileName);
if (imagereader.Read())
  {
  DataElement compressedPixelData = imagereader.GetFile().GetDataSet().GetDataElement(Helper.PIXEL_DATA);
  SequenceOfFragments sf = compressedPixelData.GetSequenceOfFragments();
  if (sf == null) throw new Exception("Cannot get fragments!");
  Fragment frag = sf.GetFragment((uint)frameNumber);
  uint bufLen = Convert.ToUInt32(frag.GetByteValue().GetLength().toString());
  byte[] buffer = new byte[bufLen];
  frag.GetByteValue().GetBuffer(buffer, bufLen);
  }

Now I'm trying to create an image from the buffer. Since it's PALETTE_COLOR, I have to apply the LUT to the buffer. I use this code :

gdcm.LookupTable lut = imagereader.GetImage().GetLUT();

int size = ImageWidth * ImageHeight * 3;
byte[] decodedBuffer = new byte[size];

bool worked = lut.Decode(decodedBuffer, (uint)size, buffer, (uint)bufLen);

if (worked)
   return decodedBuffer;
else
   return buffer;

The decodedBuffer size is Width * Height * 3 since I expect RGB pixel after applying LUT. But the resulting image is incorrect.

If I use ImageApplyLookupTable class, I can display the image correctly.

I don't want to use ImageApplyLookupTable class since it will decode the entire image (all fragments!) into raw RGB pixels and consume a lot of memory. I want to decode framge by frame to minimize memory usage.

Could you point me to right direction on how to use gdcm.LookupTable class correctly to decode a frame at a time ? The sample file I used is here.

UPDATED: Using ImageRegionReader worked for the 8 bit PALETTE COLOR, but didn't work for 16 bit, could you check why? I have the sample here for 16 bit.


Solution

  • GDCM comes with a bunch of examples, in your case I would suggest you start reading: ExtractImageRegionWithLUT.cs.

    The code is pretty silly since it will write over and over the extracted frames over the previous one. But anyway this should give you the basic idea. Here is what I did over here:

    First make sure your C# examples are build (I use make in my case):

    $ make GDCMCSharpExample
    

    then:

    $ ExtractImageRegionWithLUT.exe PAL-16_RLE.dcm
    

    On UNIX this will create a raw file, that you can convert back to DICOM using gdcmimg:

    $ gdcmimg --depth 16 --spp 3 --size 800,600 /tmp/frame_rgb.raw /tmp/frame_rgb.raw.dcm
    

    You can then simply view it:

    $ gdcmviewer /tmp/frame_rgb.raw.dcm
    

    You can even use the following to convince yourself:

    $ gdcmimg -C 1.2.840.10008.5.1.4.1.1.3.1 --depth 16 --spp 3 --size 800,600 /tmp/frame_rgb.raw /tmp/frame_rgb.raw.dcm