none
How to Convert a 16 bit Grayscale Dicom Image to 8 bit Dicom image RRS feed

  • Question

  • Hi,

    I am working on an Image Blending application using WPF. I have two images to show on the screen. One is an XRAY(16 bits grayscale) image and other is an OPTICAL(24 bits RGB) image. The optical image I have is with 8 bits depth. Before blending two images I have to downscale the Xray image with 16 bits image depth to 8 bits image depth.

    I use the below code. Now with the Xray Image I need to downscale it. Please suggest any code snippet or any tips. I am newbie to this field. Please help me. Sorry if I have not posted it right.

    private BitmapSource LoadDiCom(string fileName, bool OpticalImage)
    {
        int stride = 0;
        int size = 0;
    
        BitmapSource bitmapSource;
        DicomFile dicomFile = new DicomFile(fileName);
        dicomFile.Load(DicomReadOptions.Default);
        int bitsPerPixel = dicomFile.DataSet.GetAttribute(DicomTags.BitsStored).GetInt32(0, 0);
        int width = dicomFile.DataSet.GetAttribute(DicomTags.Columns).GetInt32(0, 0);
        int height = dicomFile.DataSet.GetAttribute(DicomTags.Rows).GetInt32(0, 0);
    
        byte[] bitmapBuffer = (byte[])dicomFile.DataSet.GetAttribute(DicomTags.PixelData).Values;
    
    
        if (!OpticalImage)
        {
            stride = width * 2;
            bitmapSource = BitmapImage.Create(width, height, 96, 96, System.Windows.Media.PixelFormats.Gray16, null, bitmapBuffer, stride);
        }
        else
        {
            stride = width * 3;
            bitmapSource = BitmapImage.Create(width, height, 96, 96, System.Windows.Media.PixelFormats.Rgb24, null, bitmapBuffer, stride);
        }
    
        return bitmapSource;
    }


    • Moved by Bob Ding Friday, April 6, 2018 7:56 AM third party lib related
    Wednesday, March 28, 2018 5:20 AM

All replies

  • Hi Bharani Krishnan,

    According to your description, I am not sure what is DicomFile, is it a 3rd-party library or other?

    In addition, if you want to convert an image to 8-bit color, please refer to the following link.

    https://codereview.stackexchange.com/questions/140323/convert-an-image-to-8-bit-color

    Best regards,

    Zhanglong


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, March 30, 2018 8:13 AM
  • Hi Zhanglong,

    Thanks for the reply. The library I am using to open a DICOM image is ClearCanvas library. And I need specific some code snippet to downgrade the DICOM images from 16 bit depth to 8 bit depth. Some say that DICOM images to be downgraded with the help of look up table (LUT). So is there any tips or codes to do that.

    Thanks in Advance


    Tuesday, April 3, 2018 6:57 AM
  • Hi Bharani Krishnan,

    What's ClearCanvas library, is it a 3rd-party library, and I could not find an API to achieve the requirement. and here is a similar issue for your reference.

    https://stackoverflow.com/questions/49426431/how-to-downgrade-16-bit-depth-gray-scale-to-8-bit-depth-image-rgb-24

    Best regards,

    Zhanglong


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, April 4, 2018 9:12 AM