How To Color Histogram Equalization With C#

This guide shows how to apply color histogram equalization with iterative equalization of the image by using nth root or nth power.


Andraz Krzisnik
How To Color Histogram Equalization With C#

Color histogram equalization is an image processing technique for color images. If you’re familiar with this blog, you might be aware that we’ve already described a version of it, one we used on grayscale images.

In any case, if you’re not familiar, histogram equalization methods enable us to adjust contrast on an image. Furthermore, contrast is the difference between features that make an object stand out from other objects in the background.

Color histogram equalization

Problem we’re tackling here is how can we optimize the contrast, so we can see all the information in the image.

There are also several techniques that deal with this problem. The most common few are histogram equalization, gamma correction and linear contrast correction.

As we mentioned before, we used histogram equalization technique before, where we processed a grayscale image. However, when we’re dealing with grayscale images, we only need to equalize image data in one channel.

So here, we bump into another obstacle, which is processing image data across three color channels. Therefore, we’ll talk about a couple different methods of equalizing a color image.

Methods for color histogram equalization

For a simple solution to our problem, we could just equalize each color channel sub-image separately and put it back together. However, this method usually produces an image with erroneous color.

Another method we could use, starts by converting an image from RGB to HSI color space. This way, we separate all color carrying information from their intensities. Therefore, we can equalize intensity sub-image and put it all back together and convert the image back to RGB color space.

Histogram equalization process that we described in another guide is only effective for equalizing images in separate color channels. However, in this tutorial we’ll be using an iterative equalization of the image by using nth root or power.

color histogram equalization diagram
Histogram equalization iterative method diagram

Code

public static Bitmap EqualizeHistogram(this double[][] hsi)
     {
         double[] intensities = hsi[2];
         //normalize
         double max = 0d;
         for (int i = 0; i < intensities.Length; i++)
         {
             max = Math.Max(max, intensities[i]);
         }
         for (int i = 0; i < intensities.Length; i++)
         {
             intensities[i] /= max;
         }
         //calculate new intensities
         double mean = 0d;
         while (Math.Round(mean, 2) != 0.5)
         {
             mean = intensities.Sum() / intensities.Length;
             double theta = Math.Log(0.5) / Math.Log(mean);
             for (int i = 0; i < intensities.Length; i++)
             {
                 intensities[i] = Math.Pow(intensities[i], theta);
             }
         }
         //normalize to range
         double min = 255d;
         max = 0d;
         for (int i = 0; i < intensities.Length; i++)
         {
             min = Math.Min(min, intensities[i]);
             max = Math.Max(max, intensities[i]);
         }
         for (int i = 0; i < intensities.Length; i++)
         {
             intensities[i] = 255 * (intensities[i] - min) / (max - min);
         }
         //make new image
         double[][] result = { hsi[0], hsi[1], intensities };
         Bitmap res_img = result.HSI2RGB();
         return res_img;
     }

Conclusion

This guide expands on the previous post about histogram equalization by adding a new technique. I hope it was helpful.

You can also download the project and try it out yourself.

Related Articles

C# Tutorial

C# Tutorial: How To Flip an Image Horizontally

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...

Posted on by Andraz Krzisnik
Morphological Processes

How To Make Hit Or Miss Transform Work With C#

Hit or miss transform is a morphological process for shape detection. We need to use two different structuring elements to find the shapes.

Posted on by Andraz Krzisnik