简体   繁体   中英

Wrong output pixel colors (grayscale) using EvilDICOM

I'm using Evil-DICOM to construct a 2d image in unity (ie texture2d). The output pixel values are wrong compared to what I got from other DICOM viewers. I'm new to DICOM development and couldn't figure out what I did wrong. I've been stuck on this for weeks. Any help is appreciated.

I'm using this formula from:
https://www.dabsoft.ch/dicom/3/C.11.2.1.2/

I also read this answer from:
How to Display DICOM images using EvilDICOM in c#?

Known information about the DICOM file I'm using:

Bits Allocated:16
Bits Stored:16
High Bit:15
Rows, Columns:512
Pixel Representation:0 (ie uncompressed)
Window Center:40
Window Width:350
Rescale Intercept:-1024
Rescale Slope:1

//Convert pixel data to 8 bit grayscale
for (int i = 0; i < pixelData.Count; i += 2)
{
    //original data - 16 bits unsigned
    ushort pixel = (ushort)(pixelData[i] * 0xFF + pixelData[i + 1]);

    double valgray = pixel;
    valgray = slope * valgray + intercept;    //modality lut

    if (valgray <= level - 0.5 - (window - 1)/2)
    {
        valgray = 0;
    }
    else if (valgray > level - 0.5 + (window - 1)/2)
    {
        valgray = 255;
    }
    else
    {
        valgray = ((valgray - (level - 0.5)) / (window - 1) + 0.5);
    }

    //Assign valgray to RGBA
    colors[i / 2].r = (byte)(valgray);
    colors[i / 2].g = (byte)(valgray);
    colors[i / 2].b = (byte)(valgray);
    colors[i / 2].a = 0xFF    //Alpha = max
}

The left is my output, the right is output from other DICOM viewer https://drive.google.com/file/d/1IjL48_iZDXAVi4_gzG6fLN3A2td2rwfS/view?usp=sharing

I got the order of bytes in pixeldata inverted. The pixel value should be:

ushort pixel = (ushort)(pixelData[i + 1] * 256 + pixelData[i]);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM