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=?.jan– jan2013-12-24 11:37:07 +00:00Commented 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?Phantômaxx– Phantômaxx2013-12-24 11:45:50 +00:00Commented Dec 24, 2013 at 11:45
-
Like that, Average of RGB values.jan– jan2013-12-26 06:22:42 +00:00Commented Dec 26, 2013 at 6:22
Add a comment
|
3 Answers
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);
2 Comments
Kevin
For very large images, you might need to use
long for redColors and friends to avoid integer overload.Sakthivel Appavu
yes correct Kevin, should be avoid int, while using very large image.
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
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