3

I know how to get the RGB values of individual pixels of a bitmap. How can I get the average RGB value for all of the pixels of a bitmap?

3
  • Klaus66 i have one bitmap and i would like to get RGB value from that bitmap. ImageView imageView = ((ImageView)v); Bitmap bitmap = ((BitmapDrawable)imageView.getDrawable()).getBitmap(); int pixel = bitmap.getPixel(x,y); int redValue = Color.red(pixel); int blueValue = Color.blue(pixel); int greenValue = Color.green(pixel); for here get RGB value for specified pixel, but i need to get hole, bitmap.Red=?, bitmap.Green=?, bitmap.Blue=?. Commented Dec 24, 2013 at 11:37
  • A bitmap is composed of pixels. It doesn't have its own R, G, B or Alpha values! That's why I don't understand the meaning of this question. Maybe, you want the AVERAGE of all the reds, greens and blues from all the pixels? Commented Dec 24, 2013 at 11:45
  • Like that, Average of RGB values. Commented Dec 26, 2013 at 6:22

3 Answers 3

11

I think below code for exact answer to you. Get the Average(Number of pixels)of Red, Green and Blue value for the given bitmap.

Bitmap bitmap = someBitmap; //assign your bitmap here
int redColors = 0;
int greenColors = 0;
int blueColors = 0;
int pixelCount = 0;

for (int y = 0; y < bitmap.getHeight(); y++)
{
    for (int x = 0; x < bitmap.getWidth(); x++)
    {
        int c = bitmap.getPixel(x, y);
        pixelCount++;
        redColors += Color.red(c);
        greenColors += Color.green(c);
        blueColors += Color.blue(c);
    }
}
// calculate average of bitmap r,g,b values
int red = (redColors/pixelCount);
int green = (greenColors/pixelCount);
int blue = (blueColors/pixelCount);
Sign up to request clarification or add additional context in comments.

2 Comments

For very large images, you might need to use long for redColors and friends to avoid integer overload.
yes correct Kevin, should be avoid int, while using very large image.
2

The answer from john sakthi does not work correctly if the Bitmap has transparency (PNGs). I modified the answer for correctly getting the red/green/blue averages while accounting for transparent pixels:

/**
 * Calculate the average red, green, blue color values of a bitmap
 *
 * @param bitmap
 *            a {@link Bitmap}
 * @return
 */
public static int[] getAverageColorRGB(Bitmap bitmap) {
    final int width = bitmap.getWidth();
    final int height = bitmap.getHeight();
    int size = width * height;
    int pixelColor;
    int r, g, b;
    r = g = b = 0;
    for (int x = 0; x < width; ++x) {
        for (int y = 0; y < height; ++y) {
            pixelColor = bitmap.getPixel(x, y);
            if (pixelColor == 0) {
                size--;
                continue;
            }
            r += Color.red(pixelColor);
            g += Color.green(pixelColor);
            b += Color.blue(pixelColor);
        }
    }
    r /= size;
    g /= size;
    b /= size;
    return new int[] {
            r, g, b
    };
}

Comments

1

you can use this method for this purpose: Bitmap.createBitmap

For instance:

int[] colors = new int[yourWidth * yourHeight];
Arrays.fill(colors, Color.Black);
Bitmap bitamp = Bitamp.createBitmap(colors, yourWidth, yourHeight, Bitmap.Config.ARGB_8888);

Check for typo

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.