Home > Net >  blur edges function in cs50
blur edges function in cs50

Time:02-03

i know the code looks ugly but cleaning is for later i want it to work first then i could work on the clean design

i run it on check50 and it says that it can't blur edges, i try applying the same technique in a test file with constant values of red, green, blue and it works well

void blur(int height, int width, RGBTRIPLE image[height][width])
{
    RGBTRIPLE copy[height][width];

    for(int i = 0; i < height; i  )
    {
        for(int j = 0; j < width; j  )
        {
            blurcorner(i,j,height,width,image,copy);
            bluredge(i,j,height,width,image,copy);
            image[i][j] = copy[i][j];
        }
    }
    return;
}


////////
void blurcorner (int i, int j, int height, int width, RGBTRIPLE image[height][width],RGBTRIPLE copy[height][width])
{
    if (i == height - 1  && j == width - 1)
    {
        int redav = (image[i - 1][j].rgbtRed   image[i][j-1].rgbtRed   image[i-1][j-1].rgbtRed   image[i][j].rgbtRed) / 4;
        copy[i][j].rgbtRed = redav;
        int greenav = (image[i - 1][j].rgbtGreen   image[i][j-1].rgbtGreen   image[i-1][j-1].rgbtGreen   image[i][j].rgbtGreen) / 4;
        copy[i][j].rgbtGreen = greenav;
        int blueav = (image[i - 1][j].rgbtBlue   image[i][j-1].rgbtBlue   image[i-1][j-1].rgbtBlue   image[i][j].rgbtBlue) / 4;
        copy[i][j].rgbtBlue = blueav;
    }
    else if (i == height - 1  && j == 0)
    {
        int redav = (image[i - 1][j].rgbtRed   image[i][j 1].rgbtRed   image[i 1][j 1].rgbtRed   image[i][j].rgbtRed) / 4;
        copy[i][j].rgbtRed = redav;
        int greenav = (image[i - 1][j].rgbtGreen   image[i][j 1].rgbtGreen   image[i 1][j 1].rgbtGreen   image[i][j].rgbtGreen) / 4;
        copy[i][j].rgbtGreen = greenav;
        int blueav = (image[i - 1][j].rgbtBlue   image[i][j 1].rgbtBlue   image[i 1][j 1].rgbtBlue   image[i][j].rgbtBlue) / 4;
        copy[i][j].rgbtBlue = blueav;
    }
    else if (i == 0  && j == 0)
    {
        int redav = (image[i   1][j].rgbtRed   image[i][j 1].rgbtRed   image[i 1][j 1].rgbtRed   image[i][j].rgbtRed) / 4;
        copy[i][j].rgbtRed = redav;
        int greenav = (image[i   1][j].rgbtGreen   image[i][j 1].rgbtGreen   image[i 1][j 1].rgbtGreen   image[i][j].rgbtGreen) / 4;
        copy[i][j].rgbtGreen = greenav;
        int blueav = (image[i   1][j].rgbtBlue   image[i][j 1].rgbtBlue   image[i 1][j 1].rgbtBlue   image[i][j].rgbtBlue) / 4;
        copy[i][j].rgbtBlue = blueav;
    }
    else if (i == 0  && j == width - 1)
    {
        int redav = (image[i - 1][j].rgbtRed   image[i][j-1].rgbtRed   image[i-1][j-1].rgbtRed   image[i][j].rgbtRed) / 4;
        copy[i][j].rgbtRed = redav;
        int greenav = (image[i - 1][j].rgbtGreen   image[i][j-1].rgbtGreen   image[i-1][j-1].rgbtGreen   image[i][j].rgbtGreen) / 4;
        copy[i][j].rgbtGreen = greenav;
        int blueav = (image[i - 1][j].rgbtBlue   image[i][j-1].rgbtBlue   image[i-1][j-1].rgbtBlue   image[i][j].rgbtBlue) / 4;
        copy[i][j].rgbtBlue = blueav;
    }
}

