Digital image processing (17): histogram equalization

Disclaimer: This article is a blogger original article, reproduced, please indicate the link. https://blog.csdn.net/zaishuiyifangxym/article/details/89813977

table of Contents

1 Introduction histogram equalization

1.1 histogram equalization concept

Theoretical basis 1.2 histogram equalization

1.3 histogram equalization step

1.4 histogram equalization scenarios

2 histogram equalization -equalizeHist ()

3 matplotlib.pyplot.subplot () function

4 matplotlib.pyplot.imshow () function

5 histogram equalization contrast

Reference material


 

1 Introduction histogram equalization

1.1 histogram equalization concept

Histogram equalization ( the Histogram Equalization ) is the probability density distribution of an image gray known through a transformation that has so evolved into a homogeneous new image gray probability density distribution.

As shown below, too dark and too bright image after histogram equalization, so that the image becomes clear.

 

Theoretical basis 1.2 histogram equalization

Premise: If an image occupies all possible gray level, and uniformly distributed.

Conclusion: The image having a high contrast and changing shades of gray.

Appearance: images rich in detail and higher quality.

 

1.3 histogram equalization step

(1) Calculate the cumulative histogram;

(2) converting cumulative histograms interval;

(3) cumulative histogram, similar to the original probability value is treated as the same value.

 

Specific examples are as follows:

, The pixel is known as a distribution of the image in FIG. 7 \times. 7, according to the pixel value, the histogram can be calculated

 

The histogram can be calculated from the normalized histogram and cumulative histogram, as shown below:

 

The cumulative histogram range converter, as shown below:

 

From the results of the figure, the original eight gray-level gray scale into six, the original histogram equalization and histogram:

 

The above gray level is 8, then converted to 256 gray scale, a similar calculation method, as shown below:

 

Similarly, the original histogram and histogram equalization:

 

由上图可以看出,虽然二者相似,但右侧均衡化后的直方图分布更均匀,相邻像素级概率和与高概率近似相等。如果将两张图的灰度级放在同一区间,可以看出差别更大,如下图所示:

 

1.4 直方图均衡化应用场景

(1)医学图像处理

(2)车牌照识别

(3)人脸识别


 

2 直方图均衡化-equalizeHist()

OpenCV库下,直方图均衡化使用  equalizeHist() 函数,函数用法如下所示:

dst=cv2.equalizeHist(src)

其中,参数:

dst 表示处理结果

src 表示原始图像

 

代码如下所示:

#encoding:utf-8
import cv2
import numpy as np
import matplotlib.pyplot as plt
img = cv2.imread('zxp.jpg', cv2.IMREAD_GRAYSCALE)
equ = cv2.equalizeHist(img)


cv2.imshow("src", img)
cv2.imshow("result", equ)


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

cv2.waitKey(0)
cv2.destroyAllWindows()

 

运行结果如下图所示:(左图为原始灰度图像,右图为均衡化后的图像


 

3 matplotlib.pyplot.subplot() 函数

matplotlib.pyplot.subplot() 函数可以将多张图像放在一个窗口内,Pyhton下需要导入 matplotlib.pyplot 绘图包,其用法和Matlab中的subplot()函数用法类似。matplotlib 是一个强大的绘图包subplot() 函数用法如下所示:

subplot(nrows, ncols, plot_number)

其中,参数:

nrows 表示行数;

ncols  表示列数;

plot_number 表示窗口序号。

例如:排列成两行三列的图像,如下图所示:

:在实际应用中,如果每一个参数都小于10,三个数字可以连着直接写,不加标点,如 subplot(2,3,4) 可以写成 subplot(234)

 

代码如下所示:

#encoding:utf-8
import cv2
import numpy as np
import matplotlib.pyplot as plt
img = cv2.imread('zxp.jpg', cv2.IMREAD_GRAYSCALE)
equ = cv2.equalizeHist(img)

plt.subplot(221),plt.imshow(img, 'gray'),plt.title('img'), plt.xticks([]),plt.yticks([])
plt.subplot(222),plt.imshow(equ, 'gray'),plt.title('equ'), plt.xticks([]),plt.yticks([])
plt.subplot(223),plt.hist(img.ravel(),256),plt.title('img_hist')
plt.subplot(224),plt.hist(equ.ravel(),256),plt.title('equ_hist')

plt.show()

cv2.waitKey(0)
cv2.destroyAllWindows()

 

运行结果如下图所示:

 


 

4 matplotlib.pyplot.imshow() 函数

imshow() 函数用法如下所示,同样的,Pyhton下需要导入 matplotlib.pyplot 绘图包。

imshow(X, cmap=None)

其中,参数:

X 表示要绘制的图像;

cmap 表示colormap,颜色图谱,默认为RGB(A)颜色空间:

    1)对于灰度图像,使用参数 “ cmap=plt.cm.gray ”;

    2)对于彩色图像,如果使用opencv读入的图像,默认空间为BRG,需要调整色彩空间为RGB。

 

下面是分别使用函数读取灰度图像彩色图像例子。

(1)灰度图像

代码如下所示:

#encoding:utf-8
import cv2
import numpy as np
import matplotlib.pyplot as plt
img = cv2.imread('zxp.jpg')
img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

plt.subplot(221),plt.imshow(img),plt.title('img'), plt.axis('off') #坐标轴关闭
plt.subplot(222),plt.imshow(img, cmap=plt.cm.gray),plt.title('img_cmap'), plt.axis('off')

plt.subplot(223),plt.imshow(img_gray),plt.title('img_gray'), plt.axis('off')
plt.subplot(224),plt.imshow(img_gray, cmap=plt.cm.gray),plt.title('img_gray_cmap'),plt.axis('off')#正确用法


plt.show()

cv2.waitKey(0)
cv2.destroyAllWindows()

 

运行结果如下图所示:

 

 

(2)彩色图像

代码如下所示:

#encoding:utf-8
import cv2
import numpy as np
import matplotlib.pyplot as plt
img = cv2.imread('zxp.jpg')
b,g,r=cv2.split(img) #通道分割
img_RGB=cv2.merge([r,g,b])#通道组合

plt.subplot(121),plt.imshow(img),plt.title('img_BGR'), plt.axis('off') #坐标轴关闭
plt.subplot(122),plt.imshow(img_RGB),plt.title('img_RGB'), plt.axis('off')

 

运行结果如下图所示:

 


 

5 直方图均衡化对比

直方图均衡化前后对比,使用前面提到的 matplotlib.pyplot.subplot() 函数 和 matplotlib.pyplot.imshow() 函数。

 

代码如下所示:

#encoding:utf-8
import cv2
import numpy as np
import matplotlib.pyplot as plt
img_gray = cv2.imread('zxp.jpg', cv2.IMREAD_GRAYSCALE)
equ = cv2.equalizeHist(img_gray)

plt.subplot(221),plt.imshow(img_gray, cmap=plt.cm.gray),plt.title('img_gray'), plt.axis('off') #坐标轴关闭
plt.subplot(222),plt.imshow(equ, cmap=plt.cm.gray),plt.title('equ'), plt.axis('off') #坐标轴关闭
plt.subplot(223),plt.hist(img_gray.ravel(),256),plt.title('img_gray_hist')
plt.subplot(224),plt.hist(equ.ravel(),256),plt.title('equ_hist')

plt.show()

cv2.waitKey(0)
cv2.destroyAllWindows()

 

运行结果如下图所示:

 


 

参考资料

[. 1]  the Python image processing the OpenCV +

Guess you like

Origin blog.csdn.net/zaishuiyifangxym/article/details/89813977