Chapter 13: Histogram Processing

  • Histogram is an important analysis tool in image processing.
  • The histogram expresses the image from the perspective of the internal gray level of the image, and contains rich and important information.
  • Processing the image from the perspective of histogram can achieve the purpose of enhancing the display effect of the image.

1. The meaning of the histogram:

1. Ordinary histogram:

From a statistical point of view, the histogram counts the number of occurrences of each gray level in the image . Observed from the graph of the histogram, each 横坐标pixel in the image has the gray level (pixel value) .灰度级纵坐标像素个数

For example, there is an image. There are only 9 pixels in this picture, and there are 1, 2, 3, 4, 5, a total of 5 gray levels.

image-20211128143048534

Count the number of occurrences of each gray level:

image-20211128143123561

When drawing a histogram, the gray level is treated as the x-axis, and the number of occurrences of the gray level is treated as the y-axis, then it can be known that:

  • The data on the x-axis is x=[1 2 3 4 5].
  • The data on the y-axis is y=[3 1 2 1 2].

According to the above relationship, the line chart (left picture) and histogram (right picture) as shown in the figure can be drawn. In general, we refer to the straight line graph on the left and the histogram on the right as histograms.

image-20211128143221547

In actual processing, the x-axis interval of the image histogram is generally [0,255], which corresponds to 256 gray levels of an 8-bit bitmap; the y-axis corresponds to the number of pixels with corresponding gray levels.

2. Normalized histogram:

In the normalized histogram, the x-axis still represents gray levels; the y-axis no longer represents the number of occurrences of gray levels, but the frequency of occurrences of gray levels.

  • The frequency of grayscale occurrences = the number of grayscale occurrences/total number of pixels

For example, count the frequency of occurrence of each gray level:

image-20211128143803539

In the normalized histogram, the sum of the frequency of each gray level is 1. For example, in this case:

image-20211128143830213

When drawing a histogram, treat the gray level as the x-axis data and the frequency of its occurrence as the y-axis data, then we can know:

  • The data of the x-axis is x=[1 2 3 4 5]
  • The data of the y-axis is y=[3/9 1/9 2/9 1/9 2/9]

image-20211128143940422

Note: On the official website of OpenCV, there are three concepts that should be paid special attention to: DIMS, BINS, and RANGE.

  • DIMS: Indicates the number of parameters collected when drawing the histogram. Generally, there is only one kind of data collected in the histogram, which is the gray level. Therefore, the value is 1.
  • RANGE: Indicates the grayscale range to be counted, generally [0,255]. 0 corresponds to black and 255 corresponds to white.
  • BINS: The number of parameter subsets, that is, the number of groups of parameters. In the process of data processing, it is sometimes necessary to divide a large number of data into several groups for analysis.

For example, in a grayscale image, the 256 grayscale levels in the [0,255] interval are divided into subsets according to each group of 16 pixels:

[0,255]=[0,15]∪[16,31]∪…∪[240,255]

According to the above method, the entire gray scale range can be divided into 16 subsets, specifically:

The entire gray scale range = bin1 ∪ bin2 ∪…∪ bin16

After the subsets are divided, the histogram generated by a grayscale image is shown in the figure

image-20211128145033626

Discuss the value of BINS:

  • In the original image, if there are 5 gray levels in total, its BINS value is 5. After subsetting a group with 2 gray levels, 3 subsets are obtained with a BINS value of 3.
  • For the grayscale image, the grayscale range is [0,255], there are 256 grayscales in total, and its BINS value is 256; after subsetting with 16 grayscales as a group, its BINS value is 16.

2. Draw a histogram:

There are two ways to draw a histogram:

  1. The hist() function in Python's module matplotlib.pyplot can conveniently draw histograms, and we usually use this function to draw histograms directly.
  2. The cv2.calcHist() function in OpenCV can calculate the statistical histogram, and can also draw the histogram of the image on this basis.

1. Use Numpy to draw a histogram:

The Python module matplotlib.pyplot provides a framework similar to the MATLAB drawing method, and the matplotlib.pyplot.hist() function (hereinafter referred to as the hist() function) can be used to draw a histogram.

What this function does is draw a histogram based on 数据源and 灰度级grouped. Its basic syntax format is:

  • matplotlib.pyplot.hist(X,BINS)
    • X: Data source, must be one-dimensional. Images are usually two-dimensional, and you need to use the ravel() function to process the image into a one-dimensional data source before using it as a parameter.
    • BINS: The specific value of BINS, indicating the grouping of gray levels.

Example:

import cv2
import matplotlib.pyplot as plt

