Histogram Equalization Of RGB Images

contrastWhen you capture an image using your phone in the evening without flash, do you see how the image is a bit on the darker side? When you take an image with too many lights around you, the images becomes a bit too bright. Neither of the two situations gives us a good quality picture. The human eye likes contrast in images. What it means it that the bright regions should be really bright and the dark regions should be really dark. Almost all the apps and services you use today include this functionality, at least the good ones do. So how do we take one of these dark (or bright) images and improve its quality?  

How do I make them look nice?

Well, one of the ways to achieve this is by using histogram equalization. When you take an image and apply histogram equalization to it, what you are essentially doing is manipulating the pixel values to make it look more pleasant. You look at all the pixel values in the image, and you extract the histogram of the image. A histogram basically tells you how many pixels you have for each possible pixel value.

histeqIn a typical grayscale image, the values range from 0 to 255. The X-axis on the histogram will contain values from 0 to 255, and the Y-axis will contain the number of pixels in the image for each value. In a dark image, you will have more pixels concentrated towards the lower end of the histogram. For example, the values may range from 0 to 100. In an image, we can actually have values from 0 to 255. So in essence, we are wasting the values from 101 to 255. During equalization, we take the values from 0 to 100 and stretch them to range from 0 to 255. This way, you are utilizing all the available values and the image will have much better contrast.

When you capture a color image, it is usually in the RGB format. Histogram equalization is applicable to a single channel image, and RGB image has three channels (Red, Blue and Green). You cannot just directly apply histogram equalization technique to an RGB image, because it wouldn’t make sense. How do we do it then?

First of all, why can I not apply it directly to an RGB image?

Let’s consider histogram equalization for a simple grayscale image. Since it just a single channel image, you look at the values in that plane and equalize them based on the 2D histogram of the image. Now, RGB images consist of three channels. So naturally, you can just split them into three separate channels, apply histogram equalization on each and then combine them back together, right? Well, not exactly!

non-linearHistogram equalization is a non-linear process. Channel splitting and equalizing each channel separately is incorrect. Equalization involves intensity values of the image, not the color components. So for a simple RGB color image, histogram equalization cannot be applied directly on the channels. It needs to be applied in such a way that the intensity values are equalized without disturbing the color balance of the image. So, the first step is to convert the color space of the image from RGB into one of the color spaces that separates intensity values from color components. Some of the possible options are HSV/HLS, YUV, YCbCr, etc. YCbCr is preferred as it is designed for digital images. Perform histogram equalization on the intensity plane Y. Now convert the resultant YCbCr image back to RGB.

How do I do it in OpenCV?

If you want to implement this and see it in action, read on. Here is the OpenCV C++ code for histogram equalization of color image using YCbCr color space.

Mat equalizeIntensity(const Mat& inputImage)
    if(inputImage.channels() >= 3)
        Mat ycrcb;

        vector<Mat> channels;

        equalizeHist(channels[0], channels[0]);

        Mat result;

        return result;

    return Mat();

You can try it out and see the effect of histogram equalization yourself!


4 thoughts on “Histogram Equalization Of RGB Images

  1. Thanks a lot for the article. The explanation on color image equalization really helped me to understand the concept and write my own code in python!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s