Saturday, February 4, 2012

How to use a histogram

Proper exposure typically means that you maintain detail in both the bright and dark areas of the image.  A good way to see the distribution of shades from dark to light is by using a histogram.  Many people have seen a histogram either on their camera LCD or in their photo editing software, but if that little mountain-shaped graph is to be more than just a curiosity, you have to learn what it means and how to use it to your advantage.

How Digital Images Are Stored

This 200% crop shows individual pixels
Before we begin, a little technical background is in order.  A digital image is composed of millions of pixels.  Each pixel specifies color using three numbers, each indicating the amount of color in each of the three primary color bands:  red, green, and blue.  Every color in an image is represented by combining appropriate levels of red, green and blue for each pixel.  Lower numbers indicate darker colors and higher numbers indicate lighter colors.

The human eye can only distinguish between 130 or so different shades of each hue.  Any more, and the different shades appear to be a continuous spread.  A single byte in a computer contains 8 bits, which can store numbers from 0-255, making it a convenient container for each band of a pixel.  Expressing a color using fewer than 8 bits per band causes an effect known as "posterization," where you can clearly see the difference between adjacent shades of a given hue.

A 16-color (4-bit) image easily shows the
different shades. The peaks in the
histogram indicate which pixel
values are present in the image.
A JPEG image (like most image formats and most monitors) stores colors as three 8-bit values for each pixel.  Many DSLR's and some advanced point & shoots can also record images in a "raw" format, which is essentially a dump straight off the camera's sensor without any in-camera processing.  Like the sensors themselves, raw images typically have a higher bit depth, meaning they store colors using more than 8 bits per band.  DSLR's currently use either 12 or 14 bits for each RGB band.  Some digital medium format cameras record 16 bits per band.  Each added bit doubles the number of shades recorded for each hue.  A 12-bit image provides 4096 different shades per hue, while a 14-bit image provides 16,384 different shades.  While the extra shades don't matter when printing or displaying an image on screen (the human eye can't appreciate the increase), they do allow you the flexibility to adjust the spread of these shades without causing posterization in ranges that have been stretched.  More on that later.

What Is a Histogram?

A "histogram" is a list of numbers containing one entry for each possible value that a pixel can have (256 entries for an 8-bit image).  Each list entry counts the number of pixels in the image which contain that value.  For example, a monochrome, 10x10 pixel image that is pure black (color value 0) with one white spot (color value 255) will have a histogram containing 99 in the "0" entry and 1 in the "255" entry.  The more evenly distributed the color values are throughout the image, the more similar the values will be at each entry in the histogram.  A very bright image, like a polar bear in its native environment, will have a histogram containing much higher values in its upper entries (200+).  A very dark image, such as the night sky, will contain much higher values in its lower entries (below 50).

To enable quick interpretation, we prefer to display histograms graphically as a vertical bar graph.  Dark entries (low pixel values) are on the left; light entries (high pixel values) are on the right.  The more pixels in the image that contain each value, the taller the vertical line will be at that entry.  When space is limited (as when displaying the histogram for a 14-bit raw image), multiple adjacent entries are summed together and then displayed as a single bar.  Peaks in the histogram indicate pixel values (shades) that are very commonplace; valleys indicate uncommon values.

Practical Application of Histograms

This camera LCD shows the histograms
for the three individual color bands
as well as the combined brightness
Enough theory--now for some real world application.  The LCD screen on your camera can't be trusted to provide an accurate representation of the exposure for the image you just captured, especially in bright sunlight.  In order to see how bright or dim the exposure really is, you must consult the histogram.  Most digital cameras--even point & shoots--have an option to display the histogram for a photo that's been taken.  Many allow you to view the histogram for each of the three bands (red, green, blue) individually.  With a quick glance at the histogram, you can immediately tell whether your highlights (high pixel values) are too dim or whether they're blown out, eliminating any detail.

A histogram in which the values at the bright (right) end taper down to zero far below the right end of the scale indicates an image that has no truly bright pixels.  Such an image will likely also have a high concentration of dark pixels, and the far left end of the scale will have a peak.  There will be no discernible detail in most of these dark areas.

A histogram in which the highest levels are all bunched up against the right end will contain many areas which are very bright and lack any detail.

Click image for a full size 3D animation of a
2-dimensional histogram representing GCxGC
gas chromatography data (my thesis research).
By contrast, the histograms from digital
photos are only 1-dimensional.
A histogram with a perfect spread will have relatively even levels throughout the entire histogram, but then taper off right at both ends.  This indicates no blown out highlights or empty shadows.  Of course, the desired look for a given photo may require large amounts of dark or light areas, so the perfect histogram may not indicate a perfect looking photo.  The histogram is merely a tool to show you how your pixel values are distributed and whether you're likely to have lost detail at one end or the other.

If your histogram indicates that you've blown out your highlights (a large peak at the right side), you can tell your camera to underexpose the next shot.  This will pull all the pixel values lower, spreading out those higher values and leaving fewer pixels that are blown out.  The result is more discernible detail in the bright areas of your image.

Curves Adjustment

Most photo editing software will allow you to adjust the brightness of an image using what's typically called a "curves adjustment."  This tool displays your original histogram and allows you to draw a curve across it that redefines the brightness of each pixel value.  The curves tool uses a rectangular window to represent a 2D graph.  The bottom (horizontal) edge of the graph represents the original pixel values, and the original histogram is typically shown along this line.  The vertical axis represents the new pixel values. The left and lower ends indicate dark values while the right and upper ends represent bright values.  In the examples shown below, the newly adjusted histogram is shown above the original one.  These screen shots were taken from Digikam, my primary photo management application.

Properly exposed image with no adjustment
The curve that you draw runs from the lower left to the upper right.  To determine the new value for an old pixel value, draw an imaginary line straight up from the old pixel's location on the horizontal axis until it hits the curve, then draw another line straight over to the left edge.  The location at which the line hits the left edge indicates the new pixel value.  A straight line between the two corners indicates a direct mapping between the new and old values, so the image is unchanged.  If your imaginary line hits the left edge at a point that's lower than its original position (meaning the curve was drawn below the original diagonal), then that pixel value will become darker in the new image.  If the line hits the left edge higher than its original location, then that pixel value will get brighter.

Same image, adjusted darker using curves
Same image, adjusted brighter using curves

You can typically make this curve as wiggly as you like in order to change the brightness in only certain circumstances.  Examples include making an "S" curve to add an area of high contrast or pulling just the bright pixels down lower in order to add definition to a highlight that's nearly blown out.

Same image, stretched contrast using curves.
Note the posterization in the middle of the histogram.

An 8-bit JPEG image only has 256 different shades available.  If your curves adjustment is too dramatic, certain parts of the histogram will get compressed (by a more horizontal curve) and other parts will become more sparse (by a more vertical curve).  These sparse areas will result in large jumps between adjacent pixel values in the image, causing posterization.  To make sure your original image has plenty of information available to fill in these gaps, set your camera to record raw images whenever you plan to do any curves adjustment (or set it that way all the time, just to be safe).  A 12-bit raw image can distinguish 16 discrete shades for every one shade in an 8-bit image, which gives much smoother spreads during dramatic curves adjustments.


Sensor Noise and "Expose to the Right"

Noise levels at low and high ISO settings
One popular application for histogram modification is to increase the signal to noise ratio or decrease the amount of sensor noise present in an image in order to create the cleanest image possible.  Digital noise manifests itself as a random amount (positive or negative) that's added onto each pixel value recorded by the camera's sensor.  The higher your ISO, the larger this noise value may be, and the larger variety of pixel values you'd get if you were to photograph an object of a single, solid color.  Because digital noise is additive rather than multiplicative, it has a much more significant effect on darker pixels (those which have smaller values) than it does on brighter pixels (those which have larger values).  Noise shows up as speckling in shadow areas, especially in high-ISO photos.  If you increase these dark pixel values using a curves adjustment (a multiplicative operation), the noise will become more exaggerated as it gets remapped higher up toward the medium tonal range.  If you decrease a noisy pixel value, the percentage of noise in the original pixel value will remain constant, but the total amount of the noise will be decreased because the pixel value itself is being decreased.

The technique known as "exposing to the right" attempts to reduce the effect of sensor noise in an image and increase the amount of detail available in darker shades.  The basic idea is exactly as the name implies:  expose your image in the camera such that the histogram is as far to the right as possible without blowing out your highlights.  This records your "true" pixel values using the largest numbers possible (255 at the brightest).  The noise will still remain at its regular low values (say 5-10).  The initial image on the camera may seem far too bright, but that's OK, because your histogram assures you that you haven't lost any detail to blown highlights.  You can then use a curves adjustment on your computer to darken the image however you like.  The darker you make it, the smaller every pixel value will become, and the lower your noise will become.  While this technique can produce very clean images, the down side is that it requires postprocessing to be done on every single image.  The JPEG that comes straight off your camera is worthless.  As with any histogram modification (via curves or otherwise), you'll get much smoother results if you start off with a 12- or 14-bit raw image than if you start with only an 8-bit JPEG.

Hopefully this has given you a better idea of what a histogram means and how you can effectively use it to improve the images you create.  If you've got any questions, please speak up in the comments section below.  My Master's degree thesis and several jobs I've held over the years dealt directly with this subject (re: the pretty red & green 3D graphics above), so I'm pretty familiar with how it all works.  I've tried to simplify the subject for this article, but I can get as technical as you'd like if you've got more advanced questions.

This article was originally written for Jedi.com in January 2009.

1 comment:

  1. This is great information. It will take me awhile to process it, but I thank you for pointing me in this direction.

    ReplyDelete

Please leave your comment below. Comments are moderated, so don't be alarmed if your note doesn't appear immediately. Also, please don't use my blog to advertise your own web site unless it's related to the discussion at hand.