Histogram equalization in traditional image processing

Important note: This article is compiled from online information. It only records the blogger's process of learning relevant knowledge points and has not been deleted.

1. Reference materials

The principle of histogram equalization and the realization of
histogram equalization in image processing

2. Histogram

1. The concept of histogram

The grayscale histogram of an image describes the grayscale distribution in the image and can intuitively show the proportion of each grayscale level in the image. The grayscale histogram of an image is a function of grayscale, describing the number of pixels with this grayscale in the image: where the abscissa is the grayscale, and the ordinate is the number of times this grayscale occurs. As shown below:
Insert image description here

2. Properties of histograms

  1. The histogram reflects the gray distribution pattern in the image. It describes the number of pixels in each gray level, but does not include the location information of these pixels in the image.The image histogram does not care about the spatial position of the pixels, so it is not affected by changes in image rotation and translation, and can be used as a feature of the image.
  2. Any specific image has a unique histogram corresponding to it, but different images can have the same histogram.

3. cv2.calcHist()

函数原型:calcHist(images,channels,mask,histSize,ranges,hist=None,accumulate=None)

Function: Calculate image histogram.

Parameter explanation

  • images: Image matrix, for example: [image];
  • channels: Channel number, for example: 0;
  • mask:Mask, usually: None;
  • histSize: Histogram size, generally equal to the number of gray levels;
  • ranges: Horizontal axis range.

4. Sample code

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


# 获取灰度图像
img = cv2.imread("lena_std.tif", 0)
# cv2.imshow("src", img)

# 灰度图像的直方图
hist = cv2.calcHist([img],[0],None,[256],[0,256])
plt.figure() #新建一个图像
plt.hist(img.ravel(), 256)
plt.show()

Draw a histogram as shown below:
Insert image description here

3. Histogram equalization

1 Introduction

If the gray level distribution of the image is uneven , its gray level distribution is concentrated in a narrow range, making the details of the image not clear enough and the contrast low . Two transformations, histogram equalization and histogram specification , are usually used to expand the grayscale range of the image or evenly distribute the grayscale , thereby increasing the contrast and making the image details clear to achieve the purpose of image enhancement.

Histogram equalization, non-linear stretching of the image, redistributes the gray value of the image, so that the gray value of the image within a certain range is approximately equal. In this way, the contrast of the peak part in the middle of the original histogram is enhanced, while the contrast of the valley parts on both sides is reduced, and the histogram of the output image is a relatively flat histogram.

2. Equalization algorithm

Histogram equalization is actually a grayscale transformation process, which transforms the current grayscale distribution into an image with a wider range and a more uniform grayscale distribution through a transformation function. That is, the histogram of the original image is modified to be roughly evenly distributed within the entire grayscale range, thus expanding the dynamic range of the image and enhancing the contrast of the image. Usually the transformation function selected for equalization is the cumulative probability of grayscale. The steps of the histogram equalization algorithm are:

  1. Scan each pixel of the original grayscale image in turn to calculate the grayscale histogram of the image ;
  2. Calculate the cumulative distribution function of the grayscale histogram ;
  3. According to the cumulative distribution function and histogram equalization principle, the mapping relationship between input and output is obtained;
  4. Finally, the image transformation is performed according to the result obtained from the mapping relationship.

For example, as shown in the figure below, the principle and process of histogram equalization can be intuitively understood:
Insert image description here

3. cv2.equalizeHist()

Function prototype: equalizeHist(src, dst=None)

Function: Calculate histogram equalization.

Parameter explanation

  • src:Image matrix (single channel image);
  • dst: Default is enough.

4. Code examples

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


# 获取灰度图像
img = cv2.imread("lena_std.tif", 0)
cv2.imshow("src", img)

# 灰度图像的直方图
hist = cv2.calcHist([img],[0],None,[256],[0,256])
plt.figure() #新建一个图像
plt.hist(img.ravel(), 256)
plt.show()

# 灰度图像直方图均衡化
dst = cv2.equalizeHist(img)

# 直方图
hist = cv2.calcHist([dst],[0],None,[256],[0,256])

plt.figure()
plt.hist(dst.ravel(), 256)
plt.show()

cv2.imshow("dst", dst)

cv2.waitKey(0)

Histogram of src image
Insert image description here

Histogram after histogram equalization
Insert image description here

Comparison of src source image and dst image
Insert image description here

Color histogram equalization

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

# 彩色图像直方图均衡化
img = cv2.imread("lena_std.tif", 1)
cv2.imshow("src", img)

# 彩色图像均衡化,需要分解通道 对每一个通道均衡化
(b, g, r) = cv2.split(img)
bH = cv2.equalizeHist(b)
gH = cv2.equalizeHist(g)
rH = cv2.equalizeHist(r)
# 合并每一个通道
result = cv2.merge((bH, gH, rH))
cv2.imshow("dst_rgb", result)

cv2.waitKey(0)

Comparison of src source image and dst image
Insert image description here

Guess you like

Origin blog.csdn.net/m0_37605642/article/details/132352475