OpenCV Learning (histogram of computed images)

Source: Internet
Author: User
Tags ranges scalar

OpenCV calculating the histogram of an image

The histogram of the computed image is a very common basic operation in the field of image processing. The Calchist function is provided in OpenCV to calculate the histogram of the image. But this function is very difficult to tell the truth, studied for a long time to master some basic usage.

The function prototypes for the Calchist function C + + are 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 each parameter are as follows:

    • Images: The pointer to the input image 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: The number of images entered.

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

    • Mask: Mask, if mask is not empty, then it must be a 8-bit (cv_8u) array, and its size is the same size as the input image, and the value not 0 will be used to calculate the histogram.

    • hist: Output parameters, calculated histogram.

    • Dims: The dimensions of the histogram cannot be greater than cv_max_dims (32 in the existing version).

    • Histsize: The number of elements in the histogram on each dimension.

    • Ranges: The range of each dimension needed to calculate the histogram. Ranges is an array of pointers to arrays. The elements that we call the array that it points to are ranges, and the element size is the length of those arrays.

If the parameter uniform is true, this time the size of the element in the ranges is 2 (that is, ranges points to a series of 2-length arrays), storing two digits of the upper and lower bounds of each dimension, or ranges element at this time if the argument uniform to false Size is the number of bin, stored in a color value, that is, the coordinate values of each dimension is not necessarily uniform, need to be specified.

    • Uniform: If true, it indicates that each dimension of the histogram you want to calculate is evenly sized according to its range and size, and if false, the explanation of the reference parameter ranges is that each dimension of the histogram is not evenly distributed.

    • Accumulate: Indicates whether to clear zero for incoming Hist. If you do not clear 0, you can add a histogram of multiple images.

Look at the above explanation you should be able to feel how troublesome this function is. But fortunately we generally only use some of the most basic functions. For example, the histogram of a single-channel grayscale image is calculated. The value range of the histogram will generally be 0 to 255. We can then encapsulate this function again to make it better.

The following code comes from "OpenCV 2 computer Vision application Programming Cookbook". The author of the book wrote 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 histograms. 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 a histogram image:

 //computes the 1D histogram and returns an image of It.cv::mat histogram1d::gethistogramimage (const Cv::mat &A    Mp;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<int> (0.9 * histsize[0]); Draw a vertical line for each bin for (int h = 0; h < histsize[0]; h++) {Float Binval = HIST.AT<FL        Oat> (h);        int intensity = static_cast<int> (Binval * hpt/maxval); This function draws a line between 2 points cv::line (HISTIMG, CV::P oint (H, histsize[0]), CV::P oint (h,hist    size[0]-intensity), Cv::scalar::all (0)); } return histimg;}  

There is another class that can calculate the histogram of a color image. But the use is not very big. Because the color space of the color image is too large. And the resulting histogram is a three-dimensional array. It's not easy to use. In order to be complete, the code is still 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];};

Two functions Gethistogram and Getsparsehistogram 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 ran    GE); 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 ran GE);    Return hist;} 

OpenCV Learning (histogram of computed images)

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.