-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathHistogram Equalization.py
54 lines (47 loc) · 1.51 KB
/
Histogram Equalization.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import cv2
import numpy as np
import matplotlib.pyplot as plt
path = 'C:\\Users\\rabbear\\Desktop\\sky.jpg'
img = cv2.imread(path)
# plt.hist(img.ravel(),256,[0,256])
# plt.show()
def calcGrayHist(I):
# 计算灰度直方图
h, w = I.shape[:2]
grayHist = np.zeros([256], np.uint64)
for i in range(h):
for j in range(w):
grayHist[I[i][j]] += 1
return grayHist
def equalHist(img):
# 灰度图像矩阵的高、宽
h, w = img.shape[0], img.shape[1]
# 第一步:计算灰度直方图
grayHist = calcGrayHist(img)
# 第二步:计算累加灰度直方图
zeroCumuMoment = np.zeros([256], np.uint32)
for p in range(256):
if p == 0:
zeroCumuMoment[p] = grayHist[0]
else:
zeroCumuMoment[p] = zeroCumuMoment[p - 1] + grayHist[p]
# 第三步:根据累加灰度直方图得到输入灰度级和输出灰度级之间的映射关系
outPut_q = np.zeros([256], np.uint8)
cofficient = 256.0 / (h * w)
for p in range(256):
q = cofficient * float(zeroCumuMoment[p]) - 1
if q >= 0:
outPut_q[p] = np.floor(q)
else:
outPut_q[p] = 0
# 第四步:得到直方图均衡化后的图像
equalHistImage = np.zeros(img.shape, np.uint8)
for i in range(h):
for j in range(w):
equalHistImage[i][j] = outPut_q[img[i][j]]
return equalHistImage
# 使用自己写的函数实现
equa = equalHist(img)
#plt.hist(equa.ravel(),256,[0,256])
plt.imshow(equa)
plt.show()