0

I created a for loop checking each pixel around the one to be blurred. If the pixel was in range, its RGB values would be added to a total variable to then be averaged. My program works for all requirements except the 3x3 and 4x4 images. Here are my results: https://submit.cs50.io/check50/b0fce8b4cfdbb28d3a22b6f7c93c885a8fc60b8a

I'm not sure why it successfully filters all the pixels in an image but 3x3 and 4x4. I tried playing with some of the if statements to make it < width and < height instead of <= width and <= height but that still didn't work. I don't know what else to try.

Here is my code:

// Blur image
void blur(int height, int width, RGBTRIPLE image[height][width])
{
    RGBTRIPLE new_image[height][width];

    // fully duplicate image to avoid blurring already blurred pixels
    for (int i = 0; i < height; i++)
    {
        for (int x = 0; x < width; x++)
        {
            new_image[i][x].rgbtRed = image[i][x].rgbtRed;
            new_image[i][x].rgbtGreen = image[i][x].rgbtGreen;
            new_image[i][x].rgbtBlue = image[i][x].rgbtBlue;
        }
    }

    for (int i = 0; i < height; i++)
    {
        for (int x = 0; x < width; x++)
        {
            /*           center        bottom middle   top middle      center right      center left       bottom right          top left              bottom left           top right
            totalColor = image[i][x] + image[i+1][x] + image[i-1][x] + image[i][x + 1] + image[i][x - 1] + image[i + 1][x + 1] + image[i - 1][x - 1] + image[i + 1][x - 1] + image[i - 1][x + 1]
            */
            int pixels = 1;
            int totalRed = 0, totalGreen = 0, totalBlue = 0;

            // top left
            if (i - 1 >= 0 && x - 1 >= 0)
            {
                pixels++;
                totalRed += new_image[i - 1][x - 1].rgbtRed;
                totalGreen += new_image[i - 1][x - 1].rgbtGreen;
                totalBlue += new_image[i - 1][x - 1].rgbtBlue;
            }
            // top middle
            if (i - 1 >= 0)
            {
                pixels++;
                totalRed += new_image[i - 1][x].rgbtRed;
                totalGreen += new_image[i - 1][x].rgbtGreen;
                totalBlue += new_image[i - 1][x].rgbtBlue;
            }
            // top right
            if (i - 1 >= 0 && x + 1 <= width)
            {
                pixels++;
                totalRed += new_image[i - 1][x + 1].rgbtRed;
                totalGreen += new_image[i - 1][x + 1].rgbtGreen;
                totalBlue += new_image[i - 1][x + 1].rgbtBlue;
            }
            // center left
            if (x - 1 >= 0)
            {
                pixels++;
                totalRed += new_image[i][x - 1].rgbtRed;
                totalGreen += new_image[i][x - 1].rgbtGreen;
                totalBlue += new_image[i][x - 1].rgbtBlue;
            }
            // center right
            if (x + 1 <= width)
            {
                pixels++;
                totalRed += new_image[i][x + 1].rgbtRed;
                totalGreen += new_image[i][x + 1].rgbtGreen;
                totalBlue += new_image[i][x + 1].rgbtBlue;
            }
            // bottom left
            if (i + 1 <= height && x - 1 >= 0)
            {
                pixels++;
                totalRed += new_image[i + 1][x - 1].rgbtRed;
                totalGreen += new_image[i + 1][x - 1].rgbtGreen;
                totalBlue += new_image[i + 1][x - 1].rgbtBlue;
            }
            // bottom middle
            if (i + 1 <= height)
            {
                pixels++;
                totalRed += new_image[i + 1][x].rgbtRed;
                totalGreen += new_image[i + 1][x].rgbtGreen;
                totalBlue += new_image[i + 1][x].rgbtBlue;
            }
            // bottom right
            if (i + 1 <= height && x + 1 <= width)
            {
                pixels++;
                totalRed += new_image[i + 1][x + 1].rgbtRed;
                totalGreen += new_image[i + 1][x + 1].rgbtGreen;
                totalBlue += new_image[i + 1][x + 1].rgbtBlue;
            }

            image[i][x].rgbtRed = round((float)(totalRed + new_image[i][x].rgbtRed) / pixels);
            image[i][x].rgbtGreen = round((float)(totalGreen + new_image[i][x].rgbtGreen) / pixels);
            image[i][x].rgbtBlue = round((float)(totalBlue + new_image[i][x].rgbtBlue) / pixels);
        }
    }
    return;
}```

1 Answer 1

1

Very interesting! Did you notice that in the two failing cases, all of the pixels on the right and bottom edges are incorrect?

So, that says there's something wrong with the code that handles those two edges.

Now, look at the relevant code for the lower right pixel. Remember that an array declared as array[10] has valid elements from 0 to 9, not 10.

        // bottom right
        if (i + 1 <= height && x + 1 <= width)

Next, think about the indices for the lower right pixel. It would be new_image[height - 1][width - 1] So, i+1 can == height and x+1 can == width, are the indices still within the array, or is the code accessing memory that it shouldn't?

If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance, ;-)

7
  • Hello, I'm not exactly sure what you mean. I changed my bottom right to be if (i + 1 == height - 1 && x + 1 == width - 1) as well as for the center right to be if (x + 1 == width - 1) but then check50 said only the middle pixels were blurred correctly. Can you clarify if I'm not understanding something?
    – Amie
    Jul 17, 2021 at 1:45
  • if (i + 1 < height && x + 1 < width)
    – Cliff B
    Jul 17, 2021 at 2:02
  • Hm still no luck. Thank you for identifying the bottom and right though. I'll keep looking at those.
    – Amie
    Jul 17, 2021 at 2:17
  • You did remember to make the similar changes on all the right side and bottom pixel if statements, right?
    – Cliff B
    Jul 17, 2021 at 2:20
  • Yes. Would the bottom left be if (i + 1 < height && x - 1 >= 0) or if (i + 1 < height && x - 1 > 0).
    – Amie
    Jul 17, 2021 at 2:23

You must log in to answer this question.

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