Timo Willemsen
Timo Willemsen

Reputation: 8857

Dicom Window width & level formula not giving greyvalues

I'm trying to implement the Window Width and level formula from de Dicom specification in my application. Only it's not returning any grayscales at the moment. The dicom specifies the formula as following:

These Attributes are applied according to the following pseudo-code, where x is the input value, y is an output value with a range from ymin to ymax, c is Window Center (0028,1050) and w is Window Width (0028,1051):

if (x <= c - 0.5 - (w-1)/2), then y = ymin
else if (x > c - 0.5 + (w-1)/2), then y = ymax,
else y = ((x - (c - 0.5)) / (w-1) + 0.5) * (ymax - ymin)+ ymin

So i've translated this into the following c# syntax:

if (pixelData[i] <= wLevel - 0.5 - (wWidth - 1) / 2)
    oColor = 0;
else if (pixelData[i] > wLevel - 0.5 + (wWidth - 1) / 2)
    oColor = 255;
else
    oColor = (int)((pixelData[i] - (wLevel - 0.5)) / (wWidth - 1) + 0.5) * (255 - 0) + 0;

Howevery, the last part of the formula

oColor = (int)((pixelData[i] - (wLevel - 0.5)) / (wWidth - 1) + 0.5) * (255 - 0) + 0;

Only seems to return 0

Example

Anyone sees how this is possible?

Upvotes: 4

Views: 5304

Answers (1)

ruslik
ruslik

Reputation: 14870

The meaning of VOI LUT is to map a given pixel range to displayable values (usually 0..0xFF), using clamping for out of range pixel values.

This means that for a given window/level we can compute the displayable range:

level-window/2 , level + window/2 .

For pixel values that are in that range, linear transformation is used:

((pixel - lower_window_limit) / window) * displayable_range

where lower_window_limit is level - window/2

This -window/2 is missing in your formula.

Upvotes: 7

Related Questions