img = cv2.imread('../boat.512.tiff', 0)
# 将二维数组转换为一维的
data = img.ravel()
print(data)
plt.hist(data, 256)
plt.show()
cv2.imshow('img', img)

cv2.waitKey()
cv2.destroyAllWindows()

# 输出结果
[127 123 125 ... 102  95  97]

image-20211128152724481

2. Use OpenCV to draw a histogram:

In OpenCV, the function cv2.calcHist() is provided to calculate the statistical histogram of the image. This function can count the number of pixels in each gray level in the image. Then, using the plot() function in the matplotlib.pyplot module, the statistical results of the function cv2.calcHist() can be drawn into a histogram.

  1. Statistical image histogram information with cv2.calcHist() function
  • hist=cv2.calcHist(images,channels,mask,histSize,ranges,accumulate)
    • hist: The returned statistical histogram is a one-dimensional array, and the elements in the array are the number of pixels in each gray level.
    • images: Original images, which need to be enclosed in "[]".
    • channels: Specifies the channel number. The channel number needs to be enclosed by "[]". If the input image is a single-channel grayscale image, the value of this parameter is [0]. For color images, its values ​​can be [0], [1], [2], corresponding to channels B, G, R, respectively.
    • mask: mask image. When counting the histogram of the entire image, set this value to None. When counting the histogram of a certain part of the image, a mask image is needed.
    • histSize: The value of BINS, which needs to be enclosed in "[]". For example, the value of BINS is 256, you need to use "[256]" as the parameter value.
    • ranges: the range of pixel values. For example, an 8-bit grayscale image has a pixel value range of [0,255].
    • accumulate: cumulative (cumulative, overlay) flag, the default value is False. If it is set to True, the histogram will not be cleared at the beginning of the calculation, and the calculation is the cumulative result of multiple histograms, which is used to calculate the histogram for a group of images. This parameter allows computing a single histogram from multiple objects, or updating the histogram in real time. This parameter is optional and generally does not need to be set.
import cv2

img = cv2.imread('../boat.512.tiff', 0)
hist = cv2.calcHist([img], [0], None, [256],[0, 255])
print(hist)
print(type(hist))
print(hist.shape)
print(hist.size)

# 输出结果
[[7.000e+00]
 [7.000e+00]
 ......
 [0.000e+00]
 [0.000e+00]]
<class 'numpy.ndarray'>
(256, 1)
256
  1. Use the plot() function to draw a statistical histogram of an image:

Use the plot() function in the matplotlib.pyplot module to draw the return value of the function cv2.calcHist() as an image histogram

import cv2
import matplotlib.pyplot as plt

img = cv2.imread('../boat.512.tiff', 0)
hist = cv2.calcHist([img], [0], None, [256],[0, 255])

plt.plot(hist,color='b')
plt.show()
cv2.imshow('img', img)
cv2.waitKey()
cv2.destroyAllWindows()

image-20211128154551013

Supplement: Learn other uses of the plot() function:

Example 1: Draw the given x=[0,1,2,3,4,5,6], y=[0.3,0.4,2,5,3,4.5,4] using the plot() function .

import matplotlib.pyplot as plt

x = [0, 1, 2, 3, 4, 5, 6]
y = [0.3, 0.4, 2, 5, 3, 4.5, 4]
plt.plot(x, y)
plt.show()

image-20211128154859566

In the figure, the x-axis is specified by x=[0,1,2,3,4,5,6], and the y-axis is specified by y=[0.3,0.4,2,5,3,4.5,4].

Example 2: Given y=[0.3,0.4,2,5,3,4.5,4], use the plot() function to draw it, and observe the drawing result.

import matplotlib.pyplot as plt

y = [0.3, 0.4, 2, 5, 3, 4.5, 4]
plt.plot(y)
plt.show()

image-20211128155053428

It can be seen from the figure that when using the plot() function, if only one parameter is specified, the value corresponding to the x-axis defaults to a natural number sequence x=[0,1,… ,n−1,n]. The natural sequence x has the same length as y.

Example 3: Use the plot() function to combine two sets of different values ​​a=[0.3,0.4,2,5,3,4.5,4], b=[3,5,1,2,1,5,3] with Draw it in different colors.

import matplotlib.pyplot as plt

a = [0.3, 0.4, 2, 5, 3, 4.5, 4]
b = [3, 5, 1, 2, 1, 5, 3]
plt.plot(a, color='b')
plt.plot(b, color='g')

plt.show()

image-20211128155423973

Example 4: Use the function plot() and the function cv2.calcHist() to draw the histogram of each channel of the color image in a window.

import cv2
import matplotlib.pyplot as plt

img = cv2.imread('../lena512color.tiff')
hist_b = cv2.calcHist([img], [0], None, [256], [0, 255])
hist_g = cv2.calcHist([img], [1], None, [256], [0, 255])
hist_r = cv2.calcHist([img], [2], None, [256], [0, 255])

plt.plot(hist_b, 'b')
plt.plot(hist_g, 'g')
plt.plot(hist_r, 'r')
plt.show()

cv2.imshow('img', img)
cv2.waitKey()
cv2.destroyAllWindows()

image-20211128155931662

3. Plot the histogram using the mask:

import cv2
import matplotlib.pyplot as plt
import numpy as np

img = cv2.imread('../lena512color.tiff')

mask = np.zeros(img.shape[:2], np.uint8)
mask[200: 400, 200: 400] = 255

hist_b = cv2.calcHist([img], [0], mask, [256], [0, 255])
hist_g = cv2.calcHist([img], [1], mask, [256], [0, 255])
hist_r = cv2.calcHist([img], [2], mask, [256], [0, 255])

plt.plot(hist_b, 'b')
plt.plot(hist_g, 'g')
plt.plot(hist_r, 'r')
plt.show()

rst = cv2.bitwise_and(img, img, mask=mask)
cv2.imshow('img', img)
cv2.imshow('rst', rst)
cv2.waitKey()
cv2.destroyAllWindows()

image-20211128160911464

3. Histogram equalization:

Why Histogram Equalization?

If an image has all possible gray levels, and the gray levels of pixel values ​​are uniformly distributed, then the image has high contrast and variable gray tones, rich gray levels and large coverage. In appearance, such images have richer colors and are not overly dark or bright.

image-20220123044409086

As shown in the above figure, the comparison before and after histogram equalization

​ On the official website of OpenCV, the histograms before and after image equalization are compared, as shown in the figure. Among them, the left image is the histogram of the original image. It can be seen that the gray level is concentrated in the middle, and there are no darker and brighter pixels in the image; the right image is the histogram after equalizing the original image, and the pixel distribution is more balanced. .

image-20211129151054445

What is Histogram Equalization?

The main purpose of histogram equalization is to evenly map the gray level of the original image to the entire gray level range , and obtain an image with uniform gray level distribution. This equalization not only realizes the probability balance of the gray value statistics, but also realizes the visual balance of the Human Visual System (HVS).

For example: only 6 pixel values ​​such as 1, 2, 3, 101, 102, and 103 appear in a certain image, and their distributions are respectively shown in case A and case B in the table.

image-20211129152156291

The situation of pixel value balance in these two cases will be discussed separately below.

  • Case A: The number of times each gray level appears in the image is 1, and the gray level is evenly mapped to the current gray level range, so it can be understood that its histogram is balanced.
  • Case B: the number of occurrences of gray levels 1, 2, and 3 is 1, the number of occurrences of gray level 103 is 3 times, and the number of occurrences of gray levels 101 and 102 is 0. On the surface, the gray scale is uneven. However, from the perspective of HVS, the sensitivity of the human eye is not enough to distinguish the difference of 1 pixel value, that is, the human eye will regard gray levels 1, 2 and 3 as the same, and will regard gray levels 101, 102 and 103 are considered the same. That is to say, HVS will automatically divide the gray levels into two groups, the gray level [1,3] is one group, and the gray level [101,103] is another group. In the whole image, the gray levels of these two groups appear three times, and the probability is equal. In the equalization process, the balance of statistical probability and HVS is considered comprehensively.

1. The principle of histogram equalization:

The algorithm of histogram equalization mainly includes two steps:

(1) Calculate the cumulative histogram.

(2) Perform interval conversion on the cumulative histogram

On this basis, the human vision is used to achieve the purpose of histogram equalization.
Below we explain through an example. For example, image A, which is a 3-bit bitmap, has 8 (23) gray levels and 49 pixels.

image-20211129153308684

Image A has a total of 8 gray levels, the range is [0,7], and its statistical histogram is calculated as shown in the table:

image-20211129153353930

On this basis, the normalized statistical histogram is calculated by calculating the probability of each pixel appearing in the image. Probability of occurrence = number of occurrences/total number of pixels, and divide the number of pixels of each gray level by the total number of pixels (49) to obtain a normalized statistical histogram.

image-20211129153431441

Next, calculate the cumulative statistical histogram, that is, calculate the cumulative probability of all gray levels.

image-20211129153503945

On the basis of the cumulative histogram, the original gray scale space is converted. The gray level can be equalized within the original range , and the gray level can also be equalized within a wider range of gray space . The two forms are described below.

(1) Achieve equalization within the original range:

When the histogram equalization is realized in the original range, the cumulative probability of the current gray level is multiplied by the maximum value 7 of the current gray level to obtain a new gray level, which is used as the result of equalization.

