To give you an ad-free browsing experience, this website is completely funded by donations. If you use the information in this article to solve a problem, entertain yourself, or otherwise improve your life, please consider a small donation to help pay for server costs. Many thanks, and enjoy the site!

Graphics Code

Advanced Image Histograms in VB6 – Stretching and Equalizing

This is the 2nd half of a two-part project.  Part 1 is available here.

As promised, here is the second half of my histogram code project.  In this project, I’ll show you how to stretch a histogram, equalize individual channels, and – most useful of all – equalize an image’s overall luminance.  Cool, eh?

Stretching a Histogram

The most straightforward of these functions is stretching a histogram.  The idea is this: many images stretch from pretty-dark colors to pretty-light colors, but they don’t take advantage of the full luminance spectrum by stretching all the way from pure black to pure light.  (For example, a “washed-out” image typically fails to utilize the entire luminance spectrum.)  To help fix an image like this, it is necessary to adjust the image’s colors to better span all possible brightness values.

Stretching does this by finding the darkest and lightest values in an image, then performing an automatic conversion between that color range (max – min) and the ideal color range (pure white – pure black).

As an example, here is an image that doesn’t span the full brightness spectrum:

Normal Clouds

Notice how the top half of the histogram is empty?  The brightest pixels in this image are only a mid-level gray.

Here is the same image after running a stretch histogram algorithm:

Stretched Clouds

Notice how the histogram now stretches all the way to pure white?  Admittedly, this picture is not the best example of how stretching can be useful – but it’s a good demonstration of what stretching does.

Stretching is a fast and simple function, but its useful is limited.  Take the following image, for example:

Normal Tiger

The colors in this tiger picture stretch all the way from pure black to pure white, but they seem to be more heavily concentrated toward the dark end of the luminance spectrum.  To really fix this picture, we need to redistribute its colors across the spectrum in a more equal way.

Enter equalization.

Equalizing a Histogram

Equalizing is a more complex and time-consuming function than stretching, but it is able to fix problems outside the reach of stretching alone.

Here is the tiger image post-equalization:

Equalized Tiger

See how much more balanced the histogram has become?  As another example, here is the cloud picture from above after both stretching and equalizing:

Stretched and Equalized Clouds

Equalizing has brought out a bunch of details that weren’t apparent in the original image.

Rather than going through the gory details of how histogram equalizing works, I’m going to refer you to the excellent Wikipedia article on the subject:

http://en.wikipedia.org/wiki/Histogram_equalization

Also, I’d like to quote one very applicable comment from the above article (emphasis added by me):

The above describes histogram equalization on a greyscale image. However, it can also be used on color images by applying the same method separately to the Red, Green and Blue components of the RGB color values of the image. Still, it should be noted that applying the same method on the Red, Green, and Blue components of an RGB image may yield dramatic changes in the image’s color balance since the relative distributions of the color channels change as a result of applying the algorithm. However, if the image is first converted to another color space, Lab color space, or HSL/HSV color space in particular, then the algorithm can be applied to the luminance or value channel without resulting in changes to the hue and saturation of the image.

Because of this, I have supplied two equalizing algorithms in this project – one that equalizes any combination of color channels, and another that equalizes only luminance.  The luminance method provides the code for converting between RGB and HSL color spaces…and that code is very complex.  It’s probably worth your while to take that code on faith for now, and maybe at a future date I’ll discuss color space transformations in more detail.

Get the Code!

But enough chat!  Here’s the link for the code:

Download Advanced Histogram Code (91 kb)

 

BSD License
All source code in the above zip file has been released under a BSD License.

 

Related articles:

Discussion (Oldest Comments Displayed First)

Be the first to comment on “Advanced Image Histograms in VB6 – Stretching and Equalizing”

Post a comment

Feel free to use any of these tags in your comment: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>