First, histogram equalization
Consider four types of images.
In a dark image, the composition of the histogram is concentrated on the low side of the gray level.
The histogram of the bright image tends to the side of the high gray level.
The histogram of the low contrast image is narrow and concentrated in the middle of the gray level.
The composition of the histogram of high contrast images covers a wide range of gray levels.
When we encounter the first three kinds of images, we may need histogram equalization to make the pixels of an image occupy all possible grayscale levels and distribute evenly, such images have high contrast and changeable shades of gray.
In OpenCV, we use equalizehist to perform histogram equalization for grayscale images:
Equalizehist (Inputarray src, outputarray DST)
SRC is typically a 8-bit single-channel image.
DST and SRC have the same size and type.
Second, if an image is a three-channel, we want to do a histogram of each channel statistics, then how to do it?
Analyzing an instance code for a OpenCV we know:
Use the Split function to separate the BGR three channels
Vector<mat> Bgr_planes;
Split (SRC, bgr_planes); SRC is the source image we read into Mat before
Using the Calchist function for histogram statistics, the key to using this function is the setting of its parameters. Its parameter settings are more complex.
Establish the number of bins
int histsize = 256;
Set the ranges (for B,g,r))
Float range[] = {0, 256};
Const float* Histrange = {range};
BOOL uniform = true;
bool accumulate = false;
Mat b_hist, G_hist, r_hist; These three parameters are used to record the histogram of the BGR, respectively.
Compute the histograms:
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);
The functions of calchist are described as follows:
void Calchist (const mat* images, int nimages, const int* channels, Inputarray mask, Outputarray hist, int dims, const int* Histsize, const float** ranges, BOOL uniform=true, BOOL Acculate=false)
Against the example of the upper calchist function, we can see
Image is an input to the histogram of statistics, type is a pointer, let us give the array or vector, support multiple image processing. We just have to deal with one here, so give the address directly. If there are multiple images entered, they should be of the same size and depth.
Nimages indicates the number of inputs.
Channels should also be an array, we just deal with a single channel of input, should be given int channels[] = {0}, here directly to 0 also line, do not know why.
Mask is a mask, if mask is not empty, then mask needs to be in the same size as the input, the mask is 1 of the position of the histogram. Here we give Mat ().
hist is the output.
Dims is the dimension of the histogram. This is 1. is a one-dimensional histogram. There are three-dimensional histograms that return three-dimensional cv::mat instances.
Histsize is the size of the histogram, here is 256.
Histrange is the range of histograms for each dimension.
Third, the histogram normalization:
Next Example:
Normalize the result to [0, Histimage.rows]
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 ());
The first parameter of the normalize is the input, the second parameter is the output, and in the case of the fifth parameter is Norm_minmax, the third and fourth parameters are the lower and upper bounds, respectively. The sixth parameter is negative, indicating that the input and output are of the same type. The seventh parameter is a mask.
Four, draw the histogram:
Next Example:
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));
Draw for each channel
for (int i = 1; i < histsize; i++)
{
Line (Histimage, point (bin_w* (i-1), Hist_h-cvround (b_hist.at<float> (i-1))),
Point (bin_w* (i), Hist_h-cvround (b_hist.at<float> (i))),
Scalar (255, 0, 0), 2, 8, 0);
Line (Histimage, point (bin_w* (i-1), Hist_h-cvround (g_hist.at<float> (i-1))),
Point (bin_w* (i), Hist_h-cvround (g_hist.at<float> (i))),
Scalar (0, 255, 0), 2, 8, 0);
Line (Histimage, point (bin_w* (i-1), Hist_h-cvround (r_hist.at<float> (i-1))),
Point (bin_w* (i), Hist_h-cvround (r_hist.at<float> (i))),
Scalar (0, 0, 255), 2, 8, 0);
}
V. Comparison of Histograms
With a few histograms, we can compare the differences between the histograms.
To compare the histogram, we first set a comparison of the scale or method, generally there are four kinds:
-
Correlation (Cv_comp_correl) range from 0 to 1. 1 is completely relevant, 0 is completely irrelevant.
where
and is the total number of histogram bins.
-
Chi-Square (CV_COMP_CHISQR) range from 0 to positive infinity. 0 is exactly the same, the larger the number, the larger the histogram difference. (large change in digital amplitude)
-
Intersection (method=cv_comp_intersect) range from 0 to positive infinity, the larger the number, the more similar the representation, because the more the histogram intersection. The smaller the number, the greater the difference. (Small change in digital amplitude)
-
Bhattacharyya distance (cv_comp_bhattacharyya) ranges from 0 to 1. 0 means exactly the same. 1 means completely different.
Generally before comparing the histogram, the histogram should be normalized, using the normalize function of the previous step, the upper limit is changed to 1.
In the case of OpenCV, three pictures were taken, respectively, in different angles and light, on one hand. Then compare these photos in the HSV format of the h-s two-dimensional histogram, ignoring v.
The way to compare histograms is comparehist, which is how it's used:
Double Comparehist (Inputarray H1, Inputarray H2, int method)
H1 and H2 are two histograms to compare
Method is a comparison of methods, from 0 to 3 respectively is Cv_comp_correl, CV_COMP_CHESQR, Cv_comp_intersect, Cv_comp_bhattacharyya.
Vi. inverse projection histogram back Projection
Inverse projection Histogram Simply put, we first have a histogram of the sample, and then we get a picture, and we calculate the probability that each pixel in this image belongs to this histogram. It's called anti-projection.
void Calcbackproject (const mat* images, int nimages, const int* channels, const sparsemat& hist, Outputarray Backproje CT, const float** ranges, double scale=1, bool uniform=true)
Image to be projected, can be multiple, but have the same depth
Nimages Number of images
Channels the channel used for projection
hist Histogram (This is more important)
Backproject output of images
Ranges range of Histograms
OpenCV notes (21)--several operations on the histogram