如何检测Android中的黑暗照片

use*_*943 6 java android image image-processing android-studio

我有一个Android应用程序,用户用前置摄像头拍摄自己的照片,然后将照片上传到我的服务器.我注意到许多照片来到我的服务器太暗(有时几乎不可能看到用户脸上的清晰).

我想在应用程序端过滤掉这些照片并显示通知(例如"照片太暗.再拍一张照片")给用户.我怎样才能在Android中完成这样的任务?

编辑:

我已经找到了如何计算单个像素的亮度(感谢这个答案: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;
}
Run Code Online (Sandbox Code Playgroud)

但我仍然不清楚如何确定整个图像亮度"状态".有什么建议?

Yev*_*ets 10

作为变体,您可以构建照片的亮度直方图.如此处所述计算亮度公式以确定RGB颜色的亮度.然后初始化大小为256的数组并将一个数组元素递增一个数组,该索引是每个像素的亮度.

然后查看左侧或右侧的值是否太多,这意味着您的图片太亮/太暗.例如,您可以查看10个左右值.

代码示例:

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.
Run Code Online (Sandbox Code Playgroud)