As I could not find any good examples of how to do colour Histogram Equalisation (HE) in OpenCV I thought I’d shine some light on it myself. The code I have produced is by no means the most efficient method of HE however I have written it so it is easy to understand. If you find that your HE methods are too slow then leave me a comment and I’ll write up how to make the routines more efficient.
But wait! Firstly, let’s talk a little about what exactly HE is, and more so why you might want to use it for image processing. HE in a nut shell is the ‘flattening’ of a histogram, which usually leads to a global contrast gain. This leads (often) to more vibrant colours, and allows for you to provide a way to make images look better without having to play around with contrast and brightness sliders.

OKay, so now that we’ve got a good idea of what HE is, and why it’s useful let’s talk a little about how it works, and how to equalise an image. Firstly, we need to retrieve the Red, Green and Blue values for each pixel in the image. This is so we can calculate the Luma, which is also known as the image intensity. With the RGB values we do the following:
We then process the Luma channel alone:
We then recover the colour from the colour ratios:
Note: The histogram for your image will currently look something like the following:
Then compute the cumulative distribution function:
Then use the function to assign new pixel values, leaving our resulting histogram to look something like the following:
And there we have it, histogram equalisation – but now the question is: how do we do this in code? I have written a simple image processing application, and all of the source can be viewed / downloaded from here, or you can just jump right to the histogram equalisation method. All code is in C++, and the project has been created in Qt Creator using the QtSDK for the GUI and OpenCV for Image Processing. Enjoy!

