9

I need the average color value of an image in objective c. I want to create a color gradient of it. Has anyone an idea?

Simon
  • 91
  • 1
  • 2
  • 1
    See [this answer](http://stackoverflow.com/questions/12147779/how-do-i-release-a-cgimageref-in-ios/12148136#12148136) for an optimized approach. It's about four times faster. – Nikolai Ruhe Aug 28 '12 at 11:55

2 Answers2

15

here is an experimental code that i have not tested yet.

struct pixel {
    unsigned char r, g, b, a;
};

- (UIColor*) getDominantColor:(UIImage*)image
{
    NSUInteger red = 0;
    NSUInteger green = 0;
    NSUInteger blue = 0;


    // Allocate a buffer big enough to hold all the pixels

    struct pixel* pixels = (struct pixel*) calloc(1, image.size.width * image.size.height * sizeof(struct pixel));
    if (pixels != nil)
    {

        CGContextRef context = CGBitmapContextCreate(
                                                 (void*) pixels,
                                                 image.size.width,
                                                 image.size.height,
                                                 8,
                                                 image.size.width * 4,
                                                 CGImageGetColorSpace(image.CGImage),
                                                 kCGImageAlphaPremultipliedLast
                                                 );

        if (context != NULL)
        {
            // Draw the image in the bitmap

            CGContextDrawImage(context, CGRectMake(0.0f, 0.0f, image.size.width, image.size.height), image.CGImage);

            // Now that we have the image drawn in our own buffer, we can loop over the pixels to
            // process it. This simple case simply counts all pixels that have a pure red component.

            // There are probably more efficient and interesting ways to do this. But the important
            // part is that the pixels buffer can be read directly.

            NSUInteger numberOfPixels = image.size.width * image.size.height;
            for (int i=0; i<numberOfPixels; i++) {
                red += pixels[i].r;
                green += pixels[i].g;
                blue += pixels[i].b;
            }


            red /= numberOfPixels;
            green /= numberOfPixels;
            blue/= numberOfPixels;


            CGContextRelease(context);
        }

        free(pixels);
    }
    return [UIColor colorWithRed:red/255.0f green:green/255.0f blue:blue/255.0f alpha:1.0f];
}

You can use this method eg;

-(void)doSomething
{
    UIImage *image = [UIImage imageNamed:@"someImage.png"];
    UIColor *dominantColor = [self getDominantColor:image];
}

I hope this will work for you.

Also you can implement in UIImage with category. Better way to write some utilities for objects :)

Edit : Fixed the bug in while().

Alkimake
  • 1,797
  • 14
  • 30
  • Thank you, it works great with a lighted image , but with a darker one, i got only a black color. Like this one: http://alles-iphone.de/CONTENT/content-pre1/72207-1.jpg. The values pixels->r,pixels->g and pixels->b get never higher than "20". – Simon Apr 07 '11 at 08:04
  • 3
    See [this answer](http://stackoverflow.com/questions/12147779/how-do-i-release-a-cgimageref-in-ios/12148136#12148136) for an optimized approach. It's about four times faster. – Nikolai Ruhe Aug 28 '12 at 11:56
  • but i need number of dominant colors could you help me – dineshprasanna Sep 10 '12 at 07:54
  • What do you mean by number of dominant colors ? – Alkimake Sep 21 '12 at 12:31
  • Average color != dominant color. The average color can be non-existant in an image. The dominant color not (the perceptually dominant color might though). – Regexident Sep 22 '12 at 23:44
  • even I need the list of dominant colors. Like in an image we have so many colors. For example we have stackoverflow's logo. There we have four main dominant colors - gray, orange, yellow, white...that way... – MPG Feb 18 '13 at 11:26
-1

There is a method to create the average color from Image.

[UIColor colorWithAverageColorFromImage:(UIImage *)image];
Surjeet Rajput
  • 1,251
  • 17
  • 24