Home > Blockchain >  CS50 Blur function
CS50 Blur function

Time:08-16

I am trying to create a blur function but it returns the wrong output. The function fails all the checks and I don't understand why:

:( blur correctly filters middle pixel
    expected "127 140 149\n", not "143 158 168\n"
:( blur correctly filters pixel on edge
    expected "80 95 105\n", not "96 114 126\n"
:( blur correctly filters pixel in corner
    expected "70 85 95\n", not "93 113 127\n"
:( blur correctly filters 3x3 image
    expected "70 85 95\n80 9...", not "93 113 127\n96..."
:( blur correctly filters 4x4 image
    expected "70 85 95\n80 9...", not "93 113 127\n96..."

I would be very grateful, if someone could check my code and help me identify the mistake:

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

    int offsetx[] = {0, 1, 1, 1, 0, -1, -1, -1};
    int offsety[] = {-1, -1, 0, 1, 1, 1, 0, -1};

    for (int row = 0; row < height; row  )
    {
        for (int col = 0; col < width; col  )
        {
            int sum_Red = 0;
            int sum_Green = 0;
            int sum_Blue = 0;
            int counter = 0;

            for(int i = 0; i < 9; i  )
            {
                int r = row   offsetx[i];
                int c = col   offsety[i];


                 if (r >= 0 && r < height && c >= 0 && c < width)
                 {
                    sum_Red  = image[r][c].rgbtRed;
                    sum_Green  = image[r][c].rgbtGreen;
                    sum_Blue  = image[r][c].rgbtBlue;
                    counter  ;

                 }
            }

            copy[row][col].rgbtRed = round(sum_Red / (double)counter);
            copy[row][col].rgbtGreen = round(sum_Green / (double)counter);
            copy[row][col].rgbtBlue = round(sum_Blue / (double)counter);
         }
    }

    for (int row = 0; row < height; row  )
    {
        for (int col = 0; col < width; col  )
        {
            image[row][col] = copy[row][col];
        }
    }
    return;
}

Thank you for your time and help.

CodePudding user response:

My question was already answered in the comments, but I will leave the answer here, in case someone missed it. After applying all the suggestions, it was still necessary to add one more entry for the offset arrays as follows:

  int offsetx[] = {0, 1, 1, 1, 0, -1, -1, -1, 0};
  int offsety[] = {-1, -1, 0, 1, 1, 1, 0, -1, 0};

  • Related