一. 直方圖均衡化:
直方圖均衡化是使圖像直方圖變得平坦的操作。直方圖均衡化能夠有效地解決圖像整體過暗、過亮的問題,增加圖像的清晰度。
具體流程如下所示。其中S是總的像素數,Zmax是像素的最大取值(8位灰度圖像為255),h(i)為圖像像素取值為 i 及 小於 i 的像素的總數。

直方圖均衡化算法 ↑
二. python實現直方圖均衡化操作
1 import cv2 2 import numpy as np 3 import matplotlib.pyplot as plt 4 5 # histogram equalization 6 def hist_equal(img, z_max=255): 7 H, W = img.shape 8 # S is the total of pixels 9 S = H * W * 1. 10 11 out = img.copy() 12 13 sum_h = 0. 14 15 for i in range(1, 255): 16 ind = np.where(img == i) 17 sum_h += len(img[ind]) 18 z_prime = z_max / S * sum_h 19 out[ind] = z_prime 20 21 out = out.astype(np.uint8) 22 23 return out 24 25 26 # Read image 27 img = cv2.imread("../head_g_n.jpg",0).astype(np.float) 28 29 # histogram normalization 30 out = hist_equal(img) 31 32 # Display histogram 33 plt.hist(out.ravel(), bins=255, rwidth=0.8, range=(0, 255)) 34 plt.savefig("out_his.png") 35 plt.show() 36 37 # Save result 38 cv2.imshow("result", out) 39 cv2.imwrite("out.jpg", out) 40 cv2.waitKey(0) 41 cv2.destroyAllWindows()
三. 實驗結果:

原圖 ↑

原圖的像素分布直方圖 ↑

直方圖均衡化后圖像 ↑

均衡化后圖像的直方圖 ↑
可以看到,直方圖均衡化后的圖像看起來比原來的圖像更加清晰。對於圖像亮度整體偏暗或者偏亮的圖像,我們可以采用直方圖均衡化的方法處理圖像,使得它們看上去更加清晰。
四. matlab 實現圖像直方圖均衡化:
可以參考一篇優秀的博文: https://blog.csdn.net/Ibelievesunshine/article/details/79961027
五. 參考內容: