OpenCV Learning (calculate the histogram of images)

Source: Internet
Author: User

OpenCV Learning (calculate the histogram of images)
OpenCV calculates the histogram of the image

Calculating the histogram of an image is a very common operation in the field of image processing. OpenCV provides the calcHist function to calculate the image histogram. However, this function is quite difficult to tell the truth. After studying it for a long time, I learned some basic usage.

The prototype of the calcHist Function C ++ is as follows:

void calcHist(const Mat* images,     int nimages,     const int* channels,     InputArray mask,     SparseMat& hist,     int dims,     const int* histSize,     const float** ranges,     bool uniform=true,     bool accumulate=false )

The meanings of parameters are as follows:

Images: the pointer to the input image. It can be multiple images, but all images must have the same depth (CV_8U or CV_32F ). A pair of images can have multiple channels.

Nimages: Number of input images.

Channels: the array of channels used to calculate the histogram.

Mask: mask. If the mask is not empty, it must be an 8-bit (CV_8U) array, and its size is the same as the size of the input image, A non-zero vertex is used to calculate the histogram.

Hist: output parameter, calculated histogram.

Dims: the dimension of the histogram. It cannot be greater than CV_MAX_DIMS (32 in the current version ).

HistSize: number of elements in the histogram on each dimension.

Ranges: the range of each dimension of the histogram to be calculated. Ranges is an array pointing to an array. The array we call it to is the ranges element, and the element size is the length of the array.

If the parameter uniform is true, the element size in ranges is 2 (that is, ranges points to a series of arrays with a length of 2), which stores the upper and lower limits of each dimension; if the parameter uniform is false, then the ranges element size is the number of bin, storing color values, that is, the coordinate values of each dimension are not necessarily even, manually specified.

Uniform: true indicates that each dimension of the histogram to be calculated has a uniform value based on its range and size. If false, it indicates that each dimension of the histogram is not evenly distributed. For details, refer to the ranges parameter.

Accumulate: Indicates whether to clear the passed hist. If not cleared, the histogram of multiple images can be accumulated.

Looking at the above explanation, we should be able to see how troublesome this function is. However, we generally only use some of the most basic functions. For example, calculate the histogram of a single channel grayscale image. The value range of the histogram is generally 0 to 255. In this case, we can encapsulate this function again to make it easier to use.

The following code comes from OpenCV 2 Computer Vision Application Programming Cookbook. The author of the book writes a class called Histogram1D.

The declaration of this class is as follows:

class Histogram1D{public:    Histogram1D()    {        // Prepare arguments for 1D histogram        histSize[0] = 256;        hranges[0] = 0.0;        hranges[1] = 255.0;        ranges[0] = hranges;        channels[0] = 0; // by default, we look at channel 0    }    ~Histogram1D();    // Computes the 1D histogram and returns an image of it.    cv::Mat getHistogramImage(const cv::Mat &image);    // Computes the 1D histogram.    cv::MatND getHistogram(const cv::Mat &image);private:    int histSize[1]; // number of bins    float hranges[2]; // min and max pixel value    const float* ranges[1];    int channels[1]; // only 1 channel used here};

The getHistogram function is used to calculate the histogram. The implementation is as follows:

// Computes the 1D histogram.cv::MatND Histogram1D::getHistogram(const cv::Mat &image){    cv::MatND hist;    // Compute histogram    cv::calcHist(&image,        1, // histogram from 1 image only        channels, // the channel used        cv::Mat(), // no mask is used        hist, // the resulting histogram        1, // it is a 1D histogram        histSize, // number of bins        ranges // pixel value range    );    return hist;}

The getHistogramImage function is used to generate the histogram image:

// Computes the 1D histogram and returns an image of it.cv::Mat Histogram1D::getHistogramImage(const cv::Mat &image){    // Compute histogram first    cv::MatND hist = getHistogram(image);    // Get min and max bin values    double maxVal = 0;    double minVal = 0;    cv::minMaxLoc(hist, &minVal, &maxVal, 0, 0);    // Image on which to display histogram    cv::Mat histImg(histSize[0], histSize[0], CV_8U, cv::Scalar(255));    // set highest point at 90% of nbins    int hpt = static_cast
  
   (0.9 * histSize[0]);    // Draw a vertical line for each bin    for( int h = 0; h < histSize[0]; h++ )    {        float binVal = hist.at
   
    (h);        int intensity = static_cast
    
     (binVal * hpt / maxVal);        // This function draws a line between 2 points        cv::line(histImg, cv::Point(h, histSize[0]),        cv::Point(h,histSize[0]-intensity), cv::Scalar::all(0));    }    return histImg;}
    
   
  

Another class can be used to calculate the histogram of a color image. However, this is not very useful. Because the color space of the color image is too large. The histogram is a three-dimensional array. It is not convenient to use. For completeness, the code is listed here.

class ColorHistogram{public:    ColorHistogram()    {        // Prepare arguments for a color histogram        histSize[0] = histSize[1] = histSize[2] = 256;        hranges[0] = 0.0; // BRG range        hranges[1] = 255.0;        ranges[0] = hranges; // all channels have the same range        ranges[1] = hranges;        ranges[2] = hranges;        channels[0] = 0; // the three channels        channels[1] = 1;        channels[2] = 2;    }    cv::MatND getHistogram(const cv::Mat &image) ;    cv::SparseMat getSparseHistogram(const cv::Mat &image) ;private:    int histSize[3];    float hranges[2];    const float* ranges[3];    int channels[3];};

The getHistogram and getSparseHistogram functions are implemented here. The only difference is that getSparseHistogram returns a sparse matrix.

cv::MatND ColorHistogram::getHistogram(const cv::Mat &image){    cv::MatND hist;    // Compute histogram    cv::calcHist(&image,                 1, // histogram of 1 image only                 channels, // the channel used                 cv::Mat(), // no mask is used                 hist, // the resulting histogram                 3, // it is a 3D histogram                 histSize, // number of bins                 ranges // pixel value range                 );    return hist;}cv::SparseMat ColorHistogram::getSparseHistogram(const cv::Mat &image){    cv::SparseMat hist(3,histSize,CV_32F);    // Compute histogram    cv::calcHist(&image,                 1, // histogram of 1 image only                 channels, // the channel used                 cv::Mat(), // no mask is used                 hist, // the resulting histogram                 3, // it is a 3D histogram                 histSize, // number of bins                 ranges // pixel value range                 );    return hist;}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.