import cv2
import numpy as np
from matplotlib import pyplot as plt
#读取同文件夹下的lena图像并做灰度化处理
lena = plt.imread('lena.jpg')
lena = lena[:,:,1]
#显示原图(灰度图像)及其灰度直方图
plt.figure(1)
plt.subplot(221)
plt.imshow(lena,cmap='gray')
plt.subplot(223)
le = lena.flatten()
imhist = plt.hist(le, bins=256, facecolor='black', alpha=0.75)
equ = cv2.equalizeHist(lena) # 得到直方图均衡化后的图像
# 绘制直方图均衡化后的图像及直方图
plt.figure(1)
plt.subplot(222)
plt.imshow(equ,cmap='gray')
plt.subplot(224)
eq = equ.flatten()
imhist2 = plt.hist(eq, bins=256, facecolor='black', alpha=0.75)
#显示图像
plt.show()
![image.png](https://img-blog.csdnimg.cn/img_convert/b212dd1c74c764d9395bd72863d5a041.png#clientId=uef8afe07-7f3b-4&from=paste&height=480&id=u836b2d9c&margin=[object Object]&name=image.png&originHeight=480&originWidth=640&originalType=binary&ratio=1&size=137279&status=done&style=none&taskId=u3c03aebf-c56b-4952-b078-7f3ae64a386&width=640)