Definition of grayscale histogramgrayscale Histogram is a function of gray level, describes the number of pixels in the image (or the frequency of the gray level pixels): The horizontal axis is the gray level, the ordinate represents the number of gray level in the image (frequency).
#include <opencv2/core/core.hpp>#include<opencv2/highgui/highgui.hpp>#include<opencv2/imgproc/imgproc.hpp>#include<iostream>using namespaceCV;using namespacestd;classhistogram1d{//define a class that processes a single channelPrivate: inthistsize[1];//the number of bins floathranges[2];//Value Fan Const float* ranges[1];//A pointer to a range of values. A pointer to a constant (which is not necessarily a constant), and cannot modify its value by a pointer intchannels[1];//Number of channelsMat Gethistogram (ConstMat &image) { //Statistical HistogramMat hist; Calchist (&image,1,//histogram of an imageChannels//the channel usedMat (),//do not use maskshist//histogram as the result 1,//one-dimensional histogramhistsize, ranges); returnhist; } Public: histogram1d () {histsize[0] = the; hranges[0] =0;//from 0 tohranges[1] = the; ranges[0] =hranges; channels[0] =0; } Mat Getimageofhistogram (ConstMat &image,intZoom=1) { //Zoom channel number//Draw a histogramMat hist=Gethistogram (image); returngetImageOfHistogram1 (hist, zoom); } //is defined as static and does not operate on member variables StaticMat getImageOfHistogram1 (ConstMat &hist,intzoom) { //get the maximum and minimum value of the box DoubleMaxval =0, Minval =0; Minmaxloc (hist,&minval, &maxval,0,0); intHistsize =hist.rows; //Histogram for displayMat histimg (Histsize*zoom, Histsize*zoom, cv_8u, Scalar (255)); //set the number of chests with a maximum point of 90% intHPT = static_cast<int> (0.9*histsize); //draw a vertical line for each box for(inth =0; H < histsize; h++) { floatBinval = hist.at<float>(h); if(binval>0) { intintensity = static_cast<int> (binval*hpt/maxval);//Relative HeightLine (histimg, point (H*zoom, histsize*zoom), point (H*zoom, (histsize-intensity) *zoom), Scalar (0), zoom); } } returnhistimg; }};intMain () {Mat image= Imread ("1.jpg"); //determines whether the emptyhistogram1d H; //Mat histo = h.gethistogram (image);Mat Histo =H.getimageofhistogram (image); //Namedwindow ("histogram"); //imshow ("histogram", histo); //output two value imageMat thresholded; Threshold (image, thresholded, $,//threshold Value 255,//assign a value to a pixel that exceeds the domain valueThresh_binary);//types of threshold valuesImshow ("Threshold", thresholded); Waitkey (0); return 0;}
View Code
The above procedure is to calculate and draw a histogram of the single make image, mainly the Calchist function. Similarly, you can define a class that calculates a color histogram.
classcolorhistgoram{Private: inthistsize[3]; floathranges[2]; Const float*ranges[3]; intchannels[3]; Public: Colorhistgoram () {histsize[0] = histsize[1] = histsize[2] = the; hranges[0] =0; hranges[1] = the; ranges[0] =hranges; ranges[1] =hranges; ranges[2] =hranges; channels[0] =0; channels[1] =1; channels[2] =2; } Mat Gethistogram (ConstMat &image) {Mat hist; Calchist (&image,1, channels, Mat (), Hist,3, histsize, ranges); returnhist; }};
View Code
Improved image contrast ratio
There are two methods, one is to apply a lookup table to stretch the histogram (some of the intensity values are not being exploited), and the other is to equalize the histogram (balanced use of all available pixel strength values).
//Lookup Table Functions StaticMat Applylookup (ConstMat &image,ConstMat &lookup) {Mat result; LUT (image, lookup, result); returnresult; } //increase the contrast of an image by looking up a tableMat Stretch (ConstMat &image,intMinValue =0) {Mat hist=Gethistogram (image); //find the left and right limits of the histogram floatImin; for(imin=0; Imin < histsize[0]; imin++) { //Ignore fewer boxes floatx = hist.at<float>(Imin); if(x>minValue) Break; } intIMAX = histsize[0] -1; for(; Imax >=0; imax--) { if(hist.at<float> (IMAX) >minValue) Break; } //Create a lookup table intDim the); Mat Lookup (1,//One-dimensional&Dim, cv_8u); for(inti =0; I < the; i++) { // if(I < imin) lookup.at<uchar> (i) =0; Else if(I>imax) lookup.at<uchar> (i) =255; Elselookup.at<uchar> (i) = Cvround (255.0* (i-imin)/(IMAX-imin)); } Mat result; Result=applylookup (image, lookup); returnresult; }
View Code
histogram equalization is achieved through the following functions.
// Grayscale Image Equalizehist (image, result);
OPENCV Cookbook reading Notes (iv): using histograms to count pixels