Image Histograms in VB6 – Part 1

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

I realized today that while I use image histograms in several of the projects on this site (particularly my Real-Time Image Levels demo), I don’t actually provide a stand-alone image histogram viewer.

…Until now, that is.  :)

Image histograms are HUGELY useful in image processing.  In fact, the most basic image processing algorithm – adjusting brightness – is entirely an image histogram process, theoretically speaking.  Many other functions (including contrast, invert, levels, curves, and more) can be described in terms of an image histogram as well.

So what is an image histogram, and why is it useful?

What is a histogram?

Answers.com defines histograms as:

A bar graph of a frequency distribution in which the widths of the bars are proportional to the classes into which the variable has been divided and the heights of the bars are proportional to the class frequencies.

I realize this definition is not entirely useful, but it is important to note that histograms are not used only in image processing.  Histograms are simply a type of graph – nothing more, nothing less.

A simpler way to define histograms is this: a histogram is a bar graph that shows population by category.  Categories are listed, in order, along the x-axis (or horizontally).  The population (or, alternatively, number of occurrences) of each category is listed along the y-axis (or vertically).  In this graph from the 2000 U.S. census, travel time of working Americans is shown as a histogram:

histogram_demo

Simple, eh?

So what is an Image Histogram?

As you can imagine, an image histogram is a type of histogram that tells us something about an image.  In most cases, the categories in an image histogram are brightness levels (from black to white), while the “population” of each category is the number of pixels with that brightness.

To demonstrate, let’s look at two different image histograms.

First, here is a histogram of a dark, cloudy image:

dark_histogram

Notice how the image histogram is heavily weighted to the left?  Because this image is quite dark, the histogram shows much more pixels on the left side of the graph (the dark side) than it does on the right side of the graph (the light side).

Conversely, here is the histogram of a bright image:

light_histogram

While there are some dark regions in the center of the image (indicated by the spike on the far left of our histogram), the bulk of the image is lighter, and as a result the bulk of our histogram is shifted toward the right.

Now that you know what image histograms represent, try out the attached histogram code and see if you can predict rough histogram outlines before loading an image.  (Note that as with all VB6 picture-box-based code, the program will not load images larger than ~2mb.)

Similar Posts:

 

DISCLAIMER: These download files are regularly scanned to ensure they remain free from malicious content. Unfortunately, some virus scanners will flag these .zip files as suspicious simply because they contain source code and/or executable files. I have submitted my projects to a number of companies in an attempt to rectify these false-positives. Some have been cooperative. Others have not. If your virus scanner alerts you regarding these files, please allow the file to be submitted for further analysis (if your program allows for that). This should help ensure that any false-positive warnings gradually disappear for all users.

This site - and its many free downloads - are 100% funded by donations. Please consider a small contribution to fund server costs and to help me support my family. Even $1.00 helps. Thank you!

4 thoughts on “Image Histograms in VB6 – Part 1”

  1. Hello Tanner. Outstanding this project. I started building an access database that suited my porposes, that is, keep a registry of my photos, but I’m a newby in vb and would like to know if it is possible to use this code in access vba. I tried but the cls modules gave me errors when they are called by the forms. I only would like to have a histogram working on a picture box inside my form. Best regards.

    1. Hi Filipe. I’m afraid I don’t know much about what VBA allows, but you should be able to copy-and-paste the code out of the class and directly into your VBA forms. However, whether or not VBA allows API calls is something you’ll have to research…

Comments are closed.