Get Light Level from Camera

rfj

Pulling my weight
Joined
Oct 26, 2014
Messages
392
Reaction score
115
I am doing home automation (using HomeSeer) and for many actions depend on the light level. For indoor I have plugin light sensors but outside I would have to use wireless sensors. Hence, I was wondering if the cameras could be used as light sensors? I don't think cameras provide any LUX measurements (though that would be cool) but maybe pixel values, gain, aperture and shutter speed are embedded into the images. If so, is there a way to retrieve that from BlueIris to calculate some light value?
 

jaydeel

BIT Beta Team
Joined
Nov 9, 2016
Messages
1,126
Reaction score
1,237
Location
SF Bay Area
I've successfully used the algorithm provided below to test the brightness (or darkness) of a camera image.
In the past I used it to automate the toggling of backlight mode for a camera in a challenging environment.

source
Code:
function getBrightness($gdHandle) {
    $width = imagesx($gdHandle);
    $height = imagesy($gdHandle);
    $totalBrightness = 0;
    $precise=5; /lower is more precise but slower
    for ($x = 0; $x < $width; $x+=$precise) {
        for ($y = 0; $y < $height; $y+=$precise) {
            $rgb = imagecolorat($gdHandle, $x, $y);
            $red = ($rgb >> 16) & 0xFF;
            $green = ($rgb >> 8) & 0xFF;
            $blue = $rgb & 0xFF;
            $totalBrightness += (max($red, $green, $blue) + min($red, $green, $blue)) / 2;
        }
    }
    imagedestroy($gdHandle);
    return ($totalBrightness / (($width/$precise) * ($height/$precise))) / 2.55;

/ USAGE $var=getBrightness(imagecreatefromjpeg('pic1.jpg'));
Here are several more helpful references used in my project:

Image creator: Dynamic Dummy Image Generator - DummyImage.com ... used to create test WxH jpgs filled with single color
 

SpacemanSpiff

Known around here
Joined
Apr 15, 2021
Messages
1,456
Reaction score
2,432
Location
USA
Any way to leverage the functionality of this algorithm to switch individual, or a group of cameras to night mode?

Several indoor cameras in a space that is dark unless it is occupied. This might be a great utility for the task
 
Top