Some header files:
#include <opencv2/core/core.hpp>#include <opencv2/highgui/highgui.hpp>#include <opencv2/imgproc/imgproc.hpp>#include <iostream>using namespace std;using namespace cv;
First, create a class:
Class histogram1d {PRIVATE: // number of points in the histogram int histsize [1]; // the histogram range is float hranges [2]; // pointer to this range const float * ranges [1]; // channel int channels [1]; public: // constructor histogram1d () {histsize [0] = 256; hranges [0] = 0.0; hranges [1] = 255.0; ranges [0] = hranges; channels [0] = 0 ;}
In opencv, use calchist to calculate the Histogram
Mat gethistogram (const mat & image) {mat hist; // calculate the histogram function // The parameter is: source image (sequence) address, number of input images, number of channels, mask, output result, histogram dimension, size of each dimension, value range of each dimension: calchist (& image, 1, channels, MAT (), Hist, 1, histsize, ranges ); return hist ;}
However, the returned result is not the desired "histogram". hist only records the number of pixels in each bin. We need to use the line function to draw pictures by ourselves:
Mat gethistogramimage (const mat & image) {// calculate the histogram mat hist = gethistogram (image); // obtain the maximum and minimum values of double maxval = 0; double minval = 0; // minmaxloc is used to obtain the maximum and minimum values. The following two parameters are the minimum and maximum values. 0 indicates that minmaxloc (Hist, & minval, & maxval, 0, 0) is not required ); // display the canvas of the histogram. The background color is white mat histimg (histsize [0], histsize [0], cv_8u, scalar (255 )); // set the highest point to 90% int hpt = static_cast <int> (0.9 * histsize [0]) of the total bin count; // draw a line for each Bin (INT h = 0; H
The function compresses the brightness to better display the histogram on the canvas.
A look-up table is a one-to-one or multiple-to-one function that specifies a brightness that is converted into another pixel after the look-up table. Using the LUT function in opencv
Mat applyLookUp(const Mat& image,const Mat& lookup){Mat result;LUT(image,lookup,result);return result;}
The specific content of the change depends on the search table defined by you. For example, if you want the search table to result in the flip of the current image, you can define the search table in the main function:
// Use the lookup table to flip the image. The gray scale int dim (256); mat lut (1, & dim, cv_8u); For (INT I = 0; I <256; I ++) {LUT. at <uchar> (I) = 255-i;} imshow ("grayscale flipped image", H. applylookup (image, LUT ));
When you want to make the histogram stretch after the image goes through the search table, the effect will be better:
Remove the bin with a number lower than the specified number (0 by default). The remaining minimum value is 0, the maximum value is 255, and the linear stretching of the middle part is as follows:
Mat strech (const mat & image, int minvalue = 0) {// calculate the histogram mat hist = gethistogram (image); // int Imin = 0; (; imin
Of course, this effect is not the best. The best balancing should make the histogram very gentle, and the number of each bin is similar. In opencv, you can use the equalizehist function to implement the histogram balancing function:
Mat equalize(const Mat &image){Mat result;equalizeHist(image,result);return result;}
With the class defined above, the main function becomes much simpler:
# Include "histogram. H "int main () {// read the image in grayscale mode mat image = imread (" D:/picture/images/group.jpg ", 0 ); imshow ("Source grayscale image", image); If (! Image. data) Return-1; histogram1d h; imshow ("histogram", H. gethistogramimage (image); // use the lookup table to flip the gray scale int dim (256); mat lut (1, & dim, cv_8u); For (INT I = 0; I <256; I ++) {LUT. at <uchar> (I) = 255-i;} imshow ("grayscale flipped image", H. applylookup (image, LUT); // use the lookup table to stretch the grayscale value of the image. // ignore the binmat strech = H that contains less than 100 pixels. strech (image, 100); imshow ("stretched image", strech); imshow ("stretched histogram", H. gethistogramimage (strech); // image balancing mat afterequalize = H. equalize (image); imshow ("solution after balancing", afterequalize); imshow ("histogram after balancing", H. gethistogramimage (afterequalize); waitkey (0); Return 0 ;}
Because there are many images drawn, no results will be posted here.