iosobjective-ccolorsgaussiancolor-blending

Gaussian blend of image


I'm writing an iPhone app and need help in figuring out how to take an image and blend it into a single color. I assume I need to do a gaussian blend but am not sure if this is correct or how to do it if it is.

Do you have any suggestions, pointers to sample gaussian blend code snippets, or am I heading in the wrong directions to get from image to blended color image?

It doesn't appear I can do this with existing iPhone frameworks or are there private methods in public frameworks that will make this job easier?


Solution

  • So you want to take an image and "blend it into a single color". Are you trying to get the 'average color' for the image?

    If so, using a Gaussian filter is perhaps overcomplicating it, since the output you require is simply a single RGB value. The easiest way to do this is to compute the average for each color channel (red, green and blue):

    int r,g,b;
    r=g=b=0;
    
    for (y=0 ; y<image_height ; y++)
        for (x=0 ; x<image_width ; x++)
        {
            r = r + image[y,x,0];
            g = g + image[y,x,1];
            b = b + image[y,x,2];
        }
    
    num_pixels = image_height * image_width;
    average_r = r / num_pixels;
    average_g = g / num_pixels;
    average_b = b / num_pixels;
    

    A Gaussian filter is a center-weighted filter, meaning that the pixel in the center of the filtering window is weighted more heavily than the others. If you want to blur an image, then this is appropriate, but for blending an entire image, equally weighting all pixels, as in the pseudo-code above, is just as effective.