I am making an image viewer using the c# ClearCanvas libraries, and I am trying to display images with their stored window center and window width values applied to the image. To do this, I read the window center, window width, and raw pixel data from the Dicom file which I convert to a byte array and then use the following code to apply the window settings:
int lower_bound = window_center - window_width / 2;
int upper_bound = window_center + window_width / 2;
//copy matrix and do window caluclations
float[] wlpixels = new float[pixels.Length];
for (int i = 0; i < w * h; i++)
{
if (pixels[i] <= lower_bound)
wlpixels[i] = 0;
else if (pixels[i] > upper_bound)
wlpixels[i] = 255;
else
{
wlpixels[i] = ((pixels[i] - lower_bound) / window_width) * 255;
}
}
This has worked like a charm for some images while others, specifically with low or negative window center values, display incorrectly. They display with an abundance of white pixels that aren't supposed to be white.
Am I doing my window/level calculations correctly? If not, how do I do them correctly? If so, what else do I need to do to make this work with all scenarios?
pixels
values, have you accounted for Rescale Intercept (0028,1052) and Rescale Slope (0028,1053)? If I am not mistaken, window level and width correspond to the rescaled pixels, when applicable. – Anders Gustafsson