# Window Width /Level Formula

• 06-17-2008, 04:24 AM
unix
Window Width /Level Formula
Dear All

Kindly guide how to apply values for window width/window level for
CT,MR,CR,US images and also provide if there are different formula for
each of them...

with regards
Dinesh K
• 06-17-2008, 05:18 PM
unix
Re: Window Width /Level Formula
On Jun 16, 10:24*pm, dinesh.karumat...@gmail.com wrote:[color=blue]
> Dear All
>
> Kindly guide how to apply values for window width/window level for
> CT,MR,CR,US images and also provide if there are different formula for
> each of them...
>
>
> with regards
> Dinesh K[/color]

What I do is to get the histogram of the image and then integrate.
hmm.. maybe posting code is easier to explain. This sets the lower
level to 5% and the higher level to 95%
We pass in a matrix of image intensity values, which gets converted
into a vector or intensity values, that vector is then sorted.
You can adapt this to 8bit or 12bit or 16bit images quite simply...

void GetWindowLevels(float *img, int nsamples, float & low, float &
high)
{
float sorted[nsamples];
float min, max;

memcpy(sorted, img, nsamples * sizeof(float));
qsort(sorted, nsamples, sizeof(float), floatcmp);

min = sorted[0];
max = sorted[nsamples-1];
if (min == max)
{
low = 0;
high = 1;
return;
}

double sigma=0;
int unique=1;
float prev=sorted[0];

for (int i=0; i < nsamples; ++i)
sorted[i] = normalize(sorted[i], min, max);

for (int i=0; i < nsamples; ++i)
{
sigma += sorted[i];
if (sorted[i] != prev)
++unique;
}

double fivepc = sigma * .05;
double nintyfivepc = sigma * .95;
sigma = 0;
low=0;
high=1;

for (int i=0; i < nsamples; ++i)
{
if (sigma < fivepc)
low = sorted[i];
if (sigma < nintyfivepc)
high = sorted[i];
sigma += sorted[i];
}
//std::cerr << "There are " << unique << " unique values" <<
std::endl;
//std::cerr << "Sigma = " << sigma << " 5%=" << fivepc << " 95% = "
<< nintyfivepc << " low = " << low << " high = " << high << std::endl;
}

// return 0 to 1
float normalize(float value, float min, float max)
{
float norm;
norm = (value-min) / (max-min);
return(norm);
}