Encode JPG image file as DICOM PixelData using ClearCanvas

2.7k Views Asked by At

I have a set of JPG images that are actually slices of a CT scan, which I want to reconstruct into DICOM image files and import into a PACS.

I am using ClearCanvas, and have set all of the requisite tags (and confirmed them by converting one of my JPG files to DICOM using a proprietary application to make sure they are the same). I am just not sure how I should be processing my JPG file to get it into the PixelData tag?

Currently I am converting it to a Byte array, on advice from ClearCanvas forums, but the image is just garbled in the DICOM viewer. How should I be processing the image data to get it into a readable format?

    public DicomFile CreateFileFromImage(Image image)
    {
        int height = image.Height;
        int width = image.Width;
        short bitsPerPixel = (short)Image.GetPixelFormatSize(image.PixelFormat);

        byte[] imageBuffer = ImageToByteArray(image);
        DicomFile dicomFile = new DicomFile();
        dicomFile.DataSet[DicomTags.Columns].SetInt32(0, width);
        dicomFile.DataSet[DicomTags.Rows].SetInt32(0, height);
        dicomFile.DataSet[DicomTags.BitsStored].SetInt16(0, bitsPerPixel);
        dicomFile.DataSet[DicomTags.BitsAllocated].SetInt16(0, bitsPerPixel);
        dicomFile.DataSet[DicomTags.HighBit].SetInt16(0, 7);
        //other tags not shown
        dicomFile.DataSet[DicomTags.PixelData].Values = imageBuffer;
        return dicomFile;
    }
    public static byte[] ImageToByteArray(Image imageIn)
    {
        MemoryStream ms = new MemoryStream();

        imageIn.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
        return ms.ToArray();
    }
3

There are 3 best solutions below

2
On

The ClearCanvas library as two helper classes that make it easier to encode and decode pixel data within a DicomFile. They are the DicomCompressedPixelData class and the DicomUncompressedPixelData class. You can use these to set the parameters for the image, and encode them into the DicomFile object.

In your case, since you're encoding a compressed object, you should use the DicomCompressedPixelData class. There are properties on the class that can be set. Calling the UpdateMessage method will copy these property values over to the DicomFile object. Also, this class has an AddFrameFragment method that properly encodes the pixel data. Note that compressed pixel data has to have some specific binary wrappers around each frame of data. This was the part missing from your previous code. The code below shows how to set this up.

        short bitsPerPixel = (short)Image.GetPixelFormatSize(image.PixelFormat);

        var dicomFile = new DicomFile();
        var pd = new DicomCompressedPixelData(dicomFile);

        pd.ImageWidth = (ushort)image.Width;
        pd.ImageHeight = (ushort) image.Height;
        pd.BitsStored = (ushort)bitsPerPixel;
        pd.BitsAllocated = (ushort) bitsPerPixel;
        pd.HighBit = 7;
        pd.SamplesPerPixel = 3;
        pd.PlanarConfiguration = 0;
        pd.PhotometricInterpretation = "YBR_FULL_422";

        byte[] imageBuffer = ImageToByteArray(image);
        pd.AddFrameFragment(imageBuffer);

        pd.UpdateMessage(dicomFile);

        return dicomFile;
0
On

It is important to know the bit depth and color components of your JPEG CT image before inserting into DICOM dataset. It could be 8-bit lossy (JPEG Compression Process 2) or 12-bit lossy (JPEG Compression Process 4) or 8, 12 or 16 bit lossless grayscale JPEG (JPEG Compression Process 14 - lossless, non-hierarchical). This information is critical for updating the Pixel Data related information such as Photometric Interpretation, Sample per Pixel, Planer Configuration, Bits Allocated, High Bit to Transfer Syntax.

0
On

I ended up processing the bitmap manually and creating an array out of the Red Channel in the image, following some code in a plugin:

            int size = rows * columns;
            byte[] pData = new byte[size]; 
            int i = 0;

            for (int row = 0; row < rows; ++row)
            {
                for (int column = 0; column < columns; column++)
                {
                    pData[i++] = image.GetPixel(column, row).R;
                }
            }

It does work, but it is horribly slow and creates bloated DICOM files. I'd love to get the inbuilt DicomCompressedPixelData class working.

Any further suggestions would be very welcome.