This case will introduce histogram calculation and histogram equalization, histogram calculation is very useful, in many cases can be used, not only in the image of the gray value, but also in the image of other features; Image equalization is often used in image preprocessing, it can enhance contrast, Makes the pixel intensity distribution range more widely. 1. Principle
Histogram calculation:
Http://www.opencv.org.cn/opencvdoc/2.3.2/html/doc/tutorials/imgproc/histograms/histogram_calculation/histogram _calculation.html#histogram-calculation
Histogram equalization:
http://www.opencv.org.cn/opencvdoc/2.3.2/html/doc/tutorials/imgproc/histograms/histogram_equalization/ Histogram_equalization.html#histogram-equalization
Learning OpenCV:
http://download.csdn.net/detail/chenjiazhou12/7083295 2, code implementation
Program function:
① input a picture, decomposition of the image of the three channels, three channels for the histogram calculation, and displayed
② decomposition of three channels of the image, three channels of the histogram equalization, and then merge three channels, the final display image and histogram calculation image
#include "stdafx.h" #include "opencv2/highgui/highgui.hpp" #include "opencv2/imgproc/imgproc.hpp" #include <
Iostream> #include <stdio.h> using namespace std;
using namespace CV;
Mat Calchistcontrol (vector<mat> RGB);
void Draw (Mat hist,int histsize,mat histimage,int bin_w,int hist_h,int RGB);
/** @ Function Main */int main (int argc, char** argv) {Mat src, dst,rgb_merge,histimage,histimage1;
Mount image src = imread ("scenery2.jpg", 1);
Imshow ("Source_image", SRC);
if (!src.data) {return-1;}
Split into 3 single-channel images (R, G and B) vector<mat> Rgb_planes;
Split (SRC, rgb_planes);
Histogram calculation histimage=calchistcontrol (rgb_planes);
Three channels histogram equalization vector<mat> rgb_planes1;
Split (SRC,RGB_PLANES1);
Equalizehist (Rgb_planes1[0],rgb_planes1[0]);
Equalizehist (rgb_planes1[1],rgb_planes1[1]);
Equalizehist (rgb_planes1[2],rgb_planes1[2]);
Histogram calculation Histimage1=calchistcontrol (rgb_planes1) of three channels histogram equalization;
Merge three-channel merge (Rgb_planes1,rgb_merge); Show Histogram imshow ("CALchist Demo ", histimage);
Imshow ("Calchist Demo1", histImage1);
Imshow ("Rgb_merge", Rgb_merge);
Waitkey (0);
return 0;
} Mat Calchistcontrol (vector<mat> RGB) {vector<mat> rgb_planes;
Rgb_planes=rgb;
Set bin number int histsize = 255;
Set value range (r,g,b)) float range[] = {0, 255};
Const float* Histrange = {range}; BOOL uniform = true;
bool accumulate = false;
Mat r_hist, G_hist, b_hist;
Calculated histogram: Calchist (&rgb_planes[0], 1, 0, Mat (), r_hist, 1, &histsize, &histrange, uniform, accumulate);
Calchist (&rgb_planes[1], 1, 0, Mat (), g_hist, 1, &histsize, &histrange, uniform, accumulate);
Calchist (&rgb_planes[2], 1, 0, Mat (), b_hist, 1, &histsize, &histrange, uniform, accumulate); Create a histogram canvas int hist_w = 400;
int hist_h = 400;
int bin_w = Cvround ((double) hist_w/histsize);
Mat histimage (Hist_w, Hist_h, CV_8UC3, Scalar (0,0,0)); Normalization of histogram to range [0, Histimage.rows] Normalize (r_hist, r_hist, 0, HistimagE.rows, Norm_minmax,-1, Mat ());
Normalize (g_hist, g_hist, 0, Histimage.rows, Norm_minmax,-1, Mat ());
Normalize (b_hist, b_hist, 0, Histimage.rows, Norm_minmax,-1, Mat ());
Draw a histogram draw (r_hist,histsize,histimage,bin_w,hist_h,1) on the histogram canvas;
Draw (g_hist,histsize,histimage,bin_w,hist_h,2);
Draw (b_hist,histsize,histimage,bin_w,hist_h,3);
return histimage;
} void Draw (Mat hist,int histsize,mat histimage,int bin_w,int hist_h,int RGB) {Scalar A;
Switch (RGB) {case 1:a=scalar (0,0,255);
Case 2:a=scalar (0,255,0);
Case 3:a=scalar (255,0,0);
} int Flag=1,first;
for (int i = 0; i < histsize; i++) {if (flag==1) {if (Cvround (hist.at<float> (i)!=0)) {flag=2;
first=i;
} else {i++; }} else {if (Cvround (hist.at<float> (i)!=0)) {line (Histimage, point (bin_w* (first), Hist_h-cvroun D (hist.at<float> (first)), point (bin_w* (i), Hist_h-cvround (hist.at<float> (i))), A, 2,8, 0);
first=i;
} else {i++; }
}
}
}
3. Operation Result
Figure 1, source image
Figure 2, merging images
Figure 3, the source image straight Fontoutou 4, the combined image histogram 4, the classes and functions used calchist
Function: Calculate the histogram of the image
Structure:
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)
Arrays: Source image set, they all need to have the same depth,cv_8u or cv_32f, the same size, can have any number of channels
Narrays: Number of input images
Channels: The Channel (dim) Index to be counted
Mask: Mask (0 means ignore the pixel), if not defined, the mask is not used, if not null, only the pixels corresponding to the mask non-0 elements will be included in the calculation histogram.
hist: An array of output histograms, which is an array of dims dimensions
Dims: Dimensions of the histogram
Histsize: The size of the histogram, that is, the number of bins per dimension
Ranges: The range of values for each dimension
Uniform, accumulate:bin the same size, clear the histogram traces
Hist.at<float> (i) indicates the number of statistics saved in the I bin equalizehist
Function: Histogram equalization of gray image
Structure:
void Equalizehist (Inputarray src, outputarray DST)
SRC: Source image, 8bit single-channel picture
DST: The target image, and the source image have the same size and type
Operation:
①, calculate src histogram h
②, normalized histogram, so that the bins in H and 255;
③, calculating histogram integrals
④, using h ' as a query table for transformation:
This method normalized the image brightness and enhanced contrast.