# Chapter 4: Histogram and Backprojection

### a. Histogram

An histogram a spectrum of intensity repartition. Concretely this is a list that contains for each possible value of pixel the number of pixels that have this value. You can say well, but in the end how can it be really useful ? for which purposes ? For instance calculate the histogram of an area on a photography, then the same area on another photography from another point of view for instance is more likely to have the same histogram. So it can be really really useful to make pattern recognition “from scratch”. Indeed OpenCV provide all the needed function to do it thank’s to CalcHist and CompareHist that return a float of the distance (accuracy).

As we have seen in the filter chapter equalizehist function intent modify an image to flatten the image histogram so that the balance between black and white is almost the same. The example below calculate the histogram of a gray picture and  show with a graph the associated histogram (on another picture), then it equalize the picture and recalculate the histogram to show the difference.

Original image:  Equalized image:  ### b. Backprojection

The backprojection is calculated from the histogram. It basically replace every pixel by it’s probability to occur in the image. It is not really useful to use it by hand, but this is used by other algorithm like meanshift. The example below shows the common usage of backprojection associated with Region Of Interest. In this example we select a rectangle in the upper left corner of the image, we compute the histogram and then apply a backprojection on the whole image to detect others parts of the image which have the same histogram.  Note: The result is more or less accurate. To get a better result, you can do it on every channel of a colour image to get a far more accurate result.