按照知乎上一篇文章的思路,构造了这样一个图片矩阵img,它总共有300个像素,其中100个灰度为50,100个灰度为51,100个灰度为52.
通过对整张图进行直方图均衡化处理,代码如下:
import cv2
import numpy as np
import matplotlib.pyplot as plt
img = np.array([50]*100 + [51]*100 + [52]*100)
img = img.astype(np.uint8) # 原图片矩阵
hist = cv2.calcHist([img], [0], None, [256], [0,256])
hists_cumsum = np.cumsum(hist)
const_a = 256/300
hists_cdf = const_a * hists_cumsum
hists_cdf[hists_cdf>255] = 255
hists_cdf = hists_cdf.astype(np.uint8)
#mapping
img_eq = hists_cdf[img] #整体直方图均衡化后的图矩阵
灰度为 50-> 85
51->170
52->255
但是用opencv自带的函数,进行处理后结果如下:
equ = cv2.equalizeHist(img)
灰度为 50->0
51->128
52->255
为什么会有这样的差异呢,但是看opencv的文档,这个函数也是整体上进行直方图均衡化处理呀。
请大神解答下