# Color histogram

Not to be confused with Image histogram.

In image processing and photography, a color histogram is a representation of the distribution of colors in an image. For digital images, a color histogram represents the number of pixels that have colors in each of a fixed list of color ranges, that span the image's color space, the set of all possible colors.

The color histogram can be built for any kind of color space, although the term is more often used for three-dimensional spaces like RGB or HSV. For monochromatic images, the term intensity histogram may be used instead. For multi-spectral images, where each pixel is represented by an arbitrary number of measurements (for example, beyond the three measurements in RGB), the color histogram is N-dimensional, with N being the number of measurements taken. Each measurement has its own wavelength range of the light spectrum, some of which may be outside the visible spectrum.

If the set of possible color values is sufficiently small, each of those colors may be placed on a range by itself; then the histogram is merely the count of pixels that have each possible color. Most often, the space is divided into an appropriate number of ranges, often arranged as a regular grid, each containing many similar color values. The color histogram may also be represented and displayed as a smooth function defined over the color space that approximates the pixel counts.

Like other kinds of histograms, the color histogram is a statistic that can be viewed as an approximation of an underlying continuous distribution of colors values.

## Overview

Color histograms are flexible constructs that can be built from images in various color spaces, whether RGB, rg chromaticity or any other color space of any dimension. A histogram of an image is produced first by discretization of the colors in the image into a number of bins, and counting the number of image pixels in each bin. For example, a Red–Blue chromaticity histogram can be formed by first normalizing color pixel values by dividing RGB values by R+G+B, then quantizing the normalized R and B coordinates into N bins each. A two-dimensional histogram of Red-Blue chromaticity divided into four bins (N=4) might yield a histogram that looks like this table:

 red 0-63 64-127 128-191 192-255 blue 0-63 43 78 18 0 64-127 45 67 33 2 128-191 127 58 25 8 192-255 140 47 47 13

A histogram can be N-dimensional. Although harder to display, a three-dimensional color histogram for the above example could be thought of as four separate Red-Blue histograms, where each of the four histograms contains the Red-Blue values for a bin of green (0-63, 64-127, 128-191, and 192-255).

The histogram provides a compact summarization of the distribution of data in an image. The color histogram of an image is relatively invariant with translation and rotation about the viewing axis, and varies only slowly with the angle of view.[1] By comparing histograms signatures of two images and matching the color content of one image with the other, the color histogram is particularly well suited for the problem of recognizing an object of unknown position and rotation within a scene. Importantly, translation of an RGB image into the illumination invariant rg-chromaticity space allows the histogram to operate well in varying light levels.

## Definition

1.What is a histogram?

A histogram is a graphical representation of the number of pixels in an image. In a more simple way to explain, a histogram is a bar graph, whose X-axis represents the tonal scale(black at the left and white at the right), and Y-axis represents the number of pixels in an image in a certain area of the tonal scale. For example, the graph of a luminance histogram shows the number of pixels for each brightness level(from black to white), and when there are more pixels, the peak at the certain luminance level is higher.

2.What is a color histogram?

A color histogram of an image represents the distribution of the composition of colors in the image. It shows different types of colors appeared and the number of pixels in each type of the colors appeared. The relation between a color histogram and a luminance histogram is that a color histogram can be also expressed as “Three Color Histograms”, each of which shows the brightness distribution of each individual Red/Green/Blue color channel.

## Characteristics of a color histogram

Note that a color histogram focuses only on the proportion of the number of different types of colors, regardless of the spatial location of the colors. The values of a color histogram are from statistics. They show the statistical distribution of colors and the essential tone of an image.

Note that in general, as the color distributions of the foreground and background in an image are different, there might be a bimodal distribution in the histogram.

Also note that for the luminance histogram alone, there is no perfect histogram and in general, the histogram can tell whether it is over exposure or not, but there are times when you might think the image is over exposed by viewing the histogram; however, in reality it is not.

For example, there are some “high key” images(an image with most pixels of the color white or near white) where most details are in the highlights of the image, that is, the histogram will show a majority of the pixels on the right side and this is not over exposure. The photo in Example2 is a highkey image.

## Principles of the formation of a color histogram

How can a color histogram be formed?

The formation of a color histogram is rather simple. From the definition above, we can simply count the number of pixels for each 256 scales in each of the 3 RGB channel, and plot them on 3 individual bar graphs.

In general, a color histogram is based on a certain color space, such as RGB or HSV. When we compute the pixels of different colors in an image, if the color space is large, then we can first divide the color space into certain numbers of small intervals. Each of the intervals is called a bin. This process is called color quantization. Then, by counting the number of pixels in each of the bins, we get the color histogram of the image.

The concrete steps of the principles can be viewed in Example 2.

## Example

Given the following image of a cat (an original version and a version that has been reduced to 256 colors for easy histogram purposes), the following data represents a color histogram in the RGB color space, using four bins. Bin 0 corresponds to intensities 0-63, bin 1 is 64-127, bin 2 is 128-191, and bin 3 is 192-255.

