提示:内容整理自:https://github.com/gzr2017/ImageProcessing100Wen
CV小白从0开始学数字图像处理
22 直方图操作
让直方图的平均值m0=128,标准差s0=52。
这里并不是变更直方图的动态范围,而是让直方图变得平坦。
可以使用下式将平均值为m标准差为s的直方图变成平均值为m0标准差为s0的直方图:
xout = s0 / s * (xin - m) + m0
代码如下:
1.引入库
CV2计算机视觉库
import cv2
import numpy as np
import matplotlib.pyplot as plt
2.读入数据
img = cv2.imread("imori_dark.jpg").astype(np.float)
H, W, C = img.shape
3.Trans [0, 255]
m0 = 128
s0 = 52
m = np.mean(img)
s = np.std(img)
out = img.copy()
out = s0 / s * (out - m) + m0
out[out < 0] = 0
out[out > 255] = 255
out = out.astype(np.uint8)
4.直方图展示
plt.hist(out.ravel(), bins=255, rwidth=0.8, range=(0, 255))
plt.savefig("out_his.png")
plt.show()