OpenCV  4.5.3-pre
Open Source Computer Vision
Histogram Calculation

Prev Tutorial: Histogram Equalization

Next Tutorial: Histogram Comparison

Original author Ana Huamán
Compatibility OpenCV >= 3.0


In this tutorial you will learn how to:

In the last tutorial (Histogram Equalization) we talked about a particular kind of histogram called Image histogram. Now we will considerate it in its more general concept. Read on!

What are histograms?

What OpenCV offers you

For simple purposes, OpenCV implements the function cv::calcHist , which calculates the histogram of a set of arrays (usually images or image planes). It can operate with up to 32 dimensions. We will see it in the code below!



our input is the image to be divided (this case with three channels) and the output is a vector of Mat )

we use the expression (C++ code):<float>(i)

where \(i\) indicates the dimension. If it were a 2D-histogram we would use something like:<float>( i, j )


  1. Using as input argument an image like the one shown below:

  2. Produces the following histogram: