This article lazy typesetting, directly in the HTML editor editor
In the image processing, we often need to find out the histogram of the image, gray average, gray variance, here is a opencv2+ to bring a program to achieve these functions.
Histogram
For histograms, use the Cv::calchist function to find out.
Prototype
void Calchist (const mat* arrays, int narrays, const int* channels, Inputarray mask, Outputarray
hist, int dims, const int* histsize, const float** ranges, BOOL uniform=true, BOOL accumulate=
false); void Calchist (const mat* arrays, int narrays, const int* channels, Inputarray mask, sparsemat&
hist, int dims, const int* histsize, const float** ranges, BOOL uniform=true, BOOL accumulate=
FALSE);
- Arrays: Input image (or array, array set)
- Narrays: Number of input arrays
- Channels: The channel to be counted
- Mask: Mask (0 means ignore this pixel), if undefined, no mask is used
- hist: Matrix for storing histograms
- Dims: Dimension of histogram
- histsize: Number of bins per dimension
- Ranges: the range of values for each dimension
- uniform and accumulate: Bin size Same, clear histogram traces
Here's a way to calculate the histogram I've encapsulated.
1 voidGetgrayhist (cv::mat& src, cv::mat& grayhist,intHistsize=255)2 {3 floatRange[] = {0,255 };4 Const float* Histrange ={range};5 BOOLUniform =true;BOOLaccumulate =false;6(&SRC,1,0, Cv::mat (), Grayhist,1, &histsize, &histrange, uniform, accumulate);7}
Mean and variance
Calculation of mean and variance using Cv::meanstddev function
The function has four input parameters
1Mat Images
2Mat average, calculated to become Float64 type (double) 1*1.
3Mat Variance, the calculation will also become Float64 type 1*1.
4 Masking film
Here is a program that calculates the average grayscale and gray variance of an image:
voidGetgrayavgstddev (cv::mat& src,Double& AVG,Double&StdDev) {Cv::mat img; if(Src.channels () = =3) cv::cvtcolor (SRC, img, cv_bgr2gray); Elseimg=src; Cv::mean (SRC); Cv::mat mean; Cv::mat StdDev; Cv::meanstddev (IMG, mean, StdDev); Avg= mean.ptr<Double> (0)[0]; StdDev= stddev.ptr<Double> (0)[0];}
opencv2+ Introduction Series (iv): Calculate the histogram of the image, average grayscale, gray variance