Determining Image Luminance/Brightness
Asked Answered
F

1

11

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.

Fogdog answered 2/2, 2011 at 15:30 Comment(0)
P
16

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.

Pendergrass answered 2/2, 2011 at 15:45 Comment(8)
Thanks, I guess that has to work. I'll give it a try and post the result here.Fogdog
Have a look at my edited comment -- I've identified a potential problem and some ways you could solve it.Pendergrass
Yes, this actually works: getting pixel information splitting it into it's color intensities then calculating Y as you showed here. Since I need the average brightness value of small (fast) light pulsations before camera can adjust to it, this method works good. Thanks again.Fogdog
@misha : could you please tell me the right way to compute the average of the luma channel? I am stuck here for a long time.Decumbent
Extract the luma channel, sum all pixels of the extracted luma channel, divide the sum by the total number of pixels. What is the problem?Pendergrass
@misha well, the code is the problem. I am getting confused. How do I extract the luma channel? And how to get the pixel values, instead of accesing int, long int, uchar etc. Am bit confused here. Please help me.Decumbent
@misha Where does 0.299, 0.587, and 0.144 come from exactly?Pregnancy
@Pregnancy They are coefficients from the RGB-YUV conversion formula. They vary slightly depending on what exactly you're doing.Pendergrass

© 2022 - 2024 — McMap. All rights reserved.