### Histogram Equalization And Magic Behind It

C# tutorial on how to achieve histogram equalization on an image.

Back to Latest Articles
###
Histogram Equalization And Magic Behind It

###
C# Tutorial: How To Use Log Transformation on Images

###
C# Tutorial: How To Flip an Image Horizontally

###
C# Tutorial: How To Scale Images Without Deforming Them

C# Tutorial

C# tutorial on how to achieve histogram equalization on an image.

C# Tutorial

What is Log Transformation Anyway? Log transformation in image processing is a part of gray level transformations. It works by transforming each pixel individually. It is usually...

C# Tutorial

Where Do We Start? There are numerous ways you can flip an image horizontally. It’s pretty basic functionality in image processing. I wanted to challenge myself and not just...

C# Tutorial

Let’s Get Started A very basic problem that we face in image processing is how to scale images without making them look deformed when we want to make it of an arbitrary...

Histogram Equalization And Magic Behind It

This tutorial is meant to demonstrate how histogram equalization works. This is a continuation on contrast stretch articles I’ve made in the past. An article on histogram equalization already exists on here, but it is made in “unsafe” code and that just kinda bothers me a little bit.

This article is accompanied by code, that manipulates bytes of an image with a “lockbits” method. This method basically stores image into a one dimensional byte array.

Histograms are charts to visualize data in columns.We can create a histogram from an image, where vertical axis shows us number of pixels and horizontal axis shows color intensities that these pixels have.

Images that need to be processed here will have a washed out look. This is because the range of intensities in this image doesn’t span the whole range of possible intensities. Histogram equalization basically stretches this range to cover complete spectrum of possible intensities.

Sounds familiar doesn’t it? We talked about the same effect in article for normalization, but the key difference here is that we use a different equation to achieve this effect. As it turns out normalization is not as effective in expanding the range as histogram equalization is.

**pn = number of pixels with intensity n / total number of pixels**

pn represents the probability of each intensity in an image.

And finally the equation that does all the heavy lifting,

where k represents intensity value and L all possible intensities.

For example, if we have a pixel with intensity value of 100, we need to sum all probabilities pn up to the probability for pixels with intensity value of 100. Then we need to multiply this summation by 255 and round it down to the closest integer value. Since we are working with images of 32 bit pixel format, the max intensity value for each channel is 255, hence the multiplication with this number.

```
public static Bitmap HistEq(this Bitmap img)
{
int w = img.Width;
int h = img.Height;
BitmapData sd = img.LockBits(new Rectangle(0, 0, w, h),
ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);
int bytes = sd.Stride * sd.Height;
byte[] buffer = new byte[bytes];
byte[] result = new byte[bytes];
Marshal.Copy(sd.Scan0, buffer, 0, bytes);
img.UnlockBits(sd);
int current = 0;
double[] pn = new double[256];
for (int p = 0; p < bytes; p += 4)
{
pn[buffer[p]]++;
}
for (int prob = 0; prob < pn.Length; prob++)
{
pn[prob] /= (w * h);
}
for (int y = 0; y < h; y++)
{
for (int x = 0; x < w; x++)
{
current = y * sd.Stride + x * 4;
double sum = 0;
for (int i = 0; i < buffer[current]; i++)
{
sum += pn[i];
}
for (int c = 0; c < 3; c++)
{
result[current + c] = (byte)Math.Floor(255 * sum);
}
result[current + 3] = 255;
}
}
Bitmap res = new Bitmap(w, h);
BitmapData rd = res.LockBits(new Rectangle(0, 0, w, h),
ImageLockMode.WriteOnly, PixelFormat.Format32bppArgb);
Marshal.Copy(result, 0, rd.Scan0, bytes);
res.UnlockBits(rd);
return res;
}
```

## Comments