python - 在 OpenCV 和 Python 中控制视频流的对比度和亮度

标签 python opencv video brightness contrast

我正在使用 OpenCV3 和 Python 3.7 从我的网络摄像头捕获实时视频流,我想控制亮度和对比度。我无法使用 OpenCV 的 cap.set(cv2.CAP_PROP_BRIGHTNESS, float)cap.set(cv2.CAP_PROP_BRIGHTNESS, int) 命令控制相机设置,所以我想应用读取每帧后的对比度和亮度。每个捕获图像的 Numpy 数组是 (480, 640, 3)。以下代码可以正确显示视频流,而无需尝试更改亮度或对比度。

import numpy as np
import cv2

cap = cv2.VideoCapture(0)    
while(True):
    # Capture frame-by-frame
    ret, frame = cap.read()
    cv2.imshow('frame',frame)
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break
# When everything done, release the capture
cap.release()
cv2.destroyAllWindows()

当我使用 Numpy 的 clip() 方法控制对比度和亮度时,我得到了一个褪色的视频流,即使我设置了 contrast = 1.0(没有变化对比)和 brightness = 0(不改变亮度)。这是我控制对比度和亮度的尝试。

import numpy as np
import cv2

cap = cv2.VideoCapture(0)
while(True):
    # Capture frame-by-frame
    ret, frame = cap.read()
    contrast = 1.0
    brightness = 0
    frame = np.clip(contrast * frame + brightness, 0, 255)
    cv2.imshow('frame',frame)
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break
# When everything done, release the capture
cap.release()
cv2.destroyAllWindows()

如何使用 OpenCV 控制视频流的对比度和亮度?

最佳答案

我找到了使用 numpy.clip() 方法的解决方案,@fmw42 提供了使用 cv2.normalize() 方法的解决方案。我更喜欢 cv2.normalize() 解决方案,因为它将像素值标准化为 0-255,而不是将它们裁剪为 0 或 255。此处提供了两种解决方案。

cv2.normalize() 解决方案:

  • 亮度 - 将 alpha 和 beta 值移动相同的量。 Α 可以是负数,beta 可以高于 255。(如果 alpha >= 255, 则图片为白色,如果 beta <= 0,则图片为黑色。
  • 对比 - 扩大或缩短 alpha 和 beta 之间的差距。

代码如下:

import numpy as np
import cv2

cap = cv2.VideoCapture(0)
while(True):
    # Capture frame-by-frame
    ret, frame = cap.read()
    cv2.normalize(frame, frame, 0, 255, cv2.NORM_MINMAX)
    cv2.imshow('frame',frame)
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break
# When everything done, release the capture
cap.release()
cv2.destroyAllWindows()

numpy.clip() 解决方案:

这帮助我解决了问题:How to fast change image brightness with python + OpenCV? .我需要:

  1. 首先将红-绿-蓝 (RGB) 转换为色调-饱和度-值 (HSV) (“值”与“亮度”相同)
  2. 将 Numpy 数组“切片”到 Numpy 数组的值部分,并调整该切片的亮度和对比度
  3. 从 HSV 转换回 RGB。

这是有效的解决方案。改变 contrastbrightness 值。 numpy.clip() 确保每个 channel (R、G 和 B)上的所有像素值都保持在 0 到 255 之间。

import numpy as np
import cv2

cap = cv2.VideoCapture(0)

while(True):
    # Capture frame-by-frame
    ret, frame = cap.read()
    frame = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
    contrast = 1.25
    brightness = 50
    frame[:,:,2] = np.clip(contrast * frame[:,:,2] + brightness, 0, 255)
    frame = cv2.cvtColor(frame, cv2.COLOR_HSV2BGR)
    cv2.imshow('frame',frame)
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break
# When everything done, release the capture
cap.release()
cv2.destroyAllWindows()

关于python - 在 OpenCV 和 Python 中控制视频流的对比度和亮度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61016954/

相关文章:

c++ - OpenCV Videocapture 抓取和检索

html - 如何在页面加载时完全加载 HTML5 视频?

python - 如何将pygame相机保存为视频输出

python - 如何向 json 字典添加键值列表

python - &符号在打印功能中如何工作?

python - 将标识符行添加到数据框,然后写入 excel 文件

python - 麻烦与代码-> Python识别报纸上的面孔

Python - 读取具有不同分隔符的数据文本文件

android - 在 JNI 中将位图转换为 Opencv::Mat

javascript - 如何降低自动播放 html 视频的音量