python自动调整图片_用OpenCV自动调整图像亮度

  • Post author:
  • Post category:python


您可以尝试使用对比度优化和直方图剪切来自动调整亮度。可以通过增加直方图剪辑百分比(clip_hist_percent)来增加目标亮度。这是25%剪切的结果


α和β是自动计算的alpha 3.072289156626506

beta -144.3975903614458

这是剪辑的可视化效果。蓝色(原始),橙色(自动调整后)。在

剪切结果为35%



alpha 3.8059701492537314

beta -201.71641791044777import cv2

import numpy as np

# from matplotlib import pyplot as plt

# Automatic brightness and contrast optimization with optional histogram clipping

def automatic_brightness_and_contrast(image, clip_hist_percent=25):

gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

# Calculate grayscale histogram

hist = cv2.calcHist([gray],[0],None,[256],[0,256])

hist_size = len(hist)