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:
I tried the following:
var intArray = new int[PixelData.Length];
for (int i = 0; i < this.PixelData.Length; i++)
{
intArray[i] = rescaleSlope*this.PixelData[i] + rescaleIntercept;
}
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?
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.