一. 直方圖歸一化
有些灰度圖像的像素並沒有分布在 [0,255] 內,而是分布在 [0,255] 的子區間內。這樣的圖像肉眼看上去往往不是很清晰。我們可以通過直方圖歸一化的方式,將它的像素分布從 [0,255] 的子區間變為 [0,255] 范圍內。通過這樣的方式,往往可以增加圖像的清晰度。
這種歸一化直方圖的操作被稱為灰度變換(Grayscale Transformation)。像素點的取值范圍從 [c,d] 轉換到 [a,b] 的算法如下:
直方圖歸一化算法 ↑
二. 實驗:將一張灰度范圍為 [10,160] 的圖像進行直方圖歸一化,使其灰度范圍為 [0,255]
1 import cv2 2 import numpy as np 3 import matplotlib.pyplot as plt 4 5 # histogram normalization 6 def hist_normalization(img, a=0, b=255): 7 # get max and min 8 c = img.min() 9 d = img.max() 10 11 out = img.copy() 12 13 # normalization 14 out = (b-a) / (d - c) * (out - c) + a 15 out[out < a] = a 16 out[out > b] = b 17 out = out.astype(np.uint8) 18 19 return out 20 21 # Read image 22 img = cv2.imread("../head_g_n.jpg",0).astype(np.float) 23 # histogram normalization 24 out = hist_normalization(img) 25 26 # Display histogram 27 plt.hist(out.ravel(), bins=255, rwidth=0.8, range=(0, 255)) 28 plt.savefig("out_his.jpg") 29 plt.show() 30 31 # Save result 32 cv2.imshow("result", out) 33 cv2.imwrite("out.jpg", out) 34 cv2.waitKey(0) 35 cv2.destroyAllWindows()
三. 實驗結果及分析
原圖像像素分布直方圖[10,160] ↑
原圖像 ↑
歸一化后的圖像像素分布直方圖[0,255] ↑
歸一化后的圖像 ↑
可以看到,我們將灰度范圍為 [10,160] 的圖像進行直方圖歸一化到 [0,255] 后,圖像的清晰度顯著增強。
四. 參考內容:
