The main use of the function in the Calchist () function to statistics a histogram, Histogram statistics class header file and source file, the header file code is as follows:
#ifndef histogram1d_h#define histogram1d_h#include <opencv2/core/core.hpp> #include <opencv2/highgui/ Highgui.hpp> #include <opencv2/opencv.hpp>using namespace cv;using namespace Std;class histogram1d{public: histogram1d (); Matnd Gethistogram (const Mat &source); Mat Gethistogramimage (const mat &source);p rivate: int histsize[1]; Histogram number of items int channels[1]; Number of channels float histminmax[2]; Pixel minimum maximum const float* RANGES[1];//pixel value range}; #endif//Histogram1d_h
The source file code is as follows:
#include "histogram1d.h" histogram1d::histogram1d () {//1d parameter initialization histsize[0] = 256; Histminmax[0] = 0.0; HISTMINMAX[1] = 255.0; Ranges[0] = Histminmax; Channels[0] = 0;} Matnd Histogram1d::gethistogram (const Mat &source) {matnd Hist; Calchist (&source, 1, channels, Mat (), Hist, 1, histsize, ranges); return Hist;} Mat Histogram1d::gethistogramimage (const mat &source) {Matnd hist = Gethistogram (source);//calculation histogram double maxval = 0; Double minval = 0; Minmaxloc (hist, &minval, &maxval);//Gets the maximum and minimum values Mat histimg (histsize[0], histsize[0], cv_8u, Scalar (255));// Used to display the histogram image int toppoint = static_cast<int> (0.9*histsize[0]); for (int i = 0; i < histsize[0]; i + +) {Float Binval = hist.at<float> (i); int intensity = static_cast<int> (binval*toppoint/maxval); Line (histimg, point (I, histsize[0]), point (I, histsize[0]-intensity), Scalar: : All (0)); } RETurn histimg;}
The Main.cpp code snippet is as follows:
#include <QCoreApplication> #include "histogram1d.h" using namespace Cv;using namespace Std;int main () { Mat src = imread ("Lena.jpg", 0); HISTOGRAM1D Hg; Mat histimg = hg.gethistogramimage (src); Namedwindow ("Histimg", 0); Imshow ("Histimg", histimg); Waitkey (0); return 0;}
Function Description:
void Calchist (const mat* Arrays, //input source image int narrays,// calculation histogram number const int* channels, // Number of channels Inputarray mask, //Mask Outputarray hist, //histogram returned int dims, //number of dimensions const int* histsize, // Number of histogram items const float** ranges, //pixel value range bool Uniform = True,//bool accumulate = false);//
2, Matnd type is a general class, you can manipulate the n-dimensional matrix. He also defines an at method of one-dimensional, two-dimensional and three-dimensional matrices.
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Qt 5.3 under OpenCV 2.4.11 Development (7) single-channel histogram rendering