image-20211129154518042

  • The gray level 0 in the original image A is adjusted to a new gray level 1 (that is, the equalization value 1) after histogram equalization. In the original image A, there are 9 pixels in gray level 0, so in the equalized image, there are 9 pixels in gray level 1.
  • The gray levels 1 and 2 in the original image A are adjusted to gray level 3 after histogram equalization. In the original image A, there are 9 pixels in gray level 1 and 6 pixels in gray level 2, so in the equalized image, there are 9+6=15 pixels in gray level 3.
  • The gray level 3 in the original image A is adjusted to gray level 4 after histogram equalization. In the original image A, there are 5 pixels in gray level 3, so in the equalized image, there are 5 pixels in gray level 4.
  • Gray levels 4 and 5 in the original image A are adjusted to gray level 5 after histogram equalization. In the original image A, there are 6 pixels in gray level 4 and 3 pixels in gray level 5, so in the equalized image, there are 6+3=9 pixels in gray level 5.
  • The gray level 6 in the original image A is adjusted to gray level 6 after histogram equalization. In the original image A, there are 3 pixels in the gray level 6, so in the equalized image, there are 3 pixels in the gray level 6.
  • The gray level 7 in the original image A is adjusted to gray level 7 after histogram equalization. In the original image A, there are 8 pixels in the gray level 7, so in the equalized image, there are 8 pixels in the gray level 7.

image-20211129155005569

After equalization processing, the distribution of gray levels in the entire gray space will be more balanced.

Comparison chart before and after histogram equalization. Among them, the left picture is the histogram before equalization, and the right picture is the histogram after equalization.

image-20211129155109228

The equalization here is the result of comprehensive consideration of statistical probability and HVS.

  • In image A, before histogram equalization: the number of pixels between gray levels 0 to 3 is 29, and the number of pixels between gray levels 4 to 7 is 20.
  • After histogram equalization of image A: the number of pixels between gray levels 0 to 3 is 24, and the number of pixels between gray levels 4 to 7 is 25.

Through the above comparison, it can be seen that the gray level distribution of the image is more balanced after the histogram equalization.

(2) Equalization on a wider scale:

When implementing histogram equalization in a wider range, the cumulative probability of the current gray level is multiplied by the maximum value of the wider range of gray levels to obtain a new gray level as the result of equalization.

For example, to expand the gray level space to [0,255] with a total of 256 gray levels, the cumulative probability of the original gray level must be multiplied by 255 to obtain a new gray level. As shown in the table is the new gray level of image A in the new gray level space [0,255].

image-20211129155350672

After equalization processing, the gray level of image A remains balanced in the new gray space [0,255].

image-20211129155418320

From the above analysis, it can be seen that the histogram can be equalized through the following two steps.

  • Computes the cumulative histogram.

  • Interval transform the cumulative histogram.

Below we use mathematical expressions to describe the above histogram equalization process. Assuming that the total number of pixels in the image is N, the number of gray levels of the image is L, and the gray level space is [0,L-1], use nk n_knkIndicates the number of pixels in the image of the kth gray level (the kth gray level, the gray value is k), then the gray level in the image is rk r_krkThe probability of occurrence of (the kth gray level) is:

image-20211129155731270

According to the gray level probability, the formula for equalizing it is:

image-20211129155750774

In the formula image-20211129155922818, represents the cumulative probability, multiplying this value by the maximum value L-1 of the gray level to obtain the new equalized gray level (pixel value).

​ Histogram equalization makes the image color more balanced, the appearance is clearer, and it also makes the image easier to process. It is widely used in medical image processing, license plate recognition, face recognition and other fields.

2. Histogram equalization processing:

Histogram equalization is implemented in OpenCV through the function cv2.equalizeHist(). The specific syntax format of this function is:

  • dst = cv2.equalizeHist(src)
    • src: 8-bit single-channel raw image
    • dst: the result of histogram equalization processing.
import cv2
import matplotlib.pyplot as plt

img = cv2.imread('../lena_dark.bmp', 0)
equ = cv2.equalizeHist(img)

cv2.imshow('img', img)
cv2.imshow('rst', equ)

plt.hist(img.ravel(), 256)
plt.hist(equ.ravel(), 256)
plt.show()

cv2.waitKey()
cv2.destroyAllWindows()

image-20211129195627779

image-20211129195733685

Before histogram equalization, the overall image is brighter; after equalization, the brightness of the image becomes more balanced. The contrast between the histograms of the two images is less obvious. This actually reflects that equalization refers to the result of comprehensive consideration of statistical probability and HVS.

Guess you like

Origin blog.csdn.net/weixin_57440207/article/details/122647033