'Blurring an image with C

I am trying to blur an image based on the tutorial I am following. Here's the instruction.

Blur There are a number of ways to create the effect of blurring or softening an image. For this problem, we’ll use the “box blur,” which works by taking each pixel and, for each color value, giving it a new value by averaging the color values of neighboring pixels.

Consider the following grid of pixels, where we’ve numbered each pixel.

a grid of pixels

The new value of each pixel would be the average of the values of all of the pixels that are within 1 row and column of the original pixel (forming a 3x3 box). For example, each of the color values for pixel 6 would be obtained by averaging the original color values of pixels 1, 2, 3, 5, 6, 7, 9, 10, and 11 (note that pixel 6 itself is included in the average). Likewise, the color values for pixel 11 would be be obtained by averaging the color values of pixels 6, 7, 8, 10, 11, 12, 14, 15 and 16.

enter image description here

For a pixel along the edge or corner, like pixel 15, we would still look for all pixels within 1 row and column: in this case, pixels 10, 11, 12, 14, 15, and 16.```

So to tackle this, I've come up with this solution.

for each row
  for each column
     add the pixel if upper left exists
     add the pixel if directly above exists
     add the pixel if upper right exists
     add the pixel if right exists
     add the pixel if directly below exists
     add the pixel if left exists
divide by times added

and I have typed the code -

void blur(int height, int width, RGBTRIPLE image[height][width])
{
    int red;
    int green;
    int blue;
    int counter = 0;

    for (int i = 0; i < height; i++)
    {
        for (int j = 0; j < width; j++)
        {
            if (i + 1 && j - 1)
            {
                red = image[i][j].rgbtRed + image[i + 1][j - 1].rgbtRed;
                green = image[i][j].rgbtGreen + image[i + 1][j - 1].rgbtGreen;
                blue = image[i][j].rgbtBlue + image[i + 1][j - 1].rgbtBlue;
                counter++;
            }
            if (j + 1)
            {
                red = image[i][j].rgbtRed + image[i][j + 1].rgbtRed;
                green = image[i][j].rgbtGreen + image[i][j + 1].rgbtGreen;
                blue = image[i][j].rgbtBlue + image[i][j + 1].rgbtBlue;
                counter++;
            }
            if (i + 1 && j + 1)
            {
                red = image[i][j].rgbtRed + image[i + 1][j + 1].rgbtRed;
                green = image[i][j].rgbtGreen + image[i + 1][j + 1].rgbtGreen;
                blue = image[i][j].rgbtBlue + image[i + 1][j + 1].rgbtBlue;
                counter++;
            }
            if (i + 1)
            {
                red = image[i][j].rgbtRed + image[i + 1][j].rgbtRed;
                green = image[i][j].rgbtGreen + image[i + 1][j].rgbtGreen;
                blue = image[i][j].rgbtBlue + image[i + 1][j].rgbtBlue;
                counter++;
            }
            if (j - 1)
            {
                red = image[i][j].rgbtRed + image[i][j - 1].rgbtRed;
                green = image[i][j].rgbtGreen + image[i][j - 1].rgbtGreen;
                blue = image[i][j].rgbtBlue + image[i][j - 1].rgbtBlue;
                counter++;
            }
            if (i - 1)
            {
                red = image[i][j].rgbtRed + image[i - 1][j].rgbtRed;
                green = image[i][j].rgbtGreen + image[i - 1][j].rgbtGreen;
                blue = image[i][j].rgbtBlue + image[i - 1][j].rgbtBlue;
                counter++;
            }
            image[i][j].rgbtRed = red/counter;
            image[i][j].rgbtGreen = green/counter;
            image[i][j].rgbtBlue = blue/counter;
        }
    }
    return;
}

and it seems like I am getting errors saying

~/pset4/filter/ $ ./filter -b images/courtyard.bmp  test.bmp                                                                                                                                                        
helpers.c:84:45: runtime error: index -1 out of bounds for type 'RGBTRIPLE [width]'
helpers.c:85:49: runtime error: index -1 out of bounds for type 'RGBTRIPLE [width]'
helpers.c:86:47: runtime error: index -1 out of bounds for type 'RGBTRIPLE [width]'
helpers.c:112:45: runtime error: index -1 out of bounds for type 'RGBTRIPLE [width]'
helpers.c:113:49: runtime error: index -1 out of bounds for type 'RGBTRIPLE [width]'
helpers.c:114:47: runtime error: index -1 out of bounds for type 'RGBTRIPLE [width]'
UndefinedBehaviorSanitizer:DEADLYSIGNAL
==4657==ERROR: UndefinedBehaviorSanitizer: SEGV on unknown address 0x7fd1249a490a (pc 0x00000042b7ea bp 0x7ffd589ca4f0 sp 0x7ffd589c90f0 T4657)
==4657==The signal is caused by a READ memory access.
    #0 0x42b7e9  (/home/ubuntu/pset4/filter/filter+0x42b7e9)
    #1 0x422ed2  (/home/ubuntu/pset4/filter/filter+0x422ed2)
    #2 0x7fd123897b96  (/lib/x86_64-linux-gnu/libc.so.6+0x21b96)
    #3 0x402d69  (/home/ubuntu/pset4/filter/filter+0x402d69)

for me it seems like the code isn't really solving the problem. It fails right out of the first iteration. because i and j value becomes negative, thus not really serving the intention of checking the nearby pixels' existence.

Could you any share a point or two about what should be done here to better find out how should the program detect the surrounding pixels?

Thanks!



Solution 1:[1]

In C, non-zero considered as true, j-1 at j=0 is -1 true.

void blur(int height, int width, RGBTRIPLE image[height][width])

{

 int red;

int green;
int blue;
int counter = 0;
RGBTRIPLE **new_image = (RGBTRIPLE**) malloc(sizeof(RGBTRIPLE*)*height);

  for(int i=0;i<height;i++)
     new_image[i]=((RGBTRIPLE*) malloc(sizeof(RGBTRIPLE)*width);

for (int i = 0; i < height; i++)
{
    for (int j = 0; j < width; j++)
    {
          red=green=blue=0;
          counter=0;
        if (i + 1 <height && j - 1 >=0)
        {
            red += image[i][j].rgbtRed + image[i + 1][j - 1].rgbtRed;
            green += image[i][j].rgbtGreen + image[i + 1][j - 1].rgbtGreen;
            blue += image[i][j].rgbtBlue + image[i + 1][j - 1].rgbtBlue;
            counter++;
        }
        if (j + 1<width)
        {
            red += image[i][j].rgbtRed + image[i][j + 1].rgbtRed;
            green += image[i][j].rgbtGreen + image[i][j + 1].rgbtGreen;
            blue += image[i][j].rgbtBlue + image[i][j + 1].rgbtBlue;
            counter++;
        }
        if (i + 1 <height && j + 1 <width)
        {
            red += image[i][j].rgbtRed + image[i + 1][j + 1].rgbtRed;
            green += image[i][j].rgbtGreen + image[i + 1][j + 1].rgbtGreen;
            blue += image[i][j].rgbtBlue + image[i + 1][j + 1].rgbtBlue;
            counter++;
        }
        if (i + 1<height)
        {
            red += image[i][j].rgbtRed + image[i + 1][j].rgbtRed;
            green += image[i][j].rgbtGreen + image[i + 1][j].rgbtGreen;
            blue += image[i][j].rgbtBlue + image[i + 1][j].rgbtBlue;
            counter++;
        }
        if (j - 1>=0)
        {
            red += image[i][j].rgbtRed + image[i][j - 1].rgbtRed;
            green += image[i][j].rgbtGreen + image[i][j - 1].rgbtGreen;
            blue += image[i][j].rgbtBlue + image[i][j - 1].rgbtBlue;
            counter++;
        }
        if (i - 1>=0)
        {
            red += image[i][j].rgbtRed + image[i - 1][j].rgbtRed;
            green += image[i][j].rgbtGreen + image[i - 1][j].rgbtGreen;
            blue += image[i][j].rgbtBlue + image[i - 1][j].rgbtBlue;
            counter++;
        }
        new_image[i][j].rgbtRed = red/counter;
        new_image[i][j].rgbtGreen = green/counter;
        new_image[i][j].rgbtBlue = blue/counter;
    }
}
return;
}

Solution 2:[2]

This is a better approach, with the possibility of increasing the blur box to others dimensions forming a 3x3 box, or others.

void blur(int height, int width, RGBTRIPLE image[height][width])
{
    double average_red = 0, average_green = 0, average_blue = 0;
    int count_number = 0;
int box = 1; // set 1 to have a 3x3 box

//scanning the matriz
for (int i = 0; i<height; ++i)
{
    for(int j = 0; j<width;++j)
    {
        //cleaning the averages and the count number

        average_red = 0;
        average_blue = 0;
        average_red = 0;
        count_number = 0;

        //forming a 3x3 box
        for (int li = i-box; li<=i+box; ++li )
        {
            if( li < 0 )
            {
                li = 0;
            }
            if( li == height)
            {
                break;
            }

            for(int col = j-box; col<= j+box; col++)
            {

                if (col == width ) break;
                if (col < 0)
                {
                    col = 0;
                }

                count_number++;

                average_red += image[li][col].rgbtRed;
                average_green += image[li][col].rgbtGreen;
                average_blue += image[li][col].rgbtBlue;

            }

        }
            //set the new average to the pixel
        image[i][j].rgbtRed = (int)round(average_red/count_number) ;
        image[i][j].rgbtGreen = (int)round(average_green/count_number);
        image[i][j].rgbtBlue = (int)round( average_blue/count_number );
    }
}
return;

}

Solution 3:[3]

Okay, I am high jacking the thread.. unfortunately the solution provided did not work. the correct solution should be this.

    // Blur image
void blur(int height, int width, RGBTRIPLE image[height][width])
{
    RGBTRIPLE newImage[height][width];
    for (int i = 0; i < height; i++)
    {
        for (int j = 0; j < width; j++)
        {
            newImage[i][j] = image[i][j];
        }
    }

    for (int i = 0, red, green, blue, counter; i < height; i++)
    {
        for (int j = 0; j < width; j++)
        {
            red = green = blue = counter = 0;
            // add the center pixel
            if (i >= 0 && j >= 0)
            {
                red += newImage[i][j].rgbtRed;
                green += newImage[i][j].rgbtGreen;
                blue += newImage[i][j].rgbtBlue;
                counter++;
            }
            // add below pixel
            if (i >= 0 && j - 1 >= 0)
            {
                red += newImage[i][j-1].rgbtRed;
                green += newImage[i][j-1].rgbtGreen;
                blue += newImage[i][j-1].rgbtBlue;
                counter++;
            }
            // add right pixel
            if ((i >= 0 && j + 1 >= 0) && (i >= 0 && j + 1 < width))
            {
                red += newImage[i][j+1].rgbtRed;
                green += newImage[i][j+1].rgbtGreen;
                blue += newImage[i][j+1].rgbtBlue;
                counter++;
            }
            // add left pixel
            if (i - 1 >= 0 && j >= 0)
            {
                red += newImage[i-1][j].rgbtRed;
                green += newImage[i-1][j].rgbtGreen;
                blue += newImage[i-1][j].rgbtBlue;
                counter++;
            }
            // add left below pixel
            if (i - 1 >= 0 && j - 1 >= 0)
            {
                red += newImage[i-1][j-1].rgbtRed;
                green += newImage[i-1][j-1].rgbtGreen;
                blue += newImage[i-1][j-1].rgbtBlue;
                counter++;
            }
            // add left upper pixel
            if ((i - 1 >= 0 && j + 1 >= 0) && (i - 1 >= 0 && j + 1 < width))
            {
                red += newImage[i-1][j+1].rgbtRed;
                green += newImage[i-1][j+1].rgbtGreen;
                blue += newImage[i-1][j+1].rgbtBlue;
                counter++;
            }
            // add upper pixel
            if ((i + 1 >= 0 && j >= 0) && (i + 1 < height && j >= 0))
            {
                red += newImage[i+1][j].rgbtRed;
                green += newImage[i+1][j].rgbtGreen;
                blue += newImage[i+1][j].rgbtBlue;
                counter++;
            }
            // add below right pixel
            if ((i + 1 >= 0 && j - 1 >= 0) && (i + 1 < height && j - 1 >= 0))
            {
                red += newImage[i+1][j-1].rgbtRed;
                green += newImage[i+1][j-1].rgbtGreen;
                blue += newImage[i+1][j-1].rgbtBlue;
                counter++;
            }
            // add upper right pixel
            if ((i + 1 >= 0 && j + 1 >= 0) && (i + 1 < height && j + 1 < width))
            {
                red += newImage[i+1][j+1].rgbtRed;
                green += newImage[i+1][j+1].rgbtGreen;
                blue += newImage[i+1][j+1].rgbtBlue;
                counter++;
            }

            image[i][j].rgbtRed = round(red / (counter * 1.0));
            image[i][j].rgbtGreen = round(green / (counter * 1.0));
            image[i][j].rgbtBlue = round(blue / (counter * 1.0));
        }
    }

    return;
}

Solution 4:[4]

This code works well when you consider blurring an image or n*n matxix using box blur. It considers top, bottom and middle rows and calculates the pixels RGB respectively.

// Blur imageenter code here

void blur(int height, int width, RGBTRIPLE image[height][width])
{
    int sumBlue;
    int sumGreen;
    int sumRed;
    //create a temporary table
    RGBTRIPLE temp[height][width];

    for (int i = 0; i < width; i++)
    {
        for (int j = 0; j < height; j++)
        {
            sumBlue = 0;
            sumGreen = 0;
            sumRed = 0;
            //Top row
            if (i == 0)
            {
                if (j == 0) //top row left corner
                {
                    sumBlue = round(ceil(image[i][j].rgbtBlue
                                         + image[i][j + 1].rgbtBlue
                                         + image[i + 1][j].rgbtBlue
                                         + image[i + 1][j + 1].rgbtBlue) / 4);
                    sumGreen = round(ceil(image[i][j].rgbtGreen
                                          + image[i][j + 1].rgbtGreen
                                          +  image[i + 1][j].rgbtGreen
                                          +  image[i + 1][j + 1].rgbtGreen) / 4);
                    sumRed = round(ceil(image[i][j].rgbtRed
                                        + image[i][j + 1].rgbtRed
                                        + image[i + 1][j].rgbtRed
                                        + image[i + 1][j + 1].rgbtRed) / 4);

                }

                else if (j == width - 1) //top row right corner
                {
                    sumBlue = round(ceil(image[i][j].rgbtBlue
                                         + image[i][j - 1].rgbtBlue
                                         + image[i + 1][j].rgbtBlue
                                         + image[i + 1][j - 1].rgbtBlue) / 4);
                    sumGreen = round(ceil(image[i][j].rgbtGreen
                                          + image[i][j - 1].rgbtGreen
                                          + image[i + 1][j].rgbtGreen
                                          + image[i + 1][j - 1].rgbtGreen) / 4);
                    sumRed = round(ceil(image[i][j].rgbtRed
                                        + image[i][j - 1].rgbtRed
                                        + image[i + 1][j].rgbtRed
                                        + image[i + 1][j - 1].rgbtRed) / 4);
                }

                else //top row middle pixel
                {
                    sumBlue = round(ceil(image[i][j].rgbtBlue
                                         + image[i][j - 1].rgbtBlue
                                         + image[i][j + 1].rgbtBlue
                                         + image[i + 1][j].rgbtBlue
                                         + image[i + 1][j - 1].rgbtBlue
                                         + image[i + 1][j + 1].rgbtBlue) / 6);
                    sumGreen = round(ceil(image[i][j].rgbtGreen
                                          + image[i][j - 1].rgbtGreen
                                          + image[i][j + 1].rgbtGreen
                                          + image[i + 1][j].rgbtGreen
                                          + image[i + 1][j - 1].rgbtGreen
                                          + image[i + 1][j + 1].rgbtGreen) / 6);
                    sumRed = round(ceil(image[i][j].rgbtRed
                                        + image[i][j - 1].rgbtRed
                                        + image[i][j + 1].rgbtRed
                                        + image[i + 1][j].rgbtRed
                                        + image[i + 1][j - 1].rgbtRed
                                        + image[i + 1][j + 1].rgbtRed) / 6);
                }

            }
            //Bottom row of image
            else if (i == height - 1)
            {
                if (j == 0) //left side pixel of bottom row
                {
                    sumBlue = round(ceil(image[i][j].rgbtBlue
                                         + image[i][j + 1].rgbtBlue
                                         + image[i - 1][j].rgbtBlue
                                         + image[i - 1][j + 1].rgbtBlue) / 4);
                    sumGreen = round(ceil(image[i][j].rgbtGreen
                                          + image[i][j + 1].rgbtGreen
                                          + image[i - 1][j].rgbtGreen
                                          + image[i - 1][j + 1].rgbtGreen) / 4);
                    sumRed = round(ceil(image[i][j].rgbtRed
                                        + image[i][j + 1].rgbtRed
                                        + image[i - 1][j].rgbtRed
                                        + image[i - 1][j + 1].rgbtRed) / 4);
                }

                else if (j == width - 1) //right side pixel of last row
                {
                    sumBlue = round(ceil(image[i][j].rgbtBlue
                                         + image[i][j - 1].rgbtBlue
                                         + image[i - 1][j].rgbtBlue
                                         + image[i - 1][j - 1].rgbtBlue) / 4);
                    sumGreen = round(ceil(image[i][j].rgbtGreen
                                          + image[i][j - 1].rgbtGreen
                                          + image[i - 1][j].rgbtGreen
                                          + image[i - 1][j - 1].rgbtGreen) / 4);
                    sumRed = round(ceil(image[i][j].rgbtRed
                                        + image[i][j - 1].rgbtRed
                                        + image[i - 1][j].rgbtRed
                                        + image[i - 1][j - 1].rgbtRed) / 4);
                }

                else //middle pixels of last row
                {
                    sumBlue = round(ceil(image[i][j].rgbtBlue
                                         + image[i][j - 1].rgbtBlue
                                         + image[i][j + 1].rgbtBlue
                                         + image[i - 1][j].rgbtBlue
                                         + image[i - 1][j - 1].rgbtBlue
                                         + image[i - 1][j + 1].rgbtBlue) / 6);
                    sumGreen = round(ceil(image[i][j].rgbtGreen
                                          + image[i][j - 1].rgbtGreen
                                          + image[i][j + 1].rgbtGreen
                                          + image[i - 1][j].rgbtGreen
                                          + image[i - 1][j - 1].rgbtGreen
                                          + image[i - 1][j + 1].rgbtGreen) / 6);
                    sumRed = round(ceil(image[i][j].rgbtRed
                                        + image[i][j - 1].rgbtRed
                                        + image[i][j + 1].rgbtRed
                                        + image[i - 1][j].rgbtRed
                                        + image[i - 1][j - 1].rgbtRed
                                        + image[i - 1][j + 1].rgbtRed) / 6);
                }

            }

            else
            {
                if (j == 0) //left side of image
                {
                    sumBlue = round(ceil(image[i][j].rgbtBlue
                                         + image[i - 1][j].rgbtBlue
                                         + image[i + 1][j].rgbtBlue
                                         + image[i][j + 1].rgbtBlue
                                         + image[i - 1][j + 1].rgbtBlue
                                         + image[i + 1][j + 1].rgbtBlue) / 6); //6 pixels surrounding the left side of the image
                    sumGreen = round(ceil(image[i][j].rgbtGreen
                                          +  image[i - 1][j].rgbtGreen
                                          +  image[i + 1][j].rgbtGreen
                                          +  image[i][j + 1].rgbtGreen
                                          +  image[i - 1][j + 1].rgbtGreen
                                          +  image[i + 1][j + 1].rgbtGreen) / 6);
                    sumRed = round(ceil(image[i][j].rgbtRed
                                        +  image[i - 1][j].rgbtRed
                                        +  image[i + 1][j].rgbtRed
                                        +  image[i][j + 1].rgbtRed
                                        +  image[i - 1][j + 1].rgbtRed
                                        +  image[i + 1][j + 1].rgbtRed) / 6);
                }

                //Right side of image
                else if (j == width - 1)
                {
                    sumBlue = round(ceil(image[i][j].rgbtBlue
                                         + image[i - 1][j].rgbtBlue
                                         + image[i + 1][j].rgbtBlue
                                         + image[i][j - 1].rgbtBlue
                                         + image[i - 1][j - 1].rgbtBlue
                                         + image[i + 1][j - 1].rgbtBlue) / 6);
                    sumGreen = round(ceil(image[i][j].rgbtGreen
                                          +  image[i - 1][j].rgbtGreen
                                          +  image[i + 1][j].rgbtGreen
                                          +  image[i][j - 1].rgbtGreen
                                          +  image[i - 1][j - 1].rgbtGreen
                                          +  image[i + 1][j - 1].rgbtGreen) / 6);
                    sumRed = round(ceil(image[i][j].rgbtRed
                                        +  image[i - 1][j].rgbtRed
                                        +  image[i + 1][j].rgbtRed
                                        +  image[i][j - 1].rgbtRed
                                        +  image[i - 1][j - 1].rgbtRed
                                        +  image[i + 1][j - 1].rgbtRed) / 6);
                }

                else //middle pixels of middle rows
                {
                    //calculate for blue pixels
                    sumBlue = round(ceil(image[i - 1][j - 1].rgbtBlue
                                         + image[i - 1][j].rgbtBlue
                                         + image[i - 1][j + 1].rgbtBlue
                                         + image[i][j - 1].rgbtBlue
                                         + image[i][j].rgbtBlue
                                         + image[i][j + 1].rgbtBlue
                                         + image[i + 1][j - 1].rgbtBlue
                                         + image[i + 1][j].rgbtBlue
                                         + image[i + 1][j + 1].rgbtBlue) / 9);
                    //calculate for green pixels
                    sumGreen = round(ceil(image[i - 1][j - 1].rgbtGreen
                                          + image[i - 1][j].rgbtGreen
                                          + image[i - 1][j + 1].rgbtGreen
                                          + image[i][j - 1].rgbtGreen
                                          + image[i][j].rgbtGreen
                                          + image[i][j + 1].rgbtGreen
                                          + image[i + 1][j - 1].rgbtGreen
                                          + image[i + 1][j].rgbtGreen
                                          + image[i + 1][j + 1].rgbtGreen) / 9); // 9 pixels surrounding the middle one
                    //calculate for red pixels
                    sumRed = round(ceil(image[i - 1][j - 1].rgbtRed
                                        + image[i - 1][j].rgbtRed
                                        + image[i - 1][j + 1].rgbtRed
                                        + image[i][j - 1].rgbtRed
                                        + image[i][j].rgbtRed
                                        + image[i][j + 1].rgbtRed
                                        + image[i + 1][j - 1].rgbtRed
                                        + image[i + 1][j].rgbtRed
                                        + image[i + 1][j + 1].rgbtRed) / 9);
                }
            }

            //assign temp values with the calculated values
            temp[i][j].rgbtBlue = round((sumBlue));
            temp[i][j].rgbtGreen = round((sumGreen));
            temp[i][j].rgbtRed = round((sumRed));

        }
    }

    //copies values from temporary table and assigns to original image
    for (int i = 0; i < width; i++)
    {
        for (int j = 0; j < height; j++)
        {
            //assiging temp value to original image
            image[j][i].rgbtBlue = temp[j][i].rgbtBlue;
            image[j][i].rgbtGreen = temp[j][i].rgbtGreen;
            image[j][i].rgbtRed = temp[j][i].rgbtRed;
        }
    }
}

Solution 5:[5]

I have been researching about this problem for two days (OMG! programming is tough and frustrating). i needed a nice logic that can help us to avoid repeating the same things over and over. Hence, I found this loop. Hope it is understandable. Please let me know if something need to be clarified. Note: include <math.h>

 // Blur image
void blur(int height, int width, RGBTRIPLE image[height][width])
{
    //let us a temporary one that will help us store pixels inorder to avoid overwriting any
    RGBTRIPLE temp[height][width];

    //initialize temporary image structure;
    for (int i = 0; i < height; i++)
    {
        for (int j = 0; j < width; j++)
        {
            temp[i][j].rgbtBlue = image[i][j].rgbtBlue;
            temp[i][j].rgbtGreen = image[i][j].rgbtGreen;
            temp[i][j].rgbtRed = image[i][j].rgbtRed;
        }
    }

    int blue , red, green;
    float counter;

    //loop over our image
    for (int i = 0; i < height; i++)
    {
        for (int j = 0; j < width; j++)
        {
            //initialize all variables to 0 for each new pixel
            counter = 0;
            blue = green = red = 0;

            //now for each pixel we will chech over down, up, left and right to see if it meets the condition
            //it means checking the previous, current, and next pixel
            for (int h = -1; h < 2; h++)
            {
                for (int w = -1; w < 2; w++)
                {
                    //chech if it is still inside the image to avoid memory leak or seg fault
                    if (i + h > height-1 || i + h < 0 || j + w > width-1 || j + w < 0)
                    {
                        continue;
                    }

                    //if not the we must loop and add pixels at all corners of our current pixel
                    blue += image[i+h][j+w].rgbtBlue;
                    green += image[i+h][j+w].rgbtGreen;
                    red += image[i+h][j+w].rgbtRed;
                    counter++;
                }
            }

            //at the end of iterations around a pixel, we must record a new pixel that we just found
            temp[i][j].rgbtBlue = round(blue / counter);
            temp[i][j].rgbtGreen = round(green / counter);
            temp[i][j].rgbtRed = round(red / counter);
        }
    }

    //after we finishing iterations over all pixels of our image, let modify the original image
    //recall that we were copying our findings in temp

    for (int i = 0; i < height; i++)
    {
        for (int j = 0; j < width; j++)
        {
            image[i][j].rgbtBlue = temp[i][j].rgbtBlue;
            image[i][j].rgbtGreen = temp[i][j].rgbtGreen;
            image[i][j].rgbtRed = temp[i][j].rgbtRed;
        }
    }

    return;
}

    

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Tales jardim
Solution 3 JasonTheMarketer
Solution 4 abarna baskar
Solution 5 Gabriel-mdv