A picture of a cat
A picture of a cat reduced to 256 colors in the RGB color space
Red Green Blue Pixel Count
0 0 0 7414
0 0 1 230
0 0 2 0
0 0 3 0
0 1 0 8
0 1 1 372
0 1 2 88
0 1 3 0
0 2 0 0
0 2 1 0
0 2 2 10
0 2 3 1
0 3 0 0
0 3 1 0
0 3 2 0
0 3 3 0
1 0 0 891
1 0 1 13
1 0 2 0
1 0 3 0
1 1 0 592
1 1 1 3462
1 1 2 355
1 1 3 0
1 2 0 0
1 2 1 101
1 2 2 882
1 2 3 16
1 3 0 0
1 3 1 0
1 3 2 0
1 3 3 0
2 0 0 1146
2 0 1 0
2 0 2 0
2 0 3 0
2 1 0 2552
2 1 1 9040
2 1 2 47
2 1 3 0
2 2 0 0
2 2 1 8808
2 2 2 53110
2 2 3 11053
2 3 0 0
2 3 1 0
2 3 2 170
2 3 3 17533
3 0 0 11
3 0 1 0
3 0 2 0
3 0 3 0
3 1 0 856
3 1 1 1376
3 1 2 0
3 1 3 0
3 2 0 0
3 2 1 3650
3 2 2 6260
3 2 3 109
3 3 0 0
3 3 1 0
3 3 2 3415
3 3 3 53929

## Example2

Here is an example of 3 color histograms for each RGB channel of an image based on RGB color space.

Following the definition and the principle of the formation of a color histogram as mentioned above, a RGB-based color histogram can be expressed as 3 color histograms for each individual R,G,B color channel.

The values of a color histogram are from statistics. Thus, the frequency of a certain luminance level can be viewed as the appearance probability of the certain luminance level. Therefore, the histogram is corresponding to the cumulative sum of pdf(probability density function).

For each of the three R,G,B color histogram, we count the number of pixels in each 256 luminance level and display them.

The processes are as follows:

1. Say we have taken a certain photo and the data for the photo is m*n*3, where 3 is for the 3 layers of Red, Green and Blue.

2. Take one of the 3 layers, for example, we take red layer, now we need to compute the number of pixels in each 256 luminance level and display them(bin=256).

3. Note that the values of a color histogram are from statistics. Thus, the histogram is corresponding to the cumulative sum of probability density function.

4. Assume H(r) denotes the number of pixels for a certain luminance level r. A(r) is the total number of pixels in the image.

A(r)= $\int_0^r H(r)\,dr$

A_0= $\int_0^b H(r)\,dr$

b=255

5. Then, the pdf(probability density function) p(r) for each of the levels is:

p(r)=H(r)/A_0

Where H(r) can be computed from H(r)=dA(r)/dr

6. Now, we can compute the cumulative sum of pdfs as the following:

P(r)=$1/A_0* \int_0^r H(r)\,dr$

7. For discrete distributions, we can set dr=1, then p(r)=dA/A_0, if the total number of pixels is N, n_k denotes the number of pixels in the level r_k, then p(r_k )=n_k/N

P(r_k)=$\sum_{i \mathop =0}^k n_i/N$

8. For the computation of a luminance histogram for a certain RGB layer, according to the definition, if the image has L number of luminance levels(L usually equals 256, that is, 8-bits), then for an image f(x,y) whose size is M*N, the steps are:

1) Set hisk[k]=0; k=0,1,…,L-1;

2) For each point (x,y), f(x,y) denotes the luminance level for the point, for each luminanced level add the number of appearance of f(x,y), that is, hist[f(x,y)]++

Where x=0,1,…,M-1, y=0,1…,N-1

3) Standard the hist[f(x,y)] by dividing M*N if needed.

9. Now, we have a histogram for a certain layer(say red), what we need to do next is to repeat process 3 to 8 for the other two layers to get the luminance histogram for them too. Then we can have the color histogram for all three RGB layers of an image.

Below is the Matlab Codes for showing each R,G,B channels of a chosen image:

BW = Img;

%compute histogram for each of the 3 RGB layers

R=BW(:,:,1);

[REDcounts,x] = imhist(R);

G=BW(:,:,2);

[Greencounts,y] = imhist(G);

B=BW(:,:,3);

[Bluecounts,z] = imhist(B);

figure;

subplot(131);imhist(R);title('histogram of Red');

subplot(132);imhist(G);title('histogram of Green');

subplot(133);imhist(B);title('histogram of Blue');

## Example3

Application in camera:

Nowadays, some cameras have the ability of showing the 3 color histograms when we take photos.

We can examine clips(spikes on either the black or white side of the scale) in each of the 3 RGB color histograms. If we find one or more clipping on a channel of the 3 RGB channels, then this would result in a loss of detail for that color.

To illustrate this, consider this example:

1. We know that each of the 3 R,G,B channels has a range of values from 0-255(8 bit). So consider a photo that has a luminance range of 0-255.

2. Assume the photo we take is made of 4 blocks that are adjacent to each other and we set the luminance scale for each of the 4 blocks of original photo to be 10, 100, 205, 245. Thus, the image looks like the first figure on the right.

