1

I've been working on CS50 PSET4 Filter (Less) and finally got a working blur function (or so I thought!)

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

    RGBTRIPLE imgbuffer[height][width]; // Create an RGBTRIPLE 2-D array to store modified pixels.

    float counter = 0.00;
    int redsum, bluesum, greensum;
    redsum = bluesum = greensum = 0;

    for (int i = 0; i < height; i++)
    {
        for (int j = 0; j < width; j++)
        {
            imgbuffer[i][j] = image[i][j];
        }
    }

    for (int i = 0; i < height; i++)
    {
        for (int j = 0; j < width; j++)
        {
            for (int k = - 1; k <= 1; k++) // Iterate from top to bottom of nearby pixels
            {
                for (int l = -1; l <= 1; l++) // Iterate left to right of nearby pixels
                {
                    if (i + k >= 0 && i + k <= height && j + l >= 0 && j + l <= width) // check if pixel fits
                    {
                        redsum += image[i + k][j + l].rgbtRed;
                        greensum += image[i + k][j + l].rgbtGreen;
                        bluesum += image[i + k][j + l].rgbtBlue;
                        counter += 1;
                    }

                }
            }


            imgbuffer[i][j].rgbtRed = round(redsum / counter);
            imgbuffer[i][j].rgbtGreen = round(greensum / counter);
            imgbuffer[i][j].rgbtBlue = round(bluesum / counter);

            counter = 0;
            redsum = bluesum = greensum = 0;
        }
    }

    for (int x = 0; x < height; x++)
    {
        for (int y = 0; y < width; y++)
        {
            image[x][y] = imgbuffer[x][y];
        }
    }
    return;
}

The code seemingly blurs images without error, but check50 throws errors for the last two conditions:

:( blur correctly filters 3x3 image
    expected "70 85 95\n80 9...", not "70 85 95\n80 9..."
:( blur correctly filters 4x4 image
    expected "70 85 95\n80 9...", not "70 85 95\n80 9..."

Could anyone help me out? :-)

1
  • Hello @jonathandavis, I'm working on pset4/filter-less/blur right now. In your solution, did you consider using pointers ? cheers,\al. Commented Aug 3, 2022 at 0:24

1 Answer 1

2

"The code seemingly blurs images without error" indicates a problem. "Seemingly" correct output is not exactly correct. It tells me that the actual digital data output needs to be checked byte by byte. Checking the image to see if it seems wrong (not so much right) is a useful tool, but close isn't good enough. That's why it's necessary to check the digital output and compare it numerically to the expected output.

In this case, if you look at the actual output vs. the expected output, you'd see that in both failed tests, the pixels on the right and bottom edges are all incorrect. That should be screaming about where the error lies. Hint: It's a very easy fix!

1
  • 2
    Got it! Thanks for the tip of checking the output – turned out I just needed to change a few signs in my 'pixel valid' Boolean expression! Commented Oct 24, 2021 at 4:36

You must log in to answer this question.

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