void bluredge (int i, int j, int height, int width, RGBTRIPLE image[height][width],RGBTRIPLE copy[height][width])
{
    if (i == 0 && j > 0 && j < width - 1)
    {
        int redav = (image[i][j].rgbtRed   image[i][j-1].rgbtRed   image[i 1][j-1].rgbtRed   image[i 1][j].rgbtRed   image[i 1][j 1].rgbtRed   image[i][j 1].rgbtRed) / 6;
        copy[i][j].rgbtRed = redav;
        int greenav = (image[i][j].rgbtGreen   image[i][j-1].rgbtGreen   image[i 1][j-1].rgbtGreen   image[i 1][j].rgbtGreen   image[i 1][j 1].rgbtGreen   image[i][j 1].rgbtGreen) / 6;
        copy[i][j].rgbtGreen = greenav;
        int blueav = (image[i][j].rgbtBlue   image[i][j-1].rgbtBlue   image[i 1][j 1].rgbtBlue   image[i 1][j].rgbtBlue   image[i 1][j 1].rgbtBlue   image[i][j 1].rgbtBlue) / 6;
        copy[i][j].rgbtBlue = blueav;
    }
    else if (i == height - 1 && j > 0 && j < width - 1)
    {
        int redav = (image[i][j].rgbtRed   image[i][j-1].rgbtRed   image[i-1][j-1].rgbtRed   image[i-1][j].rgbtRed   image[i-1][j 1].rgbtRed   image[i][j 1].rgbtRed) / 6;
        copy[i][j].rgbtRed = redav;
        int greenav = (image[i][j].rgbtGreen   image[i][j-1].rgbtGreen   image[i-1][j-1].rgbtGreen   image[i-1][j].rgbtGreen   image[i-1][j 1].rgbtGreen   image[i][j 1].rgbtGreen) / 6;
        copy[i][j].rgbtGreen = greenav;
        int blueav = (image[i][j].rgbtBlue   image[i][j-1].rgbtBlue   image[i-1][j-1].rgbtBlue   image[i-1][j].rgbtBlue   image[i-1][j 1].rgbtBlue   image[i][j 1].rgbtBlue) / 6;
        copy[i][j].rgbtBlue = blueav;
    }
    else if (i > 0 && i < height - 1 && j == width - 1)
    {
        int redav = (image[i][j].rgbtRed   image[i-1][j].rgbtRed   image[i 1][j].rgbtRed   image[i 1][j-1].rgbtRed   image[i][j - 1].rgbtRed   image[i-1][j-1].rgbtRed) / 6;
        copy[i][j].rgbtRed = redav;
        int greenav = (image[i][j].rgbtGreen   image[i-1][j].rgbtGreen   image[i 1][j].rgbtGreen   image[i 1][j-1].rgbtGreen   image[i][j-1].rgbtGreen   image[i-1][j-1].rgbtGreen) / 6;
        copy[i][j].rgbtGreen = greenav;
        int blueav = (image[i][j].rgbtBlue   image[i-1][j].rgbtBlue   image[i 1][j].rgbtBlue   image[i 1][j-1].rgbtBlue   image[i][j-1].rgbtBlue   image[i-1][j-1].rgbtBlue) / 6;
        copy[i][j].rgbtBlue = blueav;
    }
    else if (i > 0 && i < height - 1 && j == 0)
    {
        int redav = (image[i][j].rgbtRed   image[i-1][j].rgbtRed   image[i 1][j].rgbtRed   image[i 1]
[j 1].rgbtRed   image[i][j 1].rgbtRed   image[i-1][j 1].rgbtRed) / 6;
        copy[i][j].rgbtRed = redav;
        int greenav = (image[i][j].rgbtGreen   image[i-1][j].rgbtGreen   image[i 1][j].rgbtGreen   image[i 1][j 1].rgbtGreen   image[i][j 1].rgbtGreen   image[i-1][j 1].rgbtGreen) / 6;
        copy[i][j].rgbtGreen = greenav;
        int blueav = (image[i][j].rgbtBlue   image[i-1][j].rgbtBlue   image[i 1][j].rgbtBlue   image[i 1][j 1].rgbtBlue   image[i][j 1].rgbtBlue   image[i-1][j 1].rgbtBlue) / 6;
        copy[i][j].rgbtBlue = blueav;
    }
    else
    {
        return;
    }
}

before you start hating on me the blur corners function is similar and it worked

i tried changing how i adress each pixel many times but it didnt work

here is the error i get:

:( blur correctly filters pixel on edge
    expected "80 95 105\n", not "80 95 108\n"

i think the problem is with the blue

CodePudding user response:

in the blue of the first if in the bluredge:

int blueav = (image[i][j].rgbtBlue   image[i][j-1].rgbtBlue   image[i 1][j 1].rgbtBlue   image[i 1][j].rgbtBlue   image[i 1][j 1].rgbtBlue   image[i][j 1].rgbtBlue) / 6;
    copy[i][j].rgbtBlue = blueav;

the value image[i 1][j 1] is there twice, one of them needs to be edited into image[i 1][j-1]

CodePudding user response:

Because it wouldn't be right to write working code for you that you hadn't already tried to write for yourself, the following is to illustrate the advantages of pausing the "advance" to improve on the technique. Here is a version of most of your blurcorner() showing how you could make your code more readable and robust. Trying to find an errant ' ' that should be a '-' in the OP code is tantamount to looking for a needle in a haystack. Perhaps you will consider this before spending too much time struggling to make a poor implementation work.

void blurcorner( int i, int j, int height, int width,
    RGBTRIPLE image[][width], RGBTRIPLE copy[][width])
{
    RGBTRIPLE u0, u1, u2, u3;

    u0 = image[i][j];
    if (i == 0 ) { // Top
        u1 = image[ i   1 ][ j ];
        int off_j = ( j == 0 ) ? j   1 : j - 1;
        u2 = image[ i     ][ off_j ];
        u3 = image[ i   1 ][ off_j ];
    } else { // Bottom
        // left as an exercise
    }

    int red = u0.rgbtRed     u1.rgbtRed     u2.rgbtRed     u3.rgbtRed  ;
    int grn = u0.rgbtGreen   u1.rgbtGreen   u2.rgbtGreen   u3.rgbtGreen;
    int blu = u0.rgbtBlue    u1.rgbtBlue    u2.rgbtBlue    u3.rgbtBlue ;

    copy[i][j].rgbtRed   = red / 4;
    copy[i][j].rgbtGreen = grn / 4;
    copy[i][j].rgbtBlue  = blu / 4;
}

(The integer division problem has not been addressed here.)

  • Related