image intensity identifier

When I was developing the client of Shiyiquan,I met a problem that need to use an algorithm to identify wether the picture is mainly light color.Today let’s talk about something about that.
To identify the intensity,we need firstly grey the image.In Android,the information of an image can be sorted in a bitmap object.We can use getPixel(int x,int y) to get each color in each pixels.

Then we need to get the RGB info of each pixels:

int rgb = bi.getPixel(i, j);
int r = (rgb & 0xff0000) >> 16;
int g = (rgb & 0xff00) >> 8;
int b = (rgb & 0xff);

Then,we use those three values to calculate the grey value of certain pixel:

int gray = (int) (r * 0.3 + g * 0.59 + b * 0.11);
sgray[gray]++;

What does sgray[] do?It is used to count times that each gray value appears.Then we use Gaussian Distribution to figure out the distribution of those gray value.

In several experiments,if the distribution value is greater than approximately 420.35,it is deep color,or it is a light color image.

The Full Method is:

public boolean isDeepColor(Bitmap bi) {
        int sgray[] = new int[256];
        for (int i = 0; i < 256; i++) {
            sgray[i] = 0;
        }
        double sum = 0;
        int width = bi.getWidth();
        int height = bi.getHeight();
        for (int i = 0; i < width; i++) {
            for (int j = 0; j < height; j++) {
                int rgb = bi.getPixel(i, j);
                int r = (rgb & 0xff0000) >> 16;
                int g = (rgb & 0xff00) >> 8;
                int b = (rgb & 0xff);
                int gray = (int) (r * 0.3 + g * 0.59 + b * 0.11);
                sgray[gray]++;
            }
        }
        for (int i = 0; i < 256; i++) {
            if (sgray[i] != 0) {
                double p = sgray[i] * 1.0 / (width * height);
                sum += p * (Math.log(1 / p) / Math.log(2));
            }
        }
        sum *= 100;

        return sum >= 420;
    }