Hello Everyone!

I am wondering about the thory behind the box blur. I have an assignment, and I wrote a program implementing the box blur the way it was described in my assignment which is "When centered on a pixel, its values are multiplied with each corresponding surrounding pixel values, and then divided by the sum of the matrix values" and then it gave a 3 by 3 matrix of all ones.

The code I have works with the r, g and b values separately for an image for both the sum and the product portion of the formula, which I interpreted as (product of one color channel in every element in the matrix)/ (sum of those color channels). This does execute, however an all black image is created. I am wondering where I am going wrong. I did some sample calculations using one color stream just to see it if works, but it is way off. for example (100100160*50) / (100+100+160+50) = 80000000/ 410, which is definitely greater than the 255 max for the color value. If I could get some clarification then it would be much appreciated.

Thank you for your time and help

Hi there, r,g,b should be values 0 < n < 255.
If you have 255 straight for all 3, you get black! Which is doing what it is suppose to do, but not what your expecting.
Could you just keep the r,g,b values and focus on propagating neighbouring pixels?

You don't multiply the color values together! You multiply the color values by the corresponding values in the 3x3 matrix, then divide by the sum of the matrix.
With a matrix of all 1's that's particularly trivial - you are multiplying the values by 1 (!), adding the results, and dividing by 9
Suppose we have pixel value 100 (just one color to keep it simple).
Look at the surrounding 3x3 matrix. Maybe it will look like
91 92 93
94 100 95
96 97 98
so you take each of those 9 numbers. Multiply each by 1 (trivial!), add them together, divide by 9. That's the new value of the center pixel (95.1).

WIth a matrix of all 1's it's simply the average of the 9 pixels.
It may look complicated for such a simple result, but's it's the most trivial example of a much more general way of blurring in which the matrix may be bigger than 3x3 and may contain values other than 1. But one thing at at time...

@JamesCherrill Thanks! I have managed to get the blur working, however my image moved a little down and to the left whenever I use the blur filter code.

This is the code I am using:
where *pixel Im is an image, and tmpBuffer is a temporary buffer to apply the blur.

void MyFilter(pixel* Im, int myIm_Width, int myIm_Height){
    int x, y;
    global.tmpBuffer = copyPic(global.data, global.w, global.h);
    for (x = 0; x < myIm_Width; x++)
        for (y = 0; y < myIm_Height; y++){
            int loc = x + y*myIm_Width;

            int blurX, blurY;
            int blurRad = 5;
            int totPix = 0;
            int blurSum[] = { 0, 0, 0 }; // {r, g, b} values for blur
            //gets the pixels in the matrix
            for (blurX = -((int)blurRad / 2); blurX < (int)blurRad / 2; blurX++){
                for (blurY = -((int)blurRad / 2); blurY < (int)blurRad / 2; blurY++){
                    if (!(blurX < 0 || blurX > global.w || blurY < 0 || blurY > global.h)){
                        blurSum[0] += global.data[loc + (blurX * global.w) + blurY].r;
                        blurSum[1] += global.data[loc + (blurX * global.w) + blurY].g;
                        blurSum[2] += global.data[loc + (blurX * global.w   ) + blurY].b;
            global.tmpBuffer[loc].r = blurSum[0] / totPix;
            global.tmpBuffer[loc].g = blurSum[1] / totPix;
            global.tmpBuffer[loc].b = blurSum[2] / totPix;
    global.data = copyPic(global.tmpBuffer, global.w, global.h); // copies the picture to global.data (the picture being displayed)

the struct being used for the image looks like this

typedef struct {
    pixel *data; // Modified Image Buffer
    pixel *oImage; // Original Image
    pixel *tmpBuffer; // The Temporary Buffer
    int w, h;
} glob;

I am wondering if you can see why my blur seems to be moving rather than staying in the same spot.

Thank you for your help!

This article has been dead for over six months. Start a new discussion instead.