OpenCV  4.1.1
Open Source Computer Vision
Histogram Calculation

Prev Tutorial: Histogram Equalization

Next Tutorial: Histogram Comparison

Goal

In this tutorial you will learn how to:

Note
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!

Code

Explanation

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):

b_hist.at<float>(i)

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

b_hist.at<float>( i, j )

Result

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

    Histogram_Calculation_Original_Image.jpg
  2. Produces the following histogram:

    Histogram_Calculation_Result.jpg