Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get the average colour of an image

Tags:

java

android

I want to be able to take an image and find out what is the average colour. meaning if the image is half black half white, I would get something in between... some shade of gray. It could be the most frequent single colour or median. Any average will do.

How can I do this in android.

like image 614
code511788465541441 Avatar asked Sep 13 '12 14:09

code511788465541441


People also ask

What is the average colour?

The team determined that the average color of the universe is a beige shade not too far off from white.

How do you find the average color in Photoshop?

5) Finding the Average ColorFilter > Blur > Average finds the average of all of the colors in an image (or in a selection) and fills the entire image (or selection) with that color.

How do you find the average color of an image in Python?

Use the average() Function of NumPy to Find the Average Color of Images in Python. In mathematics, we can find the average of a vector by dividing the sum of all the elements in the vector by the total number of elements.

How do you calculate average hue?

The average hue is calculated using mean of circular quantities. The standard way of computing the average cannot be used because hue is a periodic quantity (modulo 360). The ˉhr is then converted to degress using 180πˉhr(mod360). Both ˉhr and ‖ˉh‖ are reported.


Video Answer


3 Answers

Bitmap bitmap = someFunctionReturningABitmap();
long redBucket = 0;
long greenBucket = 0;
long blueBucket = 0;
long pixelCount = 0;

for (int y = 0; y < bitmap.getHeight(); y++)
{
    for (int x = 0; x < bitmap.getWidth(); x++)
    {
        Color c = bitmap.getPixel(x, y);

        pixelCount++;
        redBucket += Color.red(c);
        greenBucket += Color.green(c);
        blueBucket += Color.blue(c);
        // does alpha matter?
    }
}

Color averageColor = Color.rgb(redBucket / pixelCount,
                                greenBucket / pixelCount,
                                blueBucket / pixelCount);
like image 145
Dan O Avatar answered Oct 01 '22 23:10

Dan O


I think you will have to do that yourself.

Just create an int array with all the colors:

Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.icon);  
bmp = bmp.copy(Bitmap.Config.ARGB_8888, true);    
int intArray[] = new int[bmp.getWidth()*bmp.getHeight()];  
bmp.getPixels(intArray, 0, bmp.getWidth(), 0, 0, bmp.getWidth(), bmp.getHeight());

Then you can get the color with intArray[0], the value could be 0xFFFF0000 for red (last 6 numbers are the RGB color value).

Here is another easy solution:

  • Get you full-size image in a bitmap.

  • Create a scaled bitmap of 1*1px.

  • Get this bitmap color.

like image 30
Stephane Mathis Avatar answered Oct 01 '22 23:10

Stephane Mathis


Building off Dan O's solution, here's a method that automatically takes into account the alpha channel and makes the optimization/ tradeoff of getPixels vs getPixel.

The cost is memory but the benefit is performance, invocation of a virtual method in a loop that could possibly be run several million times [i.e. an 8MP image has 3,456x2,304 = 7,962,624 pixels]). I've even taken things one step further by removing the looped android.graphics.Color method calls.

public static int getDominantColor(Bitmap bitmap) {
   if (null == bitmap) return Color.TRANSPARENT;

   int redBucket = 0;
   int greenBucket = 0;
   int blueBucket = 0;
   int alphaBucket = 0;

   boolean hasAlpha = bitmap.hasAlpha();
   int pixelCount = bitmap.getWidth() * bitmap.getHeight();
   int[] pixels = new int[pixelCount];
   bitmap.getPixels(pixels, 0, bitmap.getWidth(), 0, 0, bitmap.getWidth(), bitmap.getHeight());

   for (int y = 0, h = bitmap.getHeight(); y < h; y++)
   {
       for (int x = 0, w = bitmap.getWidth(); x < w; x++)
       {
           int color = pixels[x + y * w]; // x + y * width
           redBucket += (color >> 16) & 0xFF; // Color.red
           greenBucket += (color >> 8) & 0xFF; // Color.greed
           blueBucket += (color & 0xFF); // Color.blue
           if (hasAlpha) alphaBucket += (color >>> 24); // Color.alpha
       }
   }

   return Color.argb(
           (hasAlpha) ? (alphaBucket / pixelCount) : 255,
           redBucket / pixelCount,
           greenBucket / pixelCount,
           blueBucket / pixelCount);
}
like image 36
Tom Avatar answered Oct 01 '22 21:10

Tom