首页 > 解决方案 > cs50 pset 4 filter blur [only one test passes]

问题描述

My code for blur function in pset 4 filter is not passing the tests, as below.

There are other failed test cases, but I will be only posting the first two.

:( blur correctly filters middle pixel
expected "127 140 149\n", not "169 187 199\n"
Log testing with sample 3x3 image
first row: (10, 20, 30), (40, 50, 60), (70, 80, 90)
second row: (110, 130, 140), (120, 140, 150), (130, 150, 160)
third row: (200, 210, 220), (220, 230, 240), (240, 250, 255) 

running ./testing 3 0...
checking for output "127 140 149\n"...

Expected Output: 127 140 149
Actual Output: 169 187 199


:( blur correctly filters pixel on edge
expected "80 95 105\n", not "101 120 131\n"
Log testing with sample 3x3 image
first row: (10, 20, 30), (40, 50, 60), (70, 80, 90)
second row: (110, 130, 140), (120, 140, 150), (130, 150, 160)
third row: (200, 210, 220), (220, 230, 240), (240, 250, 255)

running ./testing 3 1...
checking for output "80 95 105\n"...

Expected Output: 80 95 105
Actual Output: 101 120 131


:) blur correctly filters pixel in corner
Log testing with sample 3x3 image
first row: (10, 20, 30), (40, 50, 60), (70, 80, 90)
second row: (110, 130, 140), (120, 140, 150), (130, 150, 160)
third row: (200, 210, 220), (220, 230, 240), (240, 250, 255)
running ./testing 3 2...
checking for output "70 85 95\n"...

What does my code do, and how does my algorithm work?

We are given a 2D array of struct type RGBTRIPLE and a variable called image, containing three properties, rgbtRed, rgbtBlue, rgbtGreen. .

For every pixel (=RGBTRIPLE) within the image, inside the 2D array, I check its adjacent pixels (=RGBTRIPLE) and itself to calculate the average colors.

(which is in a total of 9 pixels to be checked for every pixels inside the 2D array. If any of the pixels do NOT exist, I do NOT add them.)

If anyone has any idea why my algorithm is not working to calculate the image colors properly, please let me know.

I have been working on this for a week, re-reading my code several times, but still fail to figure out my bug.

Here is my code.

void blur(int height, int width, RGBTRIPLE image[height][width])
{
     for (int i = 0; i < height; i++)
     {
          for (int j = 0; j < width; j++)
          {
          int tmpRGB[3] = {0};
          addSelfAndAdjacent(i, j, height, width, image, tmpRGB);
          image[i][j].rgbtRed = tmpRGB[0];
          image[i][j].rgbtGreen = tmpRGB[1];
          image[i][j].rgbtBlue = tmpRGB[2];
          }
     }
return;
}

void addSelfAndAdjacent(int h, int w, int height, int width, RGBTRIPLE image[height][width], int *tmpRGB)
{
     // self, top, right, bottom, left, upperRight, lowerRight, lowerLeft, upperLeft
     int x[] = { 0, 0, 1, 0, -1, 1, 1, -1, -1};
     int y[] = { 0, -1, 0, 1, 0, -1, 1, 1, -1};
     int counter = 0;

          for (int i = 0; i < 9; i++)
          {
               if (isValid((h + y[i]), (w + x[i]), height, width))
               {
               counter++;
               *(tmpRGB + 0) += image[h + y[i]][w + x[i]].rgbtRed;
               *(tmpRGB + 1) += image[h + y[i]][w + x[i]].rgbtGreen;
               *(tmpRGB + 2) += image[h + y[i]][w + x[i]].rgbtBlue;
               }
          }

     *(tmpRGB + 0) = round(*(tmpRGB + 0) / counter);
     *(tmpRGB + 1) = round(*(tmpRGB + 1) / counter);
     *(tmpRGB + 2) = round(*(tmpRGB + 2) / counter);

     return;
}

bool isValid(int y, int x, int height, int width)
{
     if (y < 0 || y > height - 1 || x < 0 || x > width - 1)
     {
     return false;
     }

return true;
}

标签: cfiltercs50

解决方案


推荐阅读