img = cv2.imread('test.jpg')
x = []
# get histogram for each channel
for i in cv2.split(img):
hist, bins = np.histogram(i, 256, (0, 256))
# discard colors at each end of the histogram which are used by only 0.05%
tmp = np.where(hist > hist.sum() * 0.0005)[0]
i_min = tmp.min()
i_max = tmp.max()
# stretch hist
tmp = (i.astype(np.int32) - i_min) / (i_max - i_min) * 255
tmp = np.clip(tmp, 0, 255)
x.append(tmp.astype(np.uint8))
# combine image back and show it
s = np.dstack(x)
plt.imshow(s[::,::,::-1])
根据GIMP doc,我们需要丢弃红、绿、蓝柱状图每端的像素颜色,这些直方图只使用图像中0.05%的像素,并尽可能扩展剩余的范围(Python代码):
结果与GIMP的“Colors->Auto->White Balance”之后的结果非常相同
UPD:我们需要}不同地将int32转换为uint8:
^{pr2}$np.clip()
,因为OpenCV
和{为了完成@banderlog013的答案,我认为Gimp Doc指定首先丢弃每个通道的结束像素,然后拉伸剩余的范围。我相信正确的准则是:
我用它效果很好,试试看!在
根据我在快速查看源代码(以及或多或少通过测试图像确认)后的理解,这些代码是不相关的,并且是隐藏的,
Colors>Auto>White Balance
:用合成图像证明:
在此之前:
之后:
所有这些在Python中并不难实现。在
相关问题 更多 >
编程相关推荐