《OpenCV系列教程》
《深度学习-如何提高数据集质量》
项目位置:OpenCV-Sample
代码位置:39-equalizeHist.py
如果用一句话来表示的话,那就统计每个像素点数值出现的次数,如下图:
这里是一张图片,里面表示的是灰度数值,灰度值为1的有4个点,灰度值为2的有7个数值,灰度值为3的有5个。如果表示为直方图的形式就是下面的状态了。
这里用直方图的形式对像素数值进行了展示。
曝光过度 | 曝光不足 |
---|---|
这里面支持对多通道的均衡处理
import cv2
import numpy as np
from matplotlib import pyplot as plt
def main():
Overexpose = cv2.imread("res/Overexposed.jpg")
underexpose = cv2.imread("res/underexposed.jpg")
#cv2.imshow("Over", Overexpose)
#cv2.imshow("under", underexpose)
plt.figure(1)
plt.subplot(4,2,1)
plt.imshow(Overexpose)
plt.subplot(4,2,2)
plt.imshow(underexpose)
plt.subplot(4, 2, 3)
chans = cv2.split(Overexpose)
colors = ("b", "g", "r")
for (chan, color) in zip(chans, colors):
hist = cv2.calcHist([chan], [0], None, [256], [0, 256])
plt.plot(hist, color=color)
plt.xlim([0, 256])
plt.subplot(4, 2, 4)
chans = cv2.split(underexpose)
colors = ("b", "g", "r")
for (chan, color) in zip(chans, colors):
hist = cv2.calcHist([chan], [0], None, [256], [0, 256])
plt.plot(hist, color=color)
plt.xlim([0, 256])
plt.subplot(4, 2, 5)
equalizeOver = np.zeros(Overexpose.shape, Overexpose.dtype)
equalizeOver[:, :, 0] = cv2.equalizeHist(Overexpose[:, :, 0])
equalizeOver[:, :, 1] = cv2.equalizeHist(Overexpose[:, :, 1])
equalizeOver[:, :, 2] = cv2.equalizeHist(Overexpose[:, :, 2])
plt.imshow(equalizeOver)
cv2.imwrite('./out/equlizeOver.jpg', equalizeOver)
# cv2.imshow('equalizeOver', equalizeOver)
plt.subplot(4, 2, 6)
equalizeUnder = np.zeros(underexpose.shape, underexpose.dtype)
equalizeUnder[:, :, 0] = cv2.equalizeHist(underexpose[:, :, 0])
equalizeUnder[:, :, 1] = cv2.equalizeHist(underexpose[:, :, 1])
equalizeUnder[:, :, 2] = cv2.equalizeHist(underexpose[:, :, 2])
plt.imshow(equalizeUnder)
cv2.imwrite('./out/equalizeunder.jpg', equalizeUnder)
plt.subplot(4, 2, 7)
chans = cv2.split(equalizeOver)
colors = ("b", "g", "r")
for (chan, color) in zip(chans, colors):
hist = cv2.calcHist([chan], [0], None, [256], [0, 256])
plt.plot(hist, color=color)
plt.xlim([0, 256])
plt.subplot(4, 2, 8)
chans = cv2.split(equalizeUnder)
colors = ("b", "g", "r")
for (chan, color) in zip(chans, colors):
hist = cv2.calcHist([chan], [0], None, [256], [0, 256])
plt.plot(hist, color=color)
plt.xlim([0, 256])
plt.show()
cv2.waitKey(0)
if __name__ == '__main__':
main()