0

For the filter program on pset4, the blur filter seems to be working fine for all images that were included in the distribution code. The blur filter takes a pixel in an image and adds its color values with those of adjacent pixels and provides. The pixel's color values change to the average of all red, green, and blue values respectively. The program iterates over all pixels and applies the same algorithm.

Here is what my code looks like:

// Blur image
void blur(int height, int width, RGBTRIPLE image[height][width])
{
    // Iterate through each row of pixels
    // Declare red, green, blue, and counter variables
    for (int i = 0, red, green, blue, counter; i < height; i++)
    {
        // Iterate through each pixel per row
        for (int j = 0; j < width; j++)
        {
            // Assign counter, red, green, and blue variables to 0
        counter = red = green = blue = 0;

            // Add pixel[i][j]'s color value to respective neighboring pixel's color value
            // Add counter by 1

            // Check if i and j are greater than or equal to 0
            if (i >= 0 && j >= 0)
            {
                red += image[i][j].rgbtRed;
                green += image[i][j].rgbtGreen;
                blue += image[i][j].rgbtBlue;
                counter++;
            }

            // Check if (i - 1) and (j - 1) are greater than or equal to 0
            if (i - 1 >= 0 && j - 1 >= 0)
            {
                red += image[i - 1][j - 1].rgbtRed;
                green += image[i - 1][j - 1].rgbtGreen;
                blue += image[i - 1][j - 1].rgbtBlue;
                counter++;
            }

            // Check if (i - 1) is greater than or equal to 0
            if (i - 1 >= 0 && j >= 0)
            {
                red += image[i - 1][j].rgbtRed;
                green += image[i - 1][j].rgbtGreen;
                blue += image[i - 1][j].rgbtBlue;
                counter++;
            }

            // Check if (i - 1) is greater than or equal to 0
            // Check if (j + 1) is less than width
            if ((i - 1 >= 0 && j + 1 >= 0) && (i - 1 >= 0 && j + 1 < width))
            {
                red += image[i - 1][j + 1].rgbtRed;
                green += image[i - 1][j + 1].rgbtGreen;
                blue += image[i - 1][j + 1].rgbtBlue;
                counter++;
            }

            // Check if (j - 1) is greater than or equal to 0
            if (i >= 0 && j - 1 >= 0)
            {
                red += image[i][j - 1].rgbtRed;
                green += image[i][j - 1].rgbtGreen;
                blue += image[i][j - 1].rgbtBlue;
                counter++;
            }

            // Check if (j + 1) is less than width
            if ((i >= 0 && j + 1 >= 0) && (i >= 0 && j + 1 < width))
            {
                red += image[i][j + 1].rgbtRed;
                green += image[i][j + 1].rgbtGreen;
                blue += image[i][j + 1].rgbtBlue;
                counter++;
            }

            // Check if (i + 1) is less than height
            // Check if (j - 1) is greater than or equal to 0
            if ((i + 1 >= 0 && j - 1 >= 0) && (i + 1 < height && j - 1 >= 0))
            {
                red += image[i + 1][j - 1].rgbtRed;
                green += image[i + 1][j - 1].rgbtGreen;
                blue += image[i + 1][j - 1].rgbtBlue;
                counter++;
            }

            // Check if (i + 1) is less than height
            if ((i + 1 >= 0 && j >= 0) && (i + 1 < height && j >= 0))
            {
                red += image[i + 1][j].rgbtRed;
                green += image[i + 1][j].rgbtGreen;
                blue += image[i + 1][j].rgbtBlue;
                counter++;
            }

            // Check if (i + 1) is less than height
            // Check if (j + 1) is less than width
            if ((i + 1 >= 0 && j + 1 >= 0) && (i + 1 < height && j + 1 < width))
            {
                red += image[i + 1][j + 1].rgbtRed;
                green += image[i + 1][j + 1].rgbtGreen;
                blue += image[i + 1][j + 1].rgbtBlue;
                counter++;
            }

            // Calculate the average values
            int avgRed = round((float)red / counter);
            int avgGreen = round((float)green / counter);
            int avgBlue = round((float)blue / counter);

            // Assign pixel values with avg color variables
            image[i][j].rgbtRed = avgRed;
            image[i][j].rgbtGreen = avgGreen;
            image[i][j].rgbtBlue = avgBlue;
        }
    }
    return;
}

According to check50:

:( blur correctly filters middle pixel

    expected "127 140 149\n", not "145 160 169\n"

:( blur correctly filters pixel on edge

    expected "80 95 105\n", not "90 106 116\n"

:) blur correctly filters pixel in corner

:( blur correctly filters 3x3 image

    expected "70 85 95\n80 9...", not "70 85 95\n90 1..."

:( blur correctly filters 4x4 image

    expected "70 85 95\n80 9...", not "70 85 95\n90 1..."
6

You overwrite pixels you still need later, for calculating the neighbouring pixels. What looks about right visually is still wrong.

I made another array, wrote my blurred image to that, then later copied over.

| improve this answer | |

You must log in to answer this question.

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