[英]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()
解决方案:
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:我需要:
Here is the working solution.这是工作解决方案。 Vary the contrast
and brightness
values.改变contrast
和brightness
值。 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.