我正在使用OpenCV3和Python3.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控制视频流的对比度和亮度?
发布于 2020-04-04 00:11:21
我找到了使用numpy.clip()方法的解决方案,@fmw42 42提供了一个使用cv2.normalize()方法的解决方案。我更喜欢cv2.normalize()解决方案,因为它将像素值标准化为0-255,而不是在0或255处剪切它们。这两种解决方案都在这里提供。
cv2.normalize() 解决方案:
之间的距离。)
以下是代码:
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?。我需要:
slice
的亮度和对比度。
这是可行的解决办法。更改contrast和brightness值。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()发布于 2022-03-10 09:21:04
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"):
breakhttps://stackoverflow.com/questions/61016954
复制相似问题