image histogram
In statistics, a histogram is a graphical representation of the distribution of data.
The image histogram is a histogram used to represent the brightness distribution in a digital image. Using the image histogram, the brightness distribution of the image can be observed intuitively. In the image histogram, the horizontal axis represents the distribution of brightness values from pure black to pure white regions from left to right.
Abscissa: the gray level of each pixel in the image.
Vertical axis: the number of pixels in the gray level of the pixel.
原图:Understanding Digital Camera Histograms: Tones and Contrast
As shown in the figure above, we can clearly see it from the histogram of this picture. The distribution of pixel brightness in the picture is more distributed at the left and right ends, and less distributed in the middle. Darker pixels have a larger percentage.
Let's take a very simple example to see how the histogram is drawn. Suppose there is a 3x3 picture, and the gray value of each pixel is as follows:
It can be seen that pixels have 5 gray values (x), so we set the abscissa to 5 units; y represents the statistics of the number of pixels of each gray value. According to the definition of the histogram, we draw the 3x3 picture The line histogram is:
Expressed in a histogram as follows:
There is essentially no difference between these two graphs, and they can also be represented by a normalized histogram.
The horizontal axis of the normalized histogram is still the gray level of the pixel, but the vertical axis represents the proportion of the corresponding gray level pixels in the picture.
The corresponding normalized histogram (polyline) is drawn as follows:
Let's look at a practical example:
Judging from these three histograms, the histogram of the top picture is relatively balanced, and the effect looks relatively normal; the brightness distribution of the middle picture is concentrated in the middle area, and the center is relatively dense, the picture does not appear to have high contrast, and the overall grayishness; the bottom picture There are basically no pixels in the darker area of the histogram, and the distribution in the brighter area is less, and the overall distribution is slightly more in the bright area, and the distribution is not uniform. Generally speaking, except for some special scenes, the visual effect of the first balanced histogram distribution is better.
Some terminology for histograms
dims: The number of features that need to be counted. For example, dims = 1 means that only the gray value is counted.
bins: The number of subbins for each feature space.
range: The range of gray values, generally [0,255].
As shown in the figure above, initially bins = 5, we set bins to 3, and change the pixel gray level into 3 ranges (bins), each range contains two gray levels. Count the number of pixels in each grayscale range and draw them.
For bins, the image below is another example:
Histogram equalization
Histogram equalization is to adjust the pixel brightness distribution range to make the pixels more evenly distributed on the [0.255] gray scale, thereby improving the contrast of the image and improving the visual effect of the image. Generally, for images with low contrast, histogram equalization is more suitable and will enhance image details.
The operation of expanding the brightness range of the original image, from a mathematical point of view, we need a mapping function to evenly map the pixel values of the original image to the new histogram. This function must meet two conditions:
1. Do not disturb the order of the original pixel values, and the size relationship between light and dark cannot be changed after mapping.
2. The mapped pixel value is within the original pixel value range (usually [0,255]).
When doing histogram equalization, the cumulative distribution function will be used, because the cumulative distribution function is monotonically increasing and the maximum value is 1, which can guarantee the above two too golden. For the derivation of histogram equalization and cumulative distribution function relationship, you can refer to this article:
cumulative distribution function
The image is composed of discrete pixels, so the image histogram equalization is solved by the cumulative distribution function in discrete form. During the histogram equalization process, the mapping method is:
It refers to the gray value after mapping by cumulative distribution function;
MN is the number of pixels;
Indicates the number of pixels with grayscale j;
L represents the number of gray levels (8 bit pixel value has 256 gray levels)
Histogram equalization process
1. Calculate the statistical histogram of the original image
2. Obtain a normalized histogram according to the statistical histogram, and obtain a cumulative histogram through the normalized histogram
It can be seen that the value corresponding to the pixel level of the cumulative histogram is the probability of the occurrence of the pixel at this pixel level plus the sum of the occurrence probabilities of all previous pixel levels (the value corresponding to pixel level 1 in the figure is 0.37 because the normalized histogram The third decimal point is not displayed in , just understand the process).
3. Use the cumulative histogram for interval conversion
Calculated from the formula for the discrete cumulative distribution function . There are 8 pixel levels in the case, so L - 1 = 7. This is obtained by multiplying the cumulative probability by (L - 1) . Compare the original histogram and the balanced histogram:
Finally, let's look at an example of histogram equalization in action: