0

Been stuck on the blurring part for ages... I've ran out of ideas and motivation to debug at this point after spending an eternity cutting down my spaghetti codes to pappardelle.

Any help/hint/suggestion is appreciated!

The corners work, but not the edges or the middle.

    // Blur image
void blur(int height, int width, RGBTRIPLE image[height][width])
{
    //create and store a temporary copy of image and variables for summing 9 pixels
    RGBTRIPLE temp[height][width];
    int sumr;
    int sumg;
    int sumb;
    float count;

    //first step to summing main pixel + 8 surrounding pixel: setting the sum as 0 to start

    sumr = 0;
    sumg = 0;
    sumb = 0;

    //use a counter as a float to keep count of values added, set it to 0 to start
    count = 0.00;

    for(int i = 0; i < height; i++)
    {
        for(int j = 0; j < width; j++)
        {
            for(int k = -1; k < 2; k++) //creating variable k to scan and find pixel above and below the main pixel
            {
                if(i + k < 0 || i + k > height - 1)
                {
                    continue; //skip if the pixel is over the horizontal edges 
                }

                for(int l = -1; l < 2; l++) //create variable l to find pixel to the left and right of main
                {

                    if(j + l < 0 || j + l > width - 1)
                    {
                        continue; //skip if the pixel is over the vertical edges
                    }

                    //sum up 4/6/9 pixels for red, green, blue respectively
                    sumr += image[i + k][j + l].rgbtRed; 
                    sumg += image[i + k][j + l].rgbtGreen;
                    sumb += image[i + k][j + l].rgbtBlue;
                    count++; //keep count of how many pixels are added to differentiate among 4(corner)/6(edge)/9(middle) pixels

                }
            }
            //divide sum of r,g,b by count to get the average of surrounding pixels
            //store the blurred values of rgb in temps
            temp[i][j].rgbtRed = round(sumr / count);
            temp[i][j].rgbtGreen = round(sumg / count);
            temp[i][j].rgbtBlue = round(sumb / count);
        }


    }

    //blur the original image by setting pixels to the values in temps one by one
    for(int i = 0; i < height; i++)
    {
        for(int j = 0; j < width; j++)
        {
            image[i][j].rgbtRed = temp[i][j].rgbtRed;
            image[i][j].rgbtGreen = temp[i][j].rgbtGreen;
            image[i][j].rgbtBlue = temp[i][j].rgbtBlue;
        }

    }
    return;
}

:( blur correctly filters middle pixel
    expected "127 140 149\n", not "102 116 126\n"
:( blur correctly filters pixel on edge
    expected "80 95 105\n", not "76 91 101\n"
:) blur correctly filters pixel in corner
:( blur correctly filters 3x3 image
    expected "70 85 95\n80 9...", not "70 85 95\n76 9..."
:( blur correctly filters 4x4 image
    expected "70 85 95\n80 9...", not "70 85 95\n76 9..."
0

You have to reset your variables after each pixel is processed.

| improve this answer | |

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .