我找不到 OpenCV 函数,但我找到了该指南:
https://docs.opencv.org/3.4/d3/dc1/tutorial_basic_linear_transform.html
它说您可以通过这种方式调整对比度和亮度:
new_image = old_image * contrast_coeff + brightness_coeff
但是,我没有使用它,因为您可以注意到,如果您只想更改对比度,它不会同时使暗像素变暗和亮像素变亮。
之前我使用了 tensorflow 的对比度调整,所以我使用了它的公式:
float_image = image / 255.0 # image must be float!!
mean = np.mean(image, axis=-1) # compute mean values over each channel
new_image = (image - mean) * contrast_coeff + mean # change contrast
您可能还想将结果转换为 int,因此最好将值削减为 0 以下和 1 以上。您可以对其进行规范化,或者您可以只剪辑:
new_image = np.clip(new_image, a_min=0., a_max=1.) # cut values under 0 and over 1
如您所见,这一切都没有使用 OpenCV,而是使用了 NumPy,所以我希望它适合您的需求。
您可能还想了解不同的对比度公式:https://en.wikipedia.org/wiki/Contrast_(vision)