Normalization is useful in digital image processing when we want to change the range of pixel intensity values (be it hue, saturation, brightness, etc). This blog post provides an example of how one might normalize the brightness of an image using the C# programming language.

Image brightness normalization is a two-pass procedure: the first pass determines the minimum and maximum brightness of the image, and the second pass uses this information to apply a normalization formula to every pixel. In pseudo-code, the image brightness normalization formula applied to each pixel is basically the following:

normalizedBrightness = (pixelBrightness - minBrightness) / (maxBrightness - minBrightness)

Below is a simple example in C#:

Note: If performance is priority, check out LockBits. It is faster than the GetPixel()/SetPixel() approach used above (but, albeit, more convoluted).

You may be wondering what ColorConverter.ColorFromAhsb() does and where it comes from. In C#, image pixels are represented using the RGB color model, but when we call GetBrightness() the value returned is expressed in terms of the HSB color model. Therefore, once we calculate the normalized brightness of a pixel, we need to convert it from HSB back to RGB. Since the .NET Framework does not provide an easy way to do this, I have used a HSB-to-RGB conversion method originally detailed here. For posterity, I have included the code below:

And for those still reading, this is the kind of visual effect that this image brightness normalization code produces:

Share →