0

I'm working on the edge filter in pset4 in CS50. The code compiles but when checking the code with check50 I get multiple errors. Looking closer at the output of the error check the first row and last column of the image always looks as though it's right but everything else is wrong.

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

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

    //define kernels
    int gx[3][3] = {{-1, 0, -1}, {-2, 0, 2}, {-1, 0, 1}};
    int gy[3][3] = {{-1, -2, -1}, {0, 0, 0}, {1, 2, 1}};

    //iterate pixels in image
    for (int i = 0; i < height; i++)
    {
        for (int j = 0; j < width; j++)
        {
            //initiate weighted averages
            int blueX = 0;
            int redX = 0;
            int greenX = 0;
            int blueY = 0;
            int redY = 0;
            int greenY = 0;

            //for each pixel, iterate the box it is in the center of
            for (int k = -1; k < 2; k++)
            {
                for (int l = -1; l < 2; l++)
                {
                    //if either coordinate of a pixel in the box is outside the image, skip it
                    if (i + k < 0 || i + k >= height || j + l < 0 || j + l >= width)
                    {
                        continue;
                    }

                    //add the weighted value of the pixel to the weighted average
                    blueX += imageCopy[i + k][j + l].rgbtBlue * gx[k + 1][l + 1];
                    blueY += imageCopy[i + k][j + l].rgbtBlue * gy[k + 1][l + 1];
                    redX += imageCopy[i + k][j + l].rgbtRed * gx[k + 1][l + 1];
                    redY += imageCopy[i + k][j + l].rgbtRed * gy[k + 1][l + 1];
                    greenX += imageCopy[i + k][j + l].rgbtGreen * gx[k + 1][l + 1];
                    greenY += imageCopy[i + k][j + l].rgbtGreen * gy[k + 1][l + 1];
                }
            }

            //calculate new RGB values
            int blue = round(sqrt(blueX * blueX + blueY * blueY));
            int red = round(sqrt(redX * redX + redY * redY));
            int green = round(sqrt(greenX * greenX + greenY * greenY));

            //cap new values and store them
            image[i][j].rgbtBlue = blue > 255 ? 255 : blue;
            image[i][j].rgbtRed = red > 255 ? 255 : red;
            image[i][j].rgbtGreen = green > 255 ? 255 : green;
        }
    }

    return;
}

1 Answer 1

1
int gx[3][3] = {{-1, 0, -1}, {-2, 0, 2}, {-1, 0, 1}};

The third number in this (-1) should be 1.

Which makes sense with the first row and last column being right, since that value wouldn't be used when calculating those pixels.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .