0

I've been working for like a week on cs50/pset/filter/blur question, but this doesn't seem to be working. My code is like this, and the resulting "blur filtered" image is not any close to what is expected.

sorry for the length of the code, please help me out.

void blur(int height, int width, RGBTRIPLE image[height][width])
{
    int h, w;
    float bR = 0, bG = 0, bB = 0;//for blurred Red, Green, Blue
    int count = 0;
    RGBTRIPLE copy[height][width];

for (h = 0; h < height; h++)
{
    for (w = 0; w < width; w++)
    {
        if (h >= 0 && w >= 0)
        {
            bR += image[h][w].rgbtRed;
            bG += image[h][w].rgbtGreen;
            bB += image[h][w].rgbtBlue;
            count++;
        }

        if (h + 1 < height && w - 1 >= 0)
        {
            bR += image[h+1][w-1].rgbtRed;
            bG += image[h+1][w-1].rgbtGreen;
            bB += image[h+1][w-1].rgbtBlue;
            count++;
        }

        if (h + 1 < height)
        {
            bR += image[h+1][w].rgbtRed;
            bG += image[h+1][w].rgbtGreen;
            bB += image[h+1][w].rgbtBlue;
            count++;
        }

        if (h + 1 < height && w + 1 < width)
        {
            bR += image[h+1][w+1].rgbtRed;
            bG += image[h+1][w+1].rgbtGreen;
            bB += image[h+1][w+1].rgbtBlue;
            count++;
        }

        if (w - 1 >= 0)
        {
            bR += image[h][w-1].rgbtRed;
            bG += image[h][w-1].rgbtGreen;
            bB += image[h][w-1].rgbtBlue;
            count++;
        }

        if (w + 1 < width)
        {
            bR += image[h][w+1].rgbtRed;
            bG += image[h][w+1].rgbtGreen;
            bB += image[h][w+1].rgbtBlue;
            count++;
        }

        if (h - 1 >= 0 && w - 1 >= 0)
        {
            bR += image[h-1][w-1].rgbtRed;
            bG += image[h-1][w-1].rgbtGreen;
            bB += image[h-1][w-1].rgbtBlue;
            count++;
        }

        if (h - 1 >= 0)
        {
            bR += image[h-1][w].rgbtRed;
            bG += image[h-1][w].rgbtGreen;
            bB += image[h-1][w].rgbtBlue;
            count++;
        }

        if (h - 1 >= 0 && w + 1 < width)
        {
            bR += image[h-1][w+1].rgbtRed;
            bG += image[h-1][w+1].rgbtGreen;
            bB += image[h-1][w+1].rgbtBlue;
            count++;
        }

        int aveR = (int)floor(bR / count + 0.5);
        int aveG = (int)floor(bG / count + 0.5);
        int aveB = (int)floor(bB / count + 0.5);

        copy[h][w].rgbtRed = aveR;
        copy[h][w].rgbtGreen = aveG;
        copy[h][w].rgbtBlue = aveB;
    }
}

for (h = 0; h < height; h++)
{
    for (w = 0; w < width; w++)
    {
        image[h][w].rgbtRed = copy[h][w].rgbtRed;
        image[h][w].rgbtGreen = copy[h][w].rgbtGreen;
        image[h][w].rgbtBlue = copy[h][w].rgbtBlue;
    }
}
return;
}
0

I had the same problem. To be honest, I still don't know why this fix works, but copying the image array into a new one and retrieving all the values from the copied array fixed my problem without any other changes to the code. Try it out.

RGBTRIPLE copy[height][width];
for (int h = 0; h < height; h++)
    for (int w = 0; w < width; w++)
        copy[h][w] = image[h][w];
1
  • The reason why you need to add the image into an temporary array is so that the average values you calculate for the blur effect are taken from the original image. If you iterate through the loop without using a temporary image, eventually you'll to the pixel at location image[i][j] and look at the surrounding pixels. All pixels that come after image[i][j] will be normal, but every pixel before it will have gone through the blur effect, so the RGB values will be different from the original, and those values will be used in the calculation. – Mance Rah Jul 15 '20 at 5:21
0

You need to reset your count, bB, bR and bG after each for loop. In this code the values of count, bB, bR and bG continues to add up even when you move on to a new pixel. Instead of the first if statement (the condition itself is unnecessary since the middle pixel already satisfies the condition in the two for loops), you can reset the values simply by replacing

        if (h >= 0 && w >= 0)
    {
        bR += image[h][w].rgbtRed;
        bG += image[h][w].rgbtGreen;
        bB += image[h][w].rgbtBlue;
        count++;
    }

with

        bR = image[h][w].rgbtRed;
        bG = image[h][w].rgbtGreen;
        bB = image[h][w].rgbtBlue;
        count = 1;

You must log in to answer this question.

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