My iphone application captures the realtime data from camera using AVFoundation's AVCaptureSession. I'm able to access that data in it's delegate method in runtime and create an image out of it. It can be CGImage, UIImage or just raw data (CMSampleBufferRef).
What I'm trying to do is to calculate luminance, brightness of that data (image). Or it can be some other value that can indicate me how bright the input light is.
Is there any standard way to get this value? Perhaps using OpenGL.
Thanks in advance.
Here are 3 ways to calculate Luminance: Luminance (standard for certain colour spaces): (0.2126*R + 0.7152*G + 0.0722*B) source. Luminance (perceived option 1): (0.299*R + 0.587*G + 0.114*B) source.
Because luminance and illuminance are quantifiable, they are not interchangeable with brightness. Luminance is the measurable quality of light that most closely corresponds to brightness, which we cannot objectively measure.
Luminance is a photometric measure of the luminous intensity per unit area of light travelling in a given direction. It describes the amount of light that passes through, is emitted from, or is reflected from a particular area, and falls within a given solid angle.
Just convert your image to YUV format and calculate the average of luma channel. Color conversion is a typical operation and any decent image processing framework supports it. For example, OpenCV (you said OpenGL, but that really doesn't have anything to do with image processing, I'm assuming you meant OpenCV) has CvtColor.
If you don't have such a framework handy but have access to the pixel intensities, you can use the equation:
Y' = 0.299*R + 0.587*G + 0.144*B
to get the luma channel, and then calculate the average. R, G and B represent the red, green and blue channels respectively.
EDIT
Note that it is possible that your camera will compensate for bright/dark scenes by modifying its aperture. This is camera-dependent, but I think most cameras do this -- otherwise your pictures can end up either saturated (pure white) or plain black -- in either case, useless. Human eyes actually do the same thing.
The downside is that's it's hard to tell whether you are in a dark or light environment just by looking at an image. In that case, you may have to go beyond the image and query the camera. In theory, you can do this either directly through a driver (unlikely) or perhaps by looking at the image metadata (e.g. with JPEG, there's EXIF).
Lastly, you haven't said what it is exactly you want to know the brightness of. If it's the overall scene, then average will be good enough. If it's some part of the scene, you may have to do something a bit smarter. Let us know if that's the case.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With