I am looking around how to reduce the brightness of the webcam image. Can you tell me how to do it?
I couldn't find anything ...
To increase the value I use this function (I increase the brightness with a trackbar):
def change_brightness(frame, value):
hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
h, s, v = cv2.split(hsv)
lim = 255 - value
v[v > lim] = 255
v[v <= lim] = value
final_hsv = cv2.merge((h, s, v))
img = cv2.cvtColor(final_hsv, cv2.COLOR_HSV2BGR)
return img
This is the full code:
cv2.namedWindow("VIDEO")
#cv2.createTrackbar("alpha", "VIDEO", 0, 4, nothing)
cv2.createTrackbar("gamma", "VIDEO", 50, 100, nothing)
while True:
k = cv2.waitKey(1)
_, frame = video.read()
frame = cv2.resize(frame, FRAME_DIM) # frame contiene l'immagine della webcam
frame = cv2.flip(frame, 1) # giro orizzontalmente (parametro = 1) l'immagine
#test = cv2.getTrackbarPos("alpha", "VIDEO")
test = cv2.getTrackbarPos("gamma", "VIDEO")
gamma = cv2.getTrackbarPos("gamma", "VIDEO")
frame = change_brightness(frame, gamma)
if (k % 256 == ord("q")): # PATH, folder_name, full_path, IMG_LABELquando premo "q" si chiude la webcam e la relativa finestra
cv2.destroyAllWindows()
break
camera = cv2.imshow("VIDEO", frame)
CodePudding user response:
This seems to work; the essence is that the slider goes from 0 to 510, so you subtract an offset to get values to add or subtract to the 'v' value of HSV. The original code used a neat trick to easily go through all elements of the image to add brightness; the trick is repeated for subtracting off values. For example, if you subtract off -10 in brightness, anything lower than 10 is zero; otherwise, subtract off.
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Sun Feb 6 18:06:51 2022
https://stackoverflow.com/questions/71011111/reduce-brigthness-in-real-time-opencv-python#comment125531455_71011111
"""
import cv2
def change_brightness(frame, value):
hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
h, s, v = cv2.split(hsv)
# increase brightness; input value is 1 to 255
if(value>0):
lim_upper = 255 - value
# If v>lim, then set to 255
v[v > lim_upper] = 255
# if v<lim, then add
v[v <= lim_upper] = value
# decrease brightness; input value from 0 to -255
else:
lim_lower = -value
v[v < lim_lower] = 0
v[v >= lim_lower] -= -value
final_hsv = cv2.merge((h, s, v))
img = cv2.cvtColor(final_hsv, cv2.COLOR_HSV2BGR)
return img
# Read from video camera
video_capture_device_index = 0
webcam = cv2.VideoCapture(video_capture_device_index)
cv2.namedWindow("VIDEO")
cv2.createTrackbar("gamma", "VIDEO", 255, 510,(lambda a: None))
while True:
ret, frame = webcam.read()
k = cv2.waitKey(1)
# flip horizontally (mirror)
frame = cv2.flip(frame, 1) # giro orizzontalmente (parametro = 1) l'immagine
# varies from 0-510, so subtract so range is -255 to 255
gamma = cv2.getTrackbarPos("gamma", "VIDEO")
gamma = gamma - 255
frame = change_brightness(frame, gamma)
# Use q to quit
if (k == ord("q")): # PATH, folder_name, full_path, IMG_LABELquando premo "q" si chiude la webcam e la relativa finestra
cv2.destroyAllWindows()
break
camera = cv2.imshow("VIDEO", frame)
webcam.release()
print("end video.")