OpenCV利用python来实现图像的直方图均衡化

1.直方图

`cv2.calcHist(images, channels, mask, histSize, ranges):`

-img: 图像
-channels: 选取图像的哪个通道
-histSize: 直方图大小
-ranges: 直方图范围``` ```

`cv2.minMaxLoc:` 返回直方图的最大最小值，以及他们的索引

```import cv2
import numpy as np
def ImageHist(image, type):
color = (255, 255,255)
windowName = "Gray"
if type == 1:    #判断通道颜色类型 B-G-R
color = (255, 0, 0)
windowName = "B hist"
elif type == 2:
color = (0,255,0)
windowName = "G hist"
else:
color = (0,0,255)
# 得到直方图
hist = cv2.calcHist([image],[0],None,[256],[0,255])
# 得到最大值和最小值
minV,maxV,minL,maxL = cv2.minMaxLoc(hist)
histImg = np.zeros([256,256,3],np.uint8)
#直方图归一化
for h in range(256):
interNormal = int(hist[h] / maxV * 256)
cv2.line(histImg, (h, 256), (h, 256 - interNormal), color)
cv2.imshow(windowName, histImg)
return histImg
img = cv2.imread("img.jpg", 1)
channels = cv2.split(img) # R-G-B
for i in range(3):
ImageHist(channels[i], 1 + i)
cv2.waitKey(0)
```

2.直方图均衡化

```import cv2
import numpy as np
img = cv2.imread("img.jpg", 1)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
dat = cv2.equalizeHist(gray)
cv2.imshow("gray", gray)a
cv2.imshow("dat", dat)
cv2.waitKey(0)
```

```import cv2
import numpy as np
img = cv2.imread("img.jpg", 1)
cv2.imshow("img", img)
(b, g, r) = cv2.split(img)
bH = cv2.equalizeHist(b)
gH = cv2.equalizeHist(g)
rH = cv2.equalizeHist(r)
dat = cv2.merge((bH, gH, rH))
cv2.imshow("dat", dat)
cv2.waitKey(0)
```

D:Anacondalibsite-packages umpy\_distributor_init.py:32: UserWarning: loaded more than 1 DLL from .libs:
D:Anacondalibsite-packages umpy.libslibopenblas.NOIJJG62EMASZI6NYURL6JBKM4EVBGM7.gfortran-win_amd64.dll
D:Anacondalibsite-packages umpy.libslibopenblas.PYQHXLVVQ7VESDPUVUADXEVJOBGHJPAY.gfortran-win_amd64.dll
stacklevel=1)

3.源代码实现直方图均衡化

```import cv2
import numpy as np
import matplotlib.pyplot as plt
img = cv2.imread("img.jpg", 1)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
count = np.zeros(256, np.float)
for i in range(img.shape[0]):
for j in range(img.shape[1]):
count[int(gray[i, j])] += 1 # 统计该像素出现的次数
count = count / (img.shape[0] * img.shape[1]) # 得到概率
x = np.linspace(0,255,256)
plt.bar(x, count,color = "b")
plt.show()

# 计算累计概率

for i in range(1,256):
count[i] += count[i - 1]
# 映射
map1 = count * 255
for i in range(img.shape[0]):
for j in range(img.shape[1]):
p = gray[i, j]
gray[i, j] = map1[p]
cv2.imshow("gray", gray)
cv2.waitKey(0)
```

```import cv2
import numpy as np
import matplotlib.pyplot as plt
img = cv2.imread("img.jpg", 1)
# R-G-B三种染色直方图
countb = np.zeros(256, np.float32)
countg = np.zeros(256, np.float32)
countr = np.zeros(256, np.float32)

for i in range(img.shape[0]):
for j in range(img.shape[1]):
(b,g,r) = img[i,j]
b = int(b)
g = int(g)
r = int(r)
countb[b] += 1 # 统计该像素出现的次数
countg[g] += 1
countr[r] += 1
countb = countb / (img.shape[0] * img.shape[1]) # 得到概率
countg = countg / (img.shape[0] * img.shape[1])
countr = countr / (img.shape[0] * img.shape[1])
x = np.linspace(0,255,256)
plt.figure()
plt.bar(x, countb,color = "b")
plt.figure()
plt.bar(x, countg,color = "g")
plt.figure()
plt.bar(x, countr,color = "r")
plt.show()

# 计算直方图累计概率
for i in range(1,256):
countb[i] += countb[i - 1]
countg[i] += countg[i - 1]
countr[i] += countr[i - 1]
#映射表
mapb = countb * 255
mapg = countg * 255
mapr = countr * 255

dat = np.zeros(img.shape, np.uint8)
for i in range(img.shape[0]):
for j in range(img.shape[1]):
(b,g,r) = img[i, j]
dat[i, j] = (mapb[b],mapg[g],mapr[r])
cv2.imshow("dat", dat)
cv2.waitKey(0)

```

R-G-B 3 种颜色通道的直方图如下：