Dharmraj
Dharmraj

Reputation: 174

applying window level functionality on CT images in DIOCM

I am trying to implement window level functionality( To apply bone, brain, lung etc on CT) for DICOM images in my application and implemented formula as per the DICOM specification. I am changing pixel values based on below formula and creating a new image, but images are becoming blank. What am doing wrong and is this correct way to do this. Please help :(:( Thanks

BufferedImage image = input image;

double w = 2500; // Window width
double c = 500; // window Center
double ymin = 0;
double ymax = 255;
double x = 0;
double y = 0;
double slope = dicomObject.get(Tag.RescaleSlope).getFloat(true);
double intercept = dicomObject.get(Tag.RescaleIntercept).getFloat(true);

int width = image.getWidth();
int height = image.getHeight();

double val = c - 0.5 - (w - 1) / 2;

double val2 = c - 0.5 + (w - 1) / 2;

for (int m = 0; m < height; m++) {

    for (int n = 0; n < width; n++) {

        int rgb = image.getRGB(n, m);

        int valrgb = image.getRGB(n, m);
        int a = (0xff000000 & valrgb) >>> 24;
        int r = (0x00ff0000 & valrgb) >> 16;
        int g = (0x0000ff00 & valrgb) >> 8;
        int b = (0x000000ff & valrgb);

        x = a + r + g + b;

        if (x <= val)
            y = ymin;

        else if (x > val2)
            y = ymax;

        else {
            y = ((x - (c - 0.5)) / (w - 1) + 0.5) * (ymax - ymin)+ ymin;
        }
        y = y * slope + intercept;
        rgb = (int) y;
        image.setRGB(n, m, rgb);

    }
}
String filePath = "out put fileName";
ImageIO.write(image, "jpeg", new File(filePath));

Upvotes: 1

Views: 1894

Answers (1)

Witek
Witek

Reputation: 269

First of all whats in your BufferedImage image ? There are three steps you want to take from raw (decopressed) pixel data:

  1. Get stored values - apply BitsAllocated, BitsStored, HighBit transformation. (I guess you image already passed that level)
  2. Get modality values - thats your Slope, Intercept transformation. Ofter this transformation, your data will be in Hounsfield Units for CT.
  3. Then you apply WW/WL (Value Of Interest) transformation, which will transform this window of walues into grayscale color space.

EDIT:

You've got to tell me where did you get "input image" from? After decompression pixel data should be in a byte array of size byte[width*height*2] (for CT Image BitsAllocated is always 16, thus *2). You can get stored values like this:

ushort code = (ushort)((pixel[0] + (pixel[1] << 8)) & (ushort)((1<<bitsStored) - 1));
int value = TwosComplementDecode(code);

Upvotes: 2

Related Questions