1
votes

I'm implementing a DICOM-Image Viewer in C#. I don't (because I'm not allowed to) use any frameworks or libraries that do the image processing for me.

With which algorithm can I calculate the windowing? (With Window Center and Window Width)

I have the following things to work with:

  • The pixel data is stored as a byte[]
  • the pixels are stored in Hounsfield Unit (see first code)

I tried the following:

Calculating Rescale: (Edited)

var intArray = new int[PixelData.Length];
for (int i = 0; i < this.PixelData.Length; i++)
{
  intArray[i] = rescaleSlope*this.PixelData[i] + rescaleIntercept;
}

Calculating Windowing

var lowestVisibleValue = (windowCenter - 0.5 - ((windowWidth - 1) / 2));
var highestVisibleValue = (windowCenter - 0.5 + ((windowWidth - 1) / 2));

for (int i = 0; i < this.PixelData.Length; i++)
{
  if (intArray[i] <=  lowestVisibleValue)
  {
    PixelData[i] = 0;
  }
  else if (intArray[i] > highestVisibleValue)
  {
    PixelData[i] = 255;
  }
  else
  {
    PixelData[i] =(byte)((PixelData[i] - (windowCenter - 0.5))/((windowWidth -1) + 0.5)*(highestVisibleValue - lowestVisibleValue) + lowestVisibleValue);
  }
}

The second code return 0 almost all the time, so the image is almost black. Any ideas what I'm doing wrong?

EDIT 21.02.2017

I edited the code following Paolo Brandolis suggestion. I store the HU in an int[].
The Rescale Intercept is e.g. -1024 and Rescale Slope is 1. When Window Center is 40 and Window Width is 350, everything is black.
So something is still wrong. Any suggestions?

1

1 Answers

1
votes

I think that the problem is caused by the fact that the result of the slope/intercept is truncated to a byte and the higher part of the value is lost

Housfield values don't fit in a single byte. Additionally, the Hounsfield values may be signed.