Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to detect dark photos in Android [closed]

I have an Android app where user takes a photo of himself with the front camera and then the photo is being uploaded to my server. I notice that many photos comes to my server too dark (sometimes almost impossible to cleary see the user face).

I would like to filter out such photos and show notification (eg. "Photo is too dark. Take one more picture") to the user in the app side. How I could accomplish such task in Android?

EDIT:

I have found out how to calculate brightness for one single pixel (thank's to this answer: https://stackoverflow.com/a/16313099/2999943):

private boolean isPixelColorBright(int color) {
    if (android.R.color.transparent == color)
        return true;

    boolean rtnValue = false;

    int[] rgb = {Color.red(color), Color.green(color), Color.blue(color)};

    int brightness = (int) Math.sqrt(rgb[0] * rgb[0] * .299 + rgb[1]
            * rgb[1] * .587 + rgb[2] * rgb[2] * .114);

    if (brightness >= 200) {    // light color
        rtnValue = true;
    }

    return rtnValue;
}

But still I don't have clear idea how to determine whole image brightness "status". Any suggestions?

like image 304
user2999943 Avatar asked Mar 10 '16 10:03

user2999943


People also ask

How to fix a photo that is too dark?

1. Brightness/Contrast. When you need to brighten a photo the most obvious place to start is to go to Image > Adjustments > Brightness/Contrast, or to select this tool on an Adjustment Layer. Brightness/Contrast is a good, simple option to use if the overall image is too dark.

How to brighten Photos without overexposing?

Fortunately programs like Photoshop and Photoshop Elements have tools which allow you to avoid over exposing those light areas while still brightening those areas which are too dark. In both programs you have the “Image > Adjustment > Shadows / Highlights” option.


1 Answers

As a variant you can build a brightness histogram of your photo. Calculate brightness as described here Formula to determine brightness of RGB color. Then initialize array of size 256 and increment by one an array element which index is a brightness of every pixel.

Then look if too much values are on the left side or right one, it means that your picture is too light/dark. For example you can look at 10 right and left values.

Code example:

int histogram[256];
for (int i=0;i<256;i++) {
     histogram[i] = 0;
}

for (int x = 0; x < a.getWidth(); x++) {
    for(int y = 0; y < a.getHeight(); y++) {
        int color = a.getRGB(x, y);

        int r = Color.red(pixel);
        int g = Color.green(pixel);
        int b = Color.blue(pixel);

        int brightness = (int) (0.2126*r + 0.7152*g + 0.0722*b);
        histogram[brightness]++;
    }
}

int allPixelsCount = a.getWidth() * a.getHeight();

// Count pixels with brightness less then 10
int darkPixelCount = 0;
for (int i=0;i<10;i++) {
    darkPixelCount += histogram[i];
}

if (darkPixelCount > allPixelCount * 0.25) // Dark picture. Play with a percentage
else // Light picture.
like image 136
Yev Kanivets Avatar answered Oct 18 '22 04:10

Yev Kanivets