3. Then, we over expose the photo a little, say, the luminance scale of each block is increased by 10. Thus, the luminance scale for each of the 4 blocks of new photo is 20, 110, 215, 255. Then, the image looks like the second figure on the right.

There is not much difference between figure 8 and figure 9, all we can see is that the whole image becomes brighter(the contrast for each of the blocks remain the same).

4. Now, we over expose the original photo again, this time the luminance scale of each block is increased by 50. Thus, the luminance scale for each of the 4 blocks of new photo is 60, 150, 255, 255. The new image now looks like the third figure on the right.

Note that the scale for last block is 255 instead of 295, for 255 is the top scale and thus the last block has clipped! When this happens, we lose the contrast of the last 2 blocks, and thus, we cannot recover the image no matter how we adjust it.

To conclude, what can be learnt from this is that when taking photos with cameras that has histograms, always keep the brightest tone in the image below the largest scale 255 on the histogram in order to avoid of losing details.

## Drawbacks and other approaches

The main drawback of histograms for classification is that the representation is dependent of the color of the object being studied, ignoring its shape and texture. Color histograms can potentially be identical for two images with different object content which happens to share color information. Conversely, without spatial or shape information, similar objects of different color may be indistinguishable based solely on color histogram comparisons. There is no way to distinguish a red and white cup from a red and white plate. Put another way, histogram-based algorithms have no concept of a generic 'cup', and a model of a red and white cup is no use when given an otherwise identical blue and white cup. Another problem is that color histograms have high sensitivity to noisy interference such as lighting intensity changes and quantization errors. High dimensionality (bins) color histograms are also another issue. Some color histogram feature spaces often occupy more than one hundred dimensions.[2]

Some of the proposed solutions have been color histogram intersection, color constant indexing, cumulative color histogram, quadratic distance, and color correlograms. Although there are drawbacks of using histograms for indexing and classification, using color in a real-time system has several advantages. One is that color information is faster to compute compared to other invariants. It has been shown in some cases that color can be an efficient method for identifying objects of known location and appearance.

Further research into the relationship between color histogram data to the physical properties of the objects in an image has shown they can represent not only object color and illumination but relate to surface roughness and image geometry and provide an improved estimate of illumination and object color.[3]

Usually, Euclidean distance, histogram intersection, or cosine or quadratic distances are used for the calculation of image similarity ratings.[4] Any of these values do not reflect the similarity rate of two images in itself; it is useful only when used in comparison to other similar values. This is the reason that all the practical implementations of content-based image retrieval must complete computation of all images from the database, and is the main disadvantage of these implementations.

Another approach to representative color image content is two-dimensional color histogram. A two-dimensional color histogram considers the relation between the pixel pair colors (not only the lighting component).[5] A two-dimensional color histogram is a two-dimensional array. The size of each dimension is the number of colors that were used in the phase of color quantization. These arrays are treated as matrices, each element of which stores a normalized count of pixel pairs, with each color corresponding to the index of an element in each pixel neighborhood. For comparison of two-dimensional color histograms it is suggested calculating their correlation, because constructed as described above, is a random vector (in other words, a multi-dimensional random value). While creating a set of final images, the images should be arranged in decreasing order of the correlation coefficient.

The correlation coefficient may also be used for color histogram comparison. Retrieval results with correlation coefficient are better than with other metrics.[6]

## Intensity histogram of continuous data

The idea of an intensity histogram can be generalized to continuous data, say audio signals represented by real functions or images represented by functions with two-dimensional domain.

Let $f \in L^1(\mathbb{R}^n)$ (see Lebesgue space), then the cumulative histogram operator $H$ can be defined by:

$H(f)(y) = \mu\{x : f(x)\le y\}$.

$\mu$ is the Lebesgue measure of sets. $H(f)$ in turn is a real function. The (non-cumulative) histogram is defined as its derivative.

$h(f) = H(f)'$.

## References

1. ^ Shapiro, Linda G. and Stockman, George C. "Computer Vision" Prentice Hall, 2003 ISBN 0-13-030796-3
2. ^ Xiang-Yang Wang, Jun-Feng Wu1 and Hong-Ying Yang "Robust image retrieval based on color histogram of local feature regions" Springer Netherlands, 2009 ISSN 1573-7721
3. ^ Anatomy of a color histogram; Novak, C.L.; Shafer, S.A.; Computer Vision and Pattern Recognition, 1992. Proceedings CVPR '92., 1992 IEEE Computer Society Conference on 15–18 June 1992 Page(s):599 - 605 doi:10.1109/CVPR.1992.223129
4. ^ Integrated Spatial and Feature Image Systems: Retrieval, Analysis and Compression; Smith, J.R.; Graduate School of Arts and Sciences, Columbia University, 1997
5. ^ Effectiveness estimation of image retrieval by 2D color histogram; Bashkov, E.A.; Kostyukova, N.S.; Journal of Automation and Information Sciences, 2006 (6) Page(s): 84-89
6. ^ Content-Based Image Retrieval Using Color Histogram Correlation; Bashkov, E.A.; Shozda, N.S.; Graphicon proceedings, 2002 Page(s): [1]