Home > other >  Why is my blur function in CS50's filter(less) (PSET 4) not returning the correct values?
Why is my blur function in CS50's filter(less) (PSET 4) not returning the correct values?

Time:04-03

currently stuck on CS50's PSET 4 filter less. To create the blur function, I came up with 9 possible cases of where a pixel could in an image (eg in 1st row and 1st column). However, when I used check50 to check my code, the values the function returns are different from the expected values. I have looked through my code multiple times but I can't quite see where the problem lies and was hoping for a second pair of eyes to help me out. Thanks in advance.

#include "helpers.h"
#include <math.h>

// Blur image
void blur(int height, int width, RGBTRIPLE image[height][width])
{
    // Copy 2D image array into a new array to work with
    RGBTRIPLE copy[height][width];
    for (int i = 0; i < height; i  )
    {
        for (int j = 0; j < width; j  )
        {
            copy[i][j] = image[i][j];
        }
    }
    // Determine case and respond accordingly
    for (int i = 0; i < height; i  )
    {
        for (int j = 0; j < width; j  )
        {
            // CASE 1: Pixel is not in 1st/last row AND not in 1st/LAST column
            if (0 < j < width - 1 && 0 < i < height - 1 )
            {
                int R1 = round((copy[i-1][j-1].rgbtRed   copy[i-1][j].rgbtRed   copy[i-1][j 1].rgbtRed   copy[i][j-1].rgbtRed   copy[i][j].rgbtRed   copy[i][j 1].rgbtRed   copy[i 1][j-1].rgbtRed   copy[i 1][j].rgbtRed   copy[i 1][j 1].rgbtRed)/9.0);
                int G1 = round((copy[i-1][j-1].rgbtGreen   copy[i-1][j].rgbtGreen   copy[i-1][j 1].rgbtGreen   copy[i][j-1].rgbtGreen   copy[i][j].rgbtGreen   copy[i][j 1].rgbtGreen   copy[i 1][j-1].rgbtGreen   copy[i 1][j].rgbtGreen   copy[i 1][j 1].rgbtGreen)/9.0);
                int B1 = round((copy[i-1][j-1].rgbtBlue   copy[i-1][j].rgbtBlue   copy[i-1][j 1].rgbtBlue   copy[i][j-1].rgbtBlue   copy[i][j].rgbtBlue   copy[i][j 1].rgbtBlue   copy[i 1][j-1].rgbtBlue   copy[i 1][j].rgbtBlue   copy[i 1][j 1].rgbtBlue)/9.0);
                image[i][j].rgbtRed = R1;
                image[i][j].rgbtGreen = G1;
                image[i][j].rgbtBlue = B1;
            }
            // CASE 2: Pixel in 1st row, 1st column
            else if (i == 0 && j == 0)
            {
                int R2 = round((copy[i][j].rgbtRed   copy[i][j 1].rgbtRed   copy[i 1][j].rgbtRed   copy[i 1][j 1].rgbtRed)/4.0);
                int B2 = round((copy[i][j].rgbtBlue   copy[i][j 1].rgbtBlue   copy[i 1][j].rgbtBlue   copy[i 1][j 1].rgbtBlue)/4.0);
                int G2 = round((copy[i][j].rgbtGreen   copy[i][j 1].rgbtGreen   copy[i 1][j].rgbtGreen   copy[i 1][j 1].rgbtGreen)/4.0);
                image[i][j].rgbtRed = R2;
                image[i][j].rgbtGreen = G2;
                image[i][j].rgbtBlue = B2;
            }
            // CASE 3: Pixel in 1st row, NOT 1ST/LAST COLUMN
            else if (i == 0 && 0 < j < width - 1)
            {
                int R3 = round((copy[i][j-1].rgbtRed   copy[i][j].rgbtRed   copy[i][j 1].rgbtRed   copy[i 1][j-1].rgbtRed   copy[i 1][j].rgbtRed   copy[i 1][j 1].rgbtRed)/6.0);
                int G3 = round((copy[i][j-1].rgbtGreen   copy[i][j].rgbtGreen   copy[i][j 1].rgbtGreen   copy[i 1][j-1].rgbtGreen   copy[i 1][j].rgbtGreen   copy[i 1][j 1].rgbtGreen)/6.0);
                int B3 = round((copy[i][j-1].rgbtBlue   copy[i][j].rgbtBlue   copy[i][j 1].rgbtBlue   copy[i 1][j-1].rgbtBlue   copy[i 1][j].rgbtBlue   copy[i 1][j 1].rgbtBlue)/6.0);
                image[i][j].rgbtRed = R3;
                image[i][j].rgbtGreen = G3;
                image[i][j].rgbtBlue = B3;
            }
            // CASE 4: PIXEL IN 1ST ROW, LAST COLUMN
            else if (i == 0 && j == width - 1)
            {
                int R4 = round((copy[i][j-1].rgbtRed   copy[i][j].rgbtRed   copy[i 1][j].rgbtRed   copy[i 1][j-1].rgbtRed)/4.0);
                int B4 = round((copy[i][j-1].rgbtBlue   copy[i][j].rgbtBlue   copy[i 1][j].rgbtBlue   copy[i 1][j-1].rgbtBlue)/4.0);
                int G4 = round((copy[i][j-1].rgbtGreen   copy[i][j].rgbtGreen   copy[i 1][j].rgbtGreen   copy[i 1][j-1].rgbtGreen)/4.0);
                image[i][j].rgbtRed = R4;
                image[i][j].rgbtGreen = G4;
                image[i][j].rgbtBlue = B4;
            }
            // CASE 5: PIXEL NOT IN 1ST/LAST ROW, IN 1ST COLUMN
            else if (0 < i < height - 1 && j == 0)
            {
                int R5 = round((copy[i-1][j].rgbtRed   copy[i-1][j 1].rgbtRed   copy[i][j].rgbtRed   copy[i][j 1].rgbtRed   copy[i 1][j].rgbtRed   copy[i 1][j 1].rgbtRed)/6.0);
                int G5 = round((copy[i-1][j].rgbtBlue   copy[i-1][j 1].rgbtBlue   copy[i][j].rgbtBlue   copy[i][j 1].rgbtBlue   copy[i 1][j].rgbtBlue   copy[i 1][j 1].rgbtBlue)/6.0);
                int B5 = round((copy[i-1][j].rgbtGreen   copy[i-1][j 1].rgbtGreen   copy[i][j].rgbtGreen   copy[i][j 1].rgbtGreen   copy[i 1][j].rgbtGreen   copy[i 1][j 1].rgbtGreen)/6.0);
                image[i][j].rgbtRed = R5;
                image[i][j].rgbtGreen = G5;
                image[i][j].rgbtBlue = B5;
            }
            // CASE 6: PIXEL IN LAST ROW, 1ST COLUMN
            else if (i == height - 1 && j == 0)
            {
                int R6 = round((copy[i-1][j].rgbtRed   copy[i-1][j 1].rgbtRed   copy[i][j].rgbtRed   copy[i][j 1].rgbtRed)/4.0);
                int B6 = round((copy[i-1][j].rgbtBlue   copy[i-1][j 1].rgbtBlue   copy[i][j].rgbtBlue   copy[i][j 1].rgbtBlue)/4.0);
                int G6 = round((copy[i-1][j].rgbtGreen   copy[i-1][j 1].rgbtGreen   copy[i][j].rgbtGreen   copy[i][j 1].rgbtGreen)/4.0);
                image[i][j].rgbtRed = R6;
                image[i][j].rgbtGreen = G6;
                image[i][j].rgbtBlue = B6;
            }
            // CASE 7: Pixel IN LAST ROW, LAST COLUMN
            else if (i == height - 1 && j == width - 1)
            {
                int R7 = round((copy[i-1][j].rgbtRed   copy[i-1][j-1].rgbtRed   copy[i][j].rgbtRed   copy[i][j-1].rgbtRed)/4.0);
                int B7 = round((copy[i-1][j].rgbtBlue   copy[i-1][j-1].rgbtBlue   copy[i][j].rgbtBlue   copy[i][j-1].rgbtBlue)/4.0);
                int G7 = round((copy[i-1][j].rgbtGreen   copy[i-1][j-1].rgbtGreen   copy[i][j].rgbtGreen   copy[i][j-1].rgbtGreen)/4.0);
                image[i][j].rgbtRed = R7;
                image[i][j].rgbtGreen = G7;
                image[i][j].rgbtBlue = B7;
            }
            // CASE 8: PIXEL IN LAST ROW, NOT LAST / 1ST COLUMN
            else if (i == height - 1 && 0 < j < width - 1)
            {
                int R8 = round((copy[i][j-1].rgbtRed   copy[i][j].rgbtRed   copy[i][j 1].rgbtRed   copy[i-1][j-1].rgbtRed   copy[i-1][j].rgbtRed   copy[i-1][j 1].rgbtRed)/6.0);
                int G8 = round((copy[i][j-1].rgbtGreen   copy[i][j].rgbtGreen   copy[i][j 1].rgbtGreen   copy[i-1][j-1].rgbtGreen   copy[i-1][j].rgbtGreen   copy[i-1][j 1].rgbtGreen)/6.0);
                int B8 = round((copy[i][j-1].rgbtBlue   copy[i][j].rgbtBlue   copy[i][j 1].rgbtBlue   copy[i-1][j-1].rgbtBlue   copy[i-1][j].rgbtBlue   copy[i-1][j 1].rgbtBlue)/6.0);
                image[i][j].rgbtRed = R8;
                image[i][j].rgbtGreen = G8;
                image[i][j].rgbtBlue = B8;
            }
            // CASE 9: PIXEL IN LAST COLUMN, NOT 1ST / LAST ROW
            else if (0 < i < height - 1 && j == width - 1 )
            {
                int R9 = round((copy[i-1][j].rgbtRed   copy[i-1][j-1].rgbtRed   copy[i][j].rgbtRed   copy[i][j-1].rgbtRed   copy[i 1][j].rgbtRed   copy[i 1][j-1].rgbtRed)/6.0);
                int G9 = round((copy[i-1][j].rgbtBlue   copy[i-1][j-1].rgbtBlue   copy[i][j].rgbtBlue   copy[i][j-1].rgbtBlue   copy[i 1][j].rgbtBlue   copy[i 1][j-1].rgbtBlue)/6.0);
                int B9 = round((copy[i-1][j].rgbtGreen   copy[i-1][j-1].rgbtGreen   copy[i][j].rgbtGreen   copy[i][j-1].rgbtGreen   copy[i 1][j].rgbtGreen   copy[i 1][j-1].rgbtGreen)/6.0);
                image[i][j].rgbtRed = R9;
                image[i][j].rgbtGreen = G9;
                image[i][j].rgbtBlue = B9;
            }
        }
    }
}


At first, I thought that the problem was due to using the same variable names to store the new RGB values so I created unique variable names to store the RGB values for each of the 9 cases (eg. R1 R2 R3 ...). However, this was not the case.

CodePudding user response:

I think the main problem is that relational operators don't work how you hope.

Specifically; for a condition like 0 < i < height the compiler evaluates it from left to right, like (0 < i) < height, where the first part becomes true or false and the second part is comparing against true of false and not comparing against i (like TRUE < height).

To fix this, you have to use a boolean operator between the relational operators, like (0 < i) && (i < height).

As an alternative; you could delete all the branches and just do pixels in order; like:

    // Do top left pixel
    // Do middle pixels on top row
    for (int j = 1; j < width-1; j  ) {
    }
    // Do top right pixel

    // Do middle rows
    for (int i = 1; i < height-1; i  ) {
        // Do pixel on left edge
        // Do middle pixels
        for (int j = 1; j < width-1; j  ) {
        }
        // Do pixel on right edge
     }

    // Do bottom left pixel
    // Do middle pixels on bottom row
    for (int j = 1; j < width-1; j  ) {
    }
    // Do bottom right pixel
  • Related