简体   繁体   English

OpenCV和Python中控制视频Stream的对比度和亮度

[英]Controlling Contrast and Brightness of Video Stream in OpenCV and Python

I'm using OpenCV3 and Python 3.7 to capture a live video stream from my webcam and I want to control the brightness and contrast.我正在使用 OpenCV3 和 Python 3.7 从我的网络摄像头捕获实时视频 stream,我想控制亮度和对比度。 I cannot control the camera settings using OpenCV's cap.set(cv2.CAP_PROP_BRIGHTNESS, float) and cap.set(cv2.CAP_PROP_BRIGHTNESS, int) commands so I want to apply the contrast and brightness after each frame is read.我无法使用 OpenCV 的cap.set(cv2.CAP_PROP_BRIGHTNESS, float)cap.set(cv2.CAP_PROP_BRIGHTNESS, int)命令控制相机设置,因此我想在读取每一帧后应用对比度和亮度。 The Numpy array of each captured image is (480, 640, 3).每张拍摄图像的Numpy数组为(480, 640, 3)。 The following code properly displays the video stream without any attempt to change the brightness or contrast.以下代码可以正确显示视频 stream,而无需尝试更改亮度或对比度。

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()

I get a washed-out video stream when I use Numpy's clip() method to control the contrast and brightness, even when I set contrast = 1.0 (no change to contrast) and brightness = 0 (no change to brightness).当我使用 Numpy 的clip()方法控制对比度和亮度时,我得到了一个褪色的视频 stream,即使我设置了contrast = 1.0 (对比度不变)和brightness = 0 (亮度不变)。 Here is my attempt to control contrast and brightness.这是我控制对比度和亮度的尝试。

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()

How can I control the contrast and brightness of a video stream using OpenCV?如何使用 OpenCV 控制视频 stream 的对比度和亮度?

I found the solution using the numpy.clip() method and @fmw42 provided a solution using the cv2.normalize() method.我使用numpy.clip()方法找到了解决方案,@fmw42 使用cv2.normalize()方法提供了解决方案。 I like the cv2.normalize() solution slightly better because it normalizes the pixel values to 0-255 rather than clip them at 0 or 255. Both solutions are provided here.我更喜欢cv2.normalize()解决方案,因为它将像素值归一化为 0-255,而不是将它们裁剪为 0 或 255。此处提供了两种解决方案。

The cv2.normalize() solution: cv2.normalize()解决方案:

  • Brightness - shift the alpha and beta values the same amount.亮度 - 将 alpha 和 beta 值移动相同的量。 Alpha can be negative and beta can be higher than 255. (If alpha >= 255, then the picture is white and if beta <= 0, then the picure is black. alpha 可以是负数,beta 可以高于 255。(如果 alpha >= 255,则图片为白色,如果 beta <= 0,则图片为黑色。
  • Contrast - Widen or shorten the gap between alpha and beta.对比度 - 扩大或缩短 alpha 和 beta 之间的差距。

Here is the code:这是代码:

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()

The numpy.clip() solution: numpy.clip()解决方案:

This helped me solve the problem: How to fast change image brightness with python + OpenCV?这帮助我解决了问题:如何使用 python + OpenCV 快速更改图像亮度? . . I need to:我需要:

  1. Convert Red-Green Blue (RGB) to Hue-Saturation-Value (HSV) first (“Value” is the same as “Brightness”)首先将红绿蓝(RGB)转换为色相饱和度值(HSV)(“值”与“亮度”相同)
  2. “Slice” the Numpy array to the Value portion of the Numpy array and adjust brightness and contrast on that slice将 Numpy 阵列“切片”到 Numpy 阵列的值部分,并调整该切片的亮度和对比度
  3. Convert back from HSV to RGB.从 HSV 转换回 RGB。

Here is the working solution.这是工作解决方案。 Vary the contrast and brightness values.改变contrastbrightness值。 numpy.clip() ensures that all the pixel values remain between 0 and 255 in each on the channels (R, G, and B). numpy.clip()确保通道(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()
import cv2 as cv

cap = cv.VideoCapture(0)

while True:
    # Capture frame-by-frame
    ret, frame = cap.read()
    # normalize the frame
    frame = cv.normalize(
        frame, None, alpha=0, beta=255, norm_type=cv.NORM_MINMAX, dtype=cv.CV_8UC1
    )
    # Display the resulting frame
    cv.imshow("frame", frame)
    # press q to quit
    if cv.waitKey(1) & 0xFF == ord("q"):
        break

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在 opencv python 中获取图像的亮度和对比度 - Get brightness and contrast of an image in opencv python 如何使用 opencv python 自动调整扫描图像的对比度和亮度 - How to auto adjust contrast and brightness of a scanned Image with opencv python 如何使用 opencv python 调整亮度、对比度和振动度? - How do I adjust brightness, contrast and vibrance with opencv python? opencv中亮度和对比度的单位测量 - Unit measurement for brightness and contrast in opencv Python OpenCV cv2 - 将图像的亮度和对比度提高 100% 的简单方法 - Python OpenCV cv2 - Easy way to increase the brightness and contrast of an image by 100% Python/ OpenCV 中的对比度拉伸 - Contrast stretching in Python/ OpenCV 使用 OpenCV 自动调整一张纸的彩色照片的对比度和亮度 - Automatic contrast and brightness adjustment of a color photo of a sheet of paper with OpenCV 在OpenCV中标准化摄像头输入? (对比度/饱和度/亮度等..) - Standardizing camera input in OpenCV? (Contrast/Saturation/Brightness etc..) 使用Python或Vala以编程方式控制Linux Compiz亮度 - Controlling Linux Compiz Brightness Programmatically with Python or Vala Python OpenCV 套接字实时视频流滞后 - Python OpenCV socket live video stream lagging
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM