How to maintain a constant brightness in an input image from the live video stream

My application is to process the incoming image input from the live video stream and detect certain objects from that. I think changes in lights are affecting my result’s accuracy. So I need to fix the value of the brightness, and to do so I am using the V parameter from the SHV color model.

I am deriving a mean value of the V parameter from the input image and try to keep that value nearer to the predefined setpoint value. I’m using the bellow mentioned python code for this process.

ret, frame = cap.read()
hsvImage = cvtColor(frame, COLOR_BGR2HSV)
h,s,v = split(hsvImage)
live_value = mean(v)

brightness_factor = predefined_setpoint / live_value 

hsvImage[...,2] = hsvImage[...,2] * brightness_factor

frame = cvtColor(hsvImage, COLOR_HSV2BGR)

From this method, I’m getting a better result. But still, I want to verify that whether I’m applying this logic in the right way or not.

More importantly, my main concern is with the execution frequency of the code. This method is reducing the execution frequency of my python code drastically.

Is there any lightweight process or inbuilt method available to achieve the same goal?

welcome.

please refrain from using tags that are generic. I’ve removed those.

usually, cameras are supposed to adjust exposure/brightness on their own. if your camera can’t do that, you can do it yourself. if it can, you should not do that.

you can avoid converting to HSV and back by doing all operations on RGB/BGR data.

calculate the mean on the RGB values: frame.mean(axis=(0,1)). this results in a 3-element vector (RGB/BGR color). then calculate a gray value from that (with proper weights for R/G/B… or do the mean without any axis argument) and use this for your brightness_factor. then multiply frame.

be warned, OpenCV operations use saturation arithmetic, numpy operations don’t. this can cause artefacts when values exceed the uint8 range (0…255).

cross-post: