Home > database >  CS50 Pset4 (Filter) - blur function in filter
CS50 Pset4 (Filter) - blur function in filter

Time:10-16

I am working on the CS50 problem set 4 and my code seemingly does blur the output image but I get this error when I run CHECK50.

The box blur works to an extent but it does not seem to fully apply to the image's far right edge and bottom edge. Here's the error code:

:) blur correctly filters middle pixel
:) blur correctly filters pixel on edge
:) blur correctly filters pixel in corner
:( blur correctly filters 3x3 image
    expected "70 85 95\n80 9...", not "70 85 95\n80 9..."
:( blur correctly filters 4x4 image
    expected "70 85 95\n80 9...", not "70 85 95\n80 9..."

And this is the code that I have used:

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

     RGBTRIPLE temp[height][width];

     for(int i = 0; i < height; i  )
     {
         for(int j = 0; j < width; j  )
         {
             float sumRGB[3] = {0,0,0};
             float counter = 0.00;
             temp[i][j] = image[i][j];

            // The box blur
             int p[3][3] = {{i-1, i-1, i-1}, {i, i, i}, {i 1, i 1,i 1}};
             int q[3][3] = {{j-1, j, j 1}, {j-1, j, j 1}, {j-1, j, j 1}};

            // Row x
             for(int x = 0; x < 3; x  )
             {
                // Collumn y
                 for(int y = 0; y < 3; y  )
                 {
                     if (p[x][y] >= 0 && q[x][y] >= 0)
                     {
                         sumRGB[0]  = image[p[x][y]][q[x][y]].rgbtRed;
                         sumRGB[1]  = image[p[x][y]][q[x][y]].rgbtGreen;
                         sumRGB[2]  = image[p[x][y]][q[x][y]].rgbtBlue;
                        counter  ;
                     }
                 }
             }

            temp[i][j].rgbtRed = round(sumRGB[0] / counter);
            temp[i][j].rgbtGreen = round(sumRGB[1] / counter);
            temp[i][j].rgbtBlue = round(sumRGB[2] / counter);

         }
     }

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

 }

CodePudding user response:

This line

if (p[x][y] >= 0 && q[x][y] >= 0)

checks the lower bounds but you have not checked the upper bounds. It should be

if (p[x][y] >= 0 && p[x][y] < height && q[x][y] >= 0 && q[x][y] < width)

Aside, note that you don't need to copy each struct member. You can copy the whole struct

image[i][j] = temp[i][j];
  • Related