0

Getting close on pset4 filter (less) but getting hung up with the following errors for the blur function. Anyone able to see what I am doing wrong? Would welcome any advice.

Here are the errors I am getting:

:( blur correctly filters middle pixel
    expected "127 140 149\n", not "126 140 149\n"

:( 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..."

Here is my code:

void blur(int height, int width, RGBTRIPLE image[height][width])
{
    RGBTRIPLE temp[height][width]; // Declare temporary array

    for (int i = 0; i < height; i++)
    {
        for (int k = 0; k < width; k++) // Copy values from original bmp into temporary array
        {
            temp[i][k].rgbtRed = image[i][k].rgbtRed;
            temp[i][k].rgbtBlue = image[i][k].rgbtBlue;
            temp[i][k].rgbtGreen = image[i][k].rgbtGreen;
        }
    }

    for (int x = 0; x < height; x++)
    {
        for (int y = 0; y < width; y++)
        {
            if (x == 0 && y == 0) // Blur top left corner pixel
            {
                image[x][y].rgbtRed = roundf((temp[x][y].rgbtRed + temp[x + 1][y].rgbtRed + temp[x][y + 1].rgbtRed + temp[x + 1][y + 1].rgbtRed) / 4);
                image[x][y].rgbtGreen = roundf((temp[x][y].rgbtGreen + temp[x + 1][y].rgbtGreen + temp[x][y + 1].rgbtGreen + temp[x + 1][y + 1].rgbtGreen) / 4);
                image[x][y].rgbtBlue = roundf((temp[x][y].rgbtBlue + temp[x + 1][y].rgbtBlue + temp[x][y + 1].rgbtBlue + temp[x + 1][y + 1].rgbtBlue) / 4);
            }
            if (x == 0 && y == height) // Blur bottom left corner pixel
            {
                image[x][y].rgbtRed = roundf((temp[x][y].rgbtRed + temp[x][y - 1].rgbtRed + temp[x + 1][y].rgbtRed + temp[x + 1][y - 1].rgbtRed) / 4);
                image[x][y].rgbtGreen = roundf((temp[x][y].rgbtGreen + temp[x][y - 1].rgbtGreen + temp[x + 1][y].rgbtGreen + temp[x + 1][y - 1].rgbtGreen) / 4);
                image[x][y].rgbtBlue = roundf((temp[x][y].rgbtBlue + temp[x][y - 1].rgbtBlue + temp[x + 1][y].rgbtBlue + temp[x + 1][y - 1].rgbtBlue) / 4);
            }
            if (x == width && y == height) // Blur bottom right corner pixel
            {
                image[x][y].rgbtRed = roundf((temp[x][y].rgbtRed + temp[x - 1][y - 1].rgbtRed + temp[x][y - 1].rgbtRed + temp[x - 1][y - 1].rgbtRed) / 4);
                image[x][y].rgbtGreen = roundf((temp[x][y].rgbtGreen + temp[x - 1][y - 1].rgbtGreen + temp[x][y - 1].rgbtGreen + temp[x - 1][y - 1].rgbtGreen) / 4);
                image[x][y].rgbtBlue = roundf((temp[x][y].rgbtBlue + temp[x - 1][y - 1].rgbtBlue + temp[x][y - 1].rgbtBlue + temp[x - 1][y - 1].rgbtBlue) / 4);
            }
            if (x == width && y == 0) // Blur top right corner pixel
            {
                image[x][y].rgbtRed = roundf((temp[x][y].rgbtRed + temp[x - 1][y + 1].rgbtRed + temp[x + 1][y].rgbtRed + temp[x - 1][y].rgbtRed) / 4);
                image[x][y].rgbtGreen = roundf((temp[x][y].rgbtGreen + temp[x - 1][y + 1].rgbtGreen + temp[x + 1][y].rgbtGreen + temp[x - 1][y].rgbtGreen) / 4);
                image[x][y].rgbtBlue = roundf((temp[x][y].rgbtBlue + temp[x - 1][y + 1].rgbtBlue + temp[x + 1][y].rgbtBlue + temp[x - 1][y].rgbtBlue) / 4);
            }
            if (x == 0 && y != 0 && y != height) // Blur left edge
            {
                image[x][y].rgbtRed = roundf((temp[x][y - 1].rgbtRed + temp[x + 1][y - 1].rgbtRed + temp[x][y].rgbtRed + temp[x][y + 1].rgbtRed + temp[x + 1][y].rgbtRed + temp[x + 1][y + 1].rgbtRed) / 6);
                image[x][y].rgbtGreen = roundf((temp[x][y - 1].rgbtGreen + temp[x + 1][y - 1].rgbtGreen + temp[x][y].rgbtGreen + temp[x][y + 1].rgbtGreen + temp[x + 1][y].rgbtGreen + temp[x + 1][y + 1].rgbtGreen) / 6);
                image[x][y].rgbtBlue = roundf((temp[x][y - 1].rgbtBlue + temp[x + 1][y - 1].rgbtBlue + temp[x][y].rgbtBlue + temp[x][y + 1].rgbtBlue + temp[x + 1][y].rgbtBlue + temp[x + 1][y + 1].rgbtBlue) / 6);
            }
            if (x == width && y != 0 && y != height) // Blur right edge
            {
                image[x][y].rgbtRed = roundf((temp[x][y - 1].rgbtRed + temp[x][y + 1].rgbtRed + temp[x][y].rgbtRed + temp[x - 1][y].rgbtRed + temp[x - 1][y + 1].rgbtRed + temp[x - 1][y - 1].rgbtRed) / 6);
                image[x][y].rgbtGreen = roundf((temp[x][y - 1].rgbtGreen + temp[x][y + 1].rgbtGreen + temp[x][y].rgbtGreen + temp[x - 1][y].rgbtGreen + temp[x -  1][y + 1].rgbtGreen + temp[x - 1][y - 1].rgbtGreen) / 6);
                image[x][y].rgbtBlue = roundf((temp[x][y - 1].rgbtBlue + temp[x][y + 1].rgbtBlue + temp[x][y].rgbtBlue + temp[x - 1][y].rgbtBlue + temp[x - 1][y + 1].rgbtBlue + temp[x + 1][y - 1].rgbtBlue) / 6);
            }
            if (y == 0 && x != 0 && x != width) // Blur top edge
            {
                image[x][y].rgbtRed = roundf((temp[x][y + 1].rgbtRed + temp[x + 1][y + 1].rgbtRed + temp[x][y].rgbtRed + temp[x - 1][y].rgbtRed + temp[x + 1][y].rgbtRed + temp[x][y + 1].rgbtRed) / 6);
                image[x][y].rgbtGreen = roundf((temp[x][y +  1].rgbtGreen + temp[x + 1][y + 1].rgbtGreen + temp[x][y].rgbtGreen + temp[x - 1][y].rgbtGreen + temp[x + 1][y].rgbtGreen + temp[x][y + 1].rgbtGreen) / 6);
                image[x][y].rgbtBlue = roundf((temp[x][y + 1].rgbtBlue + temp[x + 1][y + 1].rgbtBlue + temp[x][y].rgbtBlue + temp[x - 1][y].rgbtBlue + temp[x + 1][y].rgbtBlue + temp[x][y - 1].rgbtBlue) / 6);
            }
            if (y == height && x != 0 && x != width) // Blur bottom edge
            {
                image[x][y].rgbtRed = roundf((temp[x][y - 1].rgbtRed + temp[x + 1][y].rgbtRed + temp[x][y].rgbtRed + temp[x - 1][y].rgbtRed + temp[x + 1][y].rgbtRed + temp[x - 1][y - 1].rgbtRed) / 6);
                image[x][y].rgbtGreen = roundf((temp[x][y - 1].rgbtGreen + temp[x + 1][y].rgbtGreen + temp[x][y].rgbtGreen + temp[x - 1][y].rgbtGreen + temp[x + 1][y].rgbtGreen + temp[x - 1][y - 1].rgbtGreen) / 6);
                image[x][y].rgbtBlue = roundf((temp[x][y - 1].rgbtBlue + temp[x + 1][y].rgbtBlue + temp[x][y].rgbtBlue + temp[x - 1][y].rgbtBlue + temp[x + 1][y].rgbtBlue + temp[x + 1][y - 1].rgbtBlue) / 6);
            }
            if (y != height && y != 0 && x != 0 && x != width) // Blur pixels in center
            {
                image[x][y].rgbtRed = roundf((temp[x - 1][y - 1].rgbtRed + temp[x - 1][y].rgbtRed + temp[x - 1][y + 1].rgbtRed + temp[x][y - 1].rgbtRed + temp[x][y].rgbtRed + temp[x][y + 1].rgbtRed + temp[x + 1][y - 1].rgbtRed + temp[x + 1][y].rgbtRed + temp[x + 1][y + 1].rgbtRed) / 9);
                image[x][y].rgbtGreen = roundf((temp[x - 1][y - 1].rgbtGreen + temp[x - 1][y].rgbtGreen + temp[x - 1][y + 1].rgbtGreen + temp[x][y - 1].rgbtGreen + temp[x][y].rgbtGreen + temp[x][y + 1].rgbtGreen + temp[x + 1][y - 1].rgbtGreen + temp[x + 1][y].rgbtGreen + temp[x + 1][y + 1].rgbtGreen) / 9);
                image[x][y].rgbtBlue = roundf((temp[x - 1][y - 1].rgbtBlue + temp[x - 1][y].rgbtBlue + temp[x - 1][y + 1].rgbtBlue + temp[x][y - 1].rgbtBlue+ temp[x][y].rgbtBlue + temp[x][y + 1].rgbtBlue + temp[x + 1][y - 1].rgbtBlue + temp[x + 1][y].rgbtBlue + temp[x + 1][y + 1].rgbtBlue) / 9);
            }
        }
    }
1

There are three groups of problems in this code.

First, it looks like a simple oversight error on the test conditions. In each test for x==width or y==height, shouldn't those be width - 1 or height - 1? Remember, an array of size x runs from 0 to x-1.

Second, each formula uses all integer vars and then is divided by the number of pixels, either 4, 6 or 9. But, because they're written as integers, it's integer division, not regular division, causing off-by-1 errors. The code needs to use 4.0, etc.

Finally, some of the formulas simply have errors in them. Specifically, they are adding the same cell twice, missing other cells. You'll need to reverify that all of the surrounding cells are added in, and only once.

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

You must log in to answer this question.

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