0

I am working on Problem Set 4 of CS50 and I have some issue with Edges detection algorithm (description of the PSET4 can be found here: https://cs50.harvard.edu/x/2020/psets/4/filter/more/). In particular, my program modifies images correctly (at least, it seems that way). However, when I test it through check50 I see the following result: :) edges correctly filters middle pixel, :) edges correctly filters pixel on edge, :) edges correctly filters pixel in corner, :( edges correctly filters 3x3 image, :( edges correctly filters 4x4 image. I checked my code many times, but I could not figure out why my program did not filter correctly 3x3 image and 4x4 image. I will be very grateful for any suggestions concerning mistakes in my code.

// Detect edges
void edges(int height, int width, RGBTRIPLE image[height][width])
{
    RGBTRIPLE imageCopy[height][width];

    for (int i = 0; i < height; i++)
    {
         for (int j = 0; j < width; j++)
         {
             imageCopy[i][j] = image[i][j];
         }
    }


    for (int i = 0; i < height; i++)
    {
        int Gx[3]; //Gx[0] - Blue, Gx[1] - Green, Gx[2] - Red.
        int Gy[3]; ////Gy[0] - Blue, Gy[1] - Green, Gy[2] - Red.

        int new_i1; int new_j1; int new_i2; int new_j2; int new_j3; int new_i3; int squareRootGxGy[3];

        for (int j = 0; j < width; j++)

        {
            for (int p = 0; p < 3; p++)
            {
                squareRootGxGy[p] = 0;
                Gx[p] = 0;
                Gy[p] = 0;
            }

           for (int n = -1; n < 2; n++)

           {
              new_i1 = i + n; new_j1 = j + n;

              if (check_edge(height, width, new_i1, new_j1))
              {
                continue;
              }

                Gx[0] += imageCopy[new_i1][new_j1].rgbtBlue * n;
                Gx[1] += imageCopy[new_i1][new_j1].rgbtGreen * n;
                Gx[2] += imageCopy[new_i1][new_j1].rgbtRed * n;

                Gy[0] += imageCopy[new_i1][new_j1].rgbtBlue * n;
                Gy[1] += imageCopy[new_i1][new_j1].rgbtGreen * n;
                Gy[2] += imageCopy[new_i1][new_j1].rgbtRed * n;

           }


           for (int k = -1, s = 1; k < 2; k ++, s--)

           {
              new_i2 = i + k; new_j2 = j + s;

              if (check_edge (height, width, new_i2, new_j2))
              {
                continue;
              }

              Gx[0] += imageCopy[new_i2][new_j2].rgbtBlue * s;
              Gx[1] += imageCopy[new_i2][new_j2].rgbtGreen * s;
              Gx[2] += imageCopy[new_i2][new_j2].rgbtRed * s;

              Gy[0] += imageCopy[new_i2][new_j2].rgbtBlue * k;
              Gy[1] += imageCopy[new_i2][new_j2].rgbtGreen * k;
              Gy[2] += imageCopy[new_i2][new_j2].rgbtRed * k;

          }

         for (int k = -1, s = -2; k < 2; k ++, s += 2)

         {
            new_j3 = j + k; new_i3 = i + k;

           if (check_edge (height, width, i, new_j3))
           {
               continue;
           }

            Gx[0] += imageCopy[i][new_j3].rgbtBlue * s;
            Gx[1] += imageCopy[i][new_j3].rgbtGreen * s;
            Gx[2] += imageCopy[i][new_j3].rgbtRed * s;

            if (check_edge (height, width, new_i3, j))
            {
              continue;
            }

            Gy[0] += imageCopy[new_i3][j].rgbtBlue * s;
            Gy[1] += imageCopy[new_i3][j].rgbtGreen * s;
            Gy[2] += imageCopy[new_i3][j].rgbtRed * s;

        }

        for (int r = 0; r < 3; r++)
        {
          squareRootGxGy[r] = round((float)(sqrt((Gx[r]*Gx[r]) + (Gy[r]*Gy[r]))));
        }


        for (int m = 0; m < 3; m++)
        {
            if (squareRootGxGy[m] > 255)
            {
                squareRootGxGy[m] = 255;
            }

        }

        image[i][j].rgbtBlue = squareRootGxGy[0];
        image[i][j].rgbtGreen = squareRootGxGy[1];
        image[i][j].rgbtRed = squareRootGxGy[2];

    }
}

 return;
}

bool check_edge (int height, int width, int x, int y)
{
  return (x < 0 || x >= height || y < 0 || y >= width);
}

You must log in to answer this question.

Browse other questions tagged .