# 1, Basic concepts

## 1. Definition of gray histogram

The statistical relationship between the gray levels at all levels in the digital image and their occurrence frequency can be expressed as:

And:

Wherein, K is the k-th gray value of the image, and is the number of pixels with a medium gray value of K;

n is the total number of pixels of the image;

L is the grayscale series.

## 2. Properties of gray histogram

① Positional deletion of histogram;

② One to many characteristics of histogram and image;

③ Superposition of histograms.

## 3. Relationship between histogram and image clarity

The histogram reflects the clarity of the image. When the histogram is evenly distributed, the image is the clearest.

Therefore, we can use histogram to make the image clear.

# 2, Histogram equalization

## 1. Definitions

Through the gray nonlinear transformation of the original image, the histogram becomes evenly distributed to increase the dynamic range of the gray value of the image, so as to enhance the overall contrast of the image and make the image clear.

## 2. Image gray transformation function conditions

① For 0 ≤ r ≤ 1, s=T[r] is a monotonically increasing function;

② For 0 ≤ r ≤ 1, 0 ≤ s=T[r] ≤ 1.

Similarly, the inverse transformation r=T-1[s] should also satisfy monotonic increasing.

## 3. Calculation process of histogram equalization

① List the gray levels of the original image and the transformed image: i,j=0,1,..., L-1, where l is the gray level series;

② Count the number of pixels of each gray level of the original image ni;

③ Calculate the histogram of the original image: P(i)=ni / n, where n is the total number of pixels of the original image;

④ Calculate cumulative histogram:

⑤ Use the gray transformation function to calculate the transformed gray value and round it:

⑥ Determine the gray transformation relationship f(m,n)=i, and modify the gray value of the original image as:

g(m,n)=j；

⑦ Count the number of pixels of each gray level after transformation nj;

⑧ Calculate the histogram of the transformed image: P(j)=nj / n

/*Histogram equalization*/ void Histogram_equalization(Mat& src, Mat& dst) { CV_Assert(src.depth() == CV_8U); //Accept only uchar images src.copyTo(dst); int nr = src.rows; int nc = src.cols; int pixnum = nr * nc; //Gray image if (src.channels() == 1) { //Statistical histogram int gray[256] = { 0 }; for (int i = 1; i < nr; i++) { const uchar* ptr = src.ptr<uchar>(i); for (int j = 0; j < nc; j++) { gray[ptr[j]]++; } } //Calculate distribution function int LUT[256]; int sum = 0; for (int k = 0; k < 256; k++) { sum += gray[k]; LUT[k] = 255 * sum / pixnum; } //Grayscale transformation (assignment) for (int i = 1; i < nr; i++) { const uchar* ptr_src = src.ptr<uchar>(i); uchar* ptr_dst = dst.ptr<uchar>(i); for (int j = 0; j < nc; j++) { ptr_dst[j] = LUT[ptr_src[j]]; } } } //color image else { //Statistical histogram int B[256] = { 0 }; int G[256] = { 0 }; int R[256] = { 0 }; for (int i = 0; i < nr; i++) { for (int j = 0; j < nc; j++) { B[src.at<Vec3b>(i, j)[0]]++; G[src.at<Vec3b>(i, j)[1]]++; R[src.at<Vec3b>(i, j)[2]]++; } } //Calculate distribution function int LUT_B[256], LUT_G[256], LUT_R[256]; int sum_B = 0, sum_G = 0, sum_R = 0; for (int k = 0; k < 256; k++) { sum_B += B[k]; sum_G += G[k]; sum_R += R[k]; LUT_B[k] = 255 * sum_B / pixnum; LUT_G[k] = 255 * sum_G / pixnum; LUT_R[k] = 255 * sum_R / pixnum; } //Gray transformation for (int i = 0; i < nr; i++) { for (int j = 0; j < nc; j++) { dst.at<Vec3b>(i, j)[0] = LUT_B[src.at<Vec3b>(i, j)[0]]; dst.at<Vec3b>(i, j)[1] = LUT_B[src.at<Vec3b>(i, j)[1]]; dst.at<Vec3b>(i, j)[2] = LUT_B[src.at<Vec3b>(i, j)[2]]; } } } }

# 3, Histogram specification

## 1. Definitions

Histogram equalization can automatically enhance the contrast of the whole image, and the result is a global homogenized histogram. However, in practical application, it is sometimes required to highlight the gray range of interest, that is, modify the histogram to make it have the required form.

## 2. Steps of histogram specification:

① Equalize the original histogram, that is, find its cumulative histogram P~i ~:! [insert picture description here]( https://img-blog.csdnimg.cn/035b67d9ee364c04821ba21fc86cdcda.png )② For the specified histogram equalization, the cumulative histogram P~j ~:! [insert picture description here]( https://img-blog.csdnimg.cn/27b245bca4d044a0830c324895523234.png )③ Transform I -- > J according to the closest principle of P~j -- > P~i ~; ④ The transformation function of I -- > J is obtained, and the gray transformation of the original image is carried out, j=T[i]. Where, P~r~(i) is the histogram of the original digital image, P~r~(j) is the specified histogram, I and j are the gray levels of the original image and the desired image respectively, and have the same value range, that is, I, j = 0,1,2,..., L-1.# 4, Adaptive histogram equalization (AHE)

## 1. Definitions

AHE is an image processing technology used to improve the image contrast. Compared with the traditional histogram equalization, the main difference is that AHE redistributes the brightness value of the image by calculating the histogram of each significant area of the image. Therefore, it is more suitable for improving the local contrast of the image and enhancing the image edge information, which is conducive to segmentation.

## 2. Disadvantages

AHE can enhance the noise in the homogeneous (uniform) region of the image while enhancing the contrast. Therefore, as an improvement of AHE, CLAHE can effectively reduce the enhancement of this noise.

## 3.CLAHE

The biggest difference between CLAHE and AHE is that the former limits the contrast. This feature can also be applied to global histogram equalization, that is, contract limited he, referred to as CLHE, but it is rarely used in practice.

In CLAHE, each pixel neighborhood must be subject to contrast restriction, so as to obtain the corresponding transformation function, which is used to reduce the noise enhancement in AHE, which is mainly realized by limiting the contrast enhancement in AHE.