0

I'm having difficulty with pset4 Blur. I cannot seem to pass the 3x3 and 4x4 test in check50. The actual output values always seem to diverge from the expected output values after a few pixels have been looped through.

This is my 3rd attempt at the blur function, i.e. I've already completely rewritten the code twice. First time my approach was to loop clockwise through each pixel if it existed, then second attempt I tried to target corners, edges and middle pixels separately but the code got too complicated. It now makes sense to me to loop through each row of the grid which 'could' surround the current pixel in the loop.

If anyone could give me some advice I'd be much appreciated.

void blur(int height, int width, RGBTRIPLE image[height][width])
{
    int row, col;
    int a, b;
    RGBTRIPLE copy[height][width];


    for (row = 0; row < height; row++)
    {
        for (col = 0; col < width; col++)
        {

            int sumRed = 0;
            int sumGreen = 0;
            int sumBlue = 0;
            float totalPixels = 0.00;

            // pixel grid layout
            // image[row - 1][col - 1]  image[row - 1][col]  image[row - 1][col + 1]
            // image[row][col - 1]      image[row][col]      image[row][col + 1];
            // image[row + 1][col - 1]  image[row + 1][col]  image[row + 1][col + 1]


            // prevent out of bounds
            // top row
            for (int top = col -1; top <= col +1; top++)
            {
                // if the pixels in the top row are in the bounds of the array
                if (row -1 >= 0 && (top >= 0 && top < width))
                {
                    sumRed += image[row - 1][top].rgbtRed;
                    sumGreen += image[row - 1][top].rgbtGreen;
                    sumBlue += image[row - 1][top].rgbtBlue;
                    totalPixels++;
                }
            }

            // middle row
            for (int mid = col -1; mid <= col +1; mid++)
            {
                if (row >= 0 && (mid >= 0 && mid < width))
                {
                    sumRed += image[row][mid].rgbtRed;
                    sumGreen += image[row][mid].rgbtGreen;
                    sumBlue += image[row][mid].rgbtBlue;
                    totalPixels++;
                }
            }

            // bottom row
            for (int low = col -1; low <= col +1; low++)
            {
                if (row +1 >= 0 && (low >= 0 && low < width))
                {
                    sumRed += image[row +1][low].rgbtRed;
                    sumGreen += image[row +1][low].rgbtGreen;
                    sumBlue += image[row +1][low].rgbtBlue;
                    totalPixels++;
                }
            }

            copy[row][col].rgbtRed = round((sumRed / totalPixels));
            copy[row][col].rgbtGreen = round((sumGreen / totalPixels));
            copy[row][col].rgbtBlue = round((sumBlue / totalPixels));
        }

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

1 Answer 1

0

Yo. Wasup. I was also struggling with PSet4 and Blur in particular. I also went thru something similar to your solution which was to create different logic gates that would compute if it was at an edge or what. I'm sorry to say, but that brute force of a solution won't work. And that took like 8 cases cuz you have to consider the top row, bottom row, left wall, right wall, corners, then the actual middle squares which would be the only ones really surrounded by 9 squares. Or atleast, theoretically it works - but think about it what if you get a 5x5 or 6x6 square?

There is a more elegant and very much simpler to code solution that would generically apply to any square. You're halfway there by traversing thru the matrix with a nested for loop. And you're also correct in that its about finding squares 1 square away (+/- 1).

Think about it and see if you can see another way to solve it.

Don't worry, it also took someone else nudging me in the right direction to figure it out.

6
  • Were you able to solve it? My solution for your reference pastebin.com/cpwq0Tkx
    – Andrew
    Commented Nov 10, 2020 at 12:32
  • Hi Andrew, thanks so much for your help with this.
    – mjday
    Commented Nov 10, 2020 at 16:17
  • Hi Andrew, thanks so much for your help with this. This bit really got me thinking "There is a more elegant and very much simpler to code solution that would generically apply to any square. You're halfway there by traversing thru the matrix with a nested for loop. And you're also correct in that its about finding squares 1 square away (+/- 1)."
    – mjday
    Commented Nov 10, 2020 at 16:21
  • And I decided to rewrite a good chunk of the code. It made sense from your comment to add another nested loop and then use those variables only once in the loop. I've just had a look at your pastebin, and I did mine slightly differently. For the 2nd nested loop, I decided to loop only around 8 pixels surrounding the current pixel in the loop. This was the code in my loop:
    – mjday
    Commented Nov 10, 2020 at 16:22
  • for (int r = row - 1; r <= row + 1; r++) { for (int c = col - 1; c <= col + 1; c++) { if (r > - 1 && r < height && c > - 1 && c < width) { sumRed += image[r][c].rgbtRed; sumGreen += image[r][c].rgbtGreen; sumBlue += image[r][c].rgbtBlue; totalPixels++; } } }
    – mjday
    Commented Nov 10, 2020 at 16:23

You must log in to answer this question.

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