Determining Image Luminance/Brightness

krafter picture krafter · Feb 2, 2011 · Viewed 11.7k times · Source

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.

Answer

mpenkov picture mpenkov · Feb 2, 2011

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.