Adam Popławski
Adam Popławski

Reputation: 355

Get luminance from iOS camera

I want get luminance from camera, I've checked solution (count average luminance) obtaining-luminosity-from-an-ios-camera

but camera automatically set exposure, so if it turn camera to the source of light (e.g. bulb) then in first time luminance is very high, but after a while this value is lowe, because exposure settings was changed. I've tested with locking exposure but this is not good solution, because if exposure was locked when image from camera is dark, then little source of light is counted as very high value. Is any way to get absolute value of luminance ? I've checked application Light detector and this application works well, exposure is changed, but value of luminance is stable.

Regards Adam

Upvotes: 0

Views: 1541

Answers (1)

James Bush
James Bush

Reputation: 1527

Do you know how to create a custom Core Image filter that returns the output of a CIKernel (or CIColorKernel) object? If not, you should; and, I'd be happy to provide you with easy-to-understand instructions for doing that.

Assuming you do, here's the OpenGL ES code that will return only the luminance values of an image it processes:

vec4 rgb2hsl(vec4 color)
{
    //Compute min and max component values
    float MAX = max(color.r, max(color.g, color.b));
    float MIN = min(color.r, min(color.g, color.b));

    //Make sure MAX > MIN to avoid division by zero later
    MAX = max(MIN + 1e-6, MAX);

    //Compute luminosity
    float l = (MIN + MAX) / 2.0;

    //Compute saturation
    float s = (l < 0.5 ? (MAX - MIN) / (MIN + MAX) : (MAX - MIN) / (2.0 - MAX - MIN));

    //Compute hue
    float h = (MAX == color.r ? (color.g - color.b) / (MAX - MIN) : (MAX == color.g ? 2.0 + (color.b - color.r) / (MAX - MIN) : 4.0 + (color.r - color.g) / (MAX - MIN)));
    h /= 6.0;
    h = (h < 0.0 ? 1.0 + h : h);

    return vec4(h, s, l, color.a);
}

kernel vec4 hsl(sampler image)
{
    //Get pixel from image (assume its alpha is 1.0 and don't unpremultiply)
    vec4 pixel = unpremultiply(sample(image, samplerCoord(image)));

    //Convert to HSL; only display luminance value
    return premultiply(vec4(vec3(rgb2hsl(pixel).b), 1.0));
}

The above is OpenGL ES code written originally by Apple developers; I modified it to display only the luminance values.

Upvotes: 2

Related Questions