0

Here's my code inside the blur function (in pset 4, Filter-Less):

int redSum;
int greenSum;
int blueSum;
int n;

for (int i = 0; i < height; i++)
{
    for (int j = 0; j < width; j++)
    {
        redSum = 0;
        greenSum = 0;
        blueSum = 0;
        n = 0;

        for (int x = -1; x <= 1; x++)
        {
            for (int y = -1; y <= 1; y++)
            {
                if (i + x >= 0 && j + y >= 0)
                {
                    redSum += image[i + x][j + y].rgbtRed;
                    greenSum += image[i + x][j + y].rgbtGreen;
                    blueSum += image[i + x][j + y].rgbtBlue;
                    n++;
                }
            }
        }
        image[i][j].rgbtRed = redSum / n;
        image[i][j].rgbtGreen = greenSum / n;
        image[i][j].rgbtBlue = blueSum / n;
    }
}
return;

It only passes one of the tests (correctly filters pixel in corner) and I think I know what the issue is: it works correctly for the pixel at image[0][0], on the top-left, but then once this value is modified all other values are off because they no longer calculate the averages of the original values (hope this is clear enough, English isn't my first language). So I think the solution would be to find a way to conserve the original values for each pixel, but how?

Of course, I'm not looking for the exact answer, just for some hints in the right direction ;)

EDIT: I've updated my code and this is working better:

int redSum;
int greenSum;
int blueSum;
float n;
RGBTRIPLE tmp[height][width];

for (int i = 0; i < height; i++)
{
    for (int j = 0; j < width; j++)
    {
        redSum = 0;
        greenSum = 0;
        blueSum = 0;
        n = 0;
        tmp[i][j] = image[i][j];

        for (int x = -1; x <= 1; x++)
        {
            for (int y = -1; y <= 1; y++)
            {
                if (i + x >= 0 && j + y >= 0)
                {
                    redSum += image[i + x][j + y].rgbtRed;
                    greenSum += image[i + x][j + y].rgbtGreen;
                    blueSum += image[i + x][j + y].rgbtBlue;
                    n++;
                }
            }
        }
        tmp[i][j].rgbtRed = round(redSum / n);
        tmp[i][j].rgbtGreen = round(greenSum / n);
        tmp[i][j].rgbtBlue = round(blueSum / n);
    }
}

for (int i = 0; i < height; i++)
{
    for (int j = 0; j < width; j++)
    {
        image[i][j].rgbtRed = tmp[i][j].rgbtRed;
        image[i][j].rgbtGreen = tmp[i][j].rgbtGreen;
        image[i][j].rgbtBlue = tmp[i][j].rgbtBlue;
    }
}
return;

Now it passes the first 3 tests (correctly filters middle pixel, pixel on edge, and pixel in corner) but not the last 2 (correctly filters 3x3 image and 4x4 image), and I'm not sure why...

1 Answer 1

0

At a glance, I would say your final issue is related to:

tmp[i][j].rgbtRed = round(redSum / n);

Both redSum and n are ints, so integer division will be performed, and the remainder will be dropped before it gets passed to round(). This has the effect of just flooring the values instead of rounding them. Try casting one of the values to (float) to compel floating point division.

This applies to the two subsequent lines as well, of course.

Edit: also, it doesn't look like your out-of-bounds test checks for coordinates beyond the height and width of the image.

You must log in to answer this question.

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