The histogram of an image not only represents the distribution of the intensity value (that is, the pixel value), but also the gradient and Motion Direction of the pixel.
Dims: indicates the number of parameters to be processed, such as the intensity value, gradient value, and direction value. This article only calculates the histogram of the pixel intensity value, so dim = 1.
Bins: number of sub-split boxes in each dim. Bins = 16 in this article.
Range: the measurement range of the pixel value. Range = [0,255] In this article
For details about the implementation process, refer to the Code and some comments. The uncommented parts have been commented out in the previous article. The original image and program running result image are given below.
# Include "opencv2/highgui. HPP "# include" opencv2/imgproc. HPP "# include <iostream> # include <stdio. h> using namespace STD; using namespace CV; int main (INT argc, char ** argv) {mat SRC, DST; // load the color image src = imread (argv [1], 1); If (! SRC. data) {return-1 ;}// split the image into three channels: B, G and R vector <mat> bgr_planes; split (SRC, bgr_planes ); // Number of created boxes int histsize = 256; // set the range (for B, G, R) float range [] = {0,256 }; // do not include upper limit 256 const float * histrange = {range}; // normalization, starting position histogram clearing content bool uniform = true; bool accumulate = false; MAT B _hist, g_hist, r_hist; // calculate the histogram of each plane // & bgr_planes [] original array, 1 original array count, 0 only processes one channel, // MAT () used to process the mask of the original array. B _hist will be used to store the space size of the mat object // 1 histogram of the histogram, and the number of boxes in each dimension of histsize, the variation range of histrange in each dimension // the size of the uniform and accumulate boxes is the same. The position at the beginning of the histogram clears the content calchist (& bgr_planes [0], 1, 0, MAT (), B _hist, 1, & histsize, & histrange, uniform, accumulate); calchist (& bgr_planes [1], 1, 0, MAT (), g_hist, 1, & histsize, & histrange, uniform, accumulate); calchist (& bgr_planes [2], 1, 0, MAT (), r_hist, 1, & histsize, & histrange, uniform, accumulate ); // draw a histogram (B, G and R) int hist_w = 512; int hist_h = 400; int bin_w = cvround (double) hist_w/histsize); MAT histimage (hist_h, hist_w, cv_8uc3, scalar (0, 0, 0); // The normalization result is [0, histimage. rows] // B _hist input array, B _hist output array, // 0 and histimage. limit values at both ends of rows normalization, // norm_minmax normalization type-1 output is the same as the input type, MAT () Optional mask normalize (B _hist, B _hist, 0, histimage. rows, norm_minmax,-1, MAT (); normalize (g_hist, g_hist, 0, histimage. rows, norm_minmax,-1, MAT (); normalize (r_hist, r_hist, 0, histimage. rows, norm_minmax,-1, MAT (); // draw a picture for (INT I = 1; I