0

I've been trying to finished filter(more) all this morning, but I'm stuck with this code, I don't know why is incorrect, withcheck50 outputs in red:

:( edges correctly filters middle pixel
    expected "210 150 60\n", not "255 212 35\n"
:( edges correctly filters pixel on edge
    expected "213 228 255\n", not "198 198 156\n"
:( edges correctly filters pixel in corner
    expected "76 117 255\n", not "42 85 226\n"
:( edges correctly filters 3x3 image
    expected "76 117 255\n21...", not "42 85 226\n198..."
:( edges correctly filters 4x4 image
    expected "76 117 255\n21...", not "42 85 226\n198..."

And my code for Edges is:

bool isValidPixel(int i, int j, int height, int width)
{
    return i >= 0 && i < height && j >= 0 && j < width;
}

int cap(int value)
{
    return (value < 255) ? value : 255;
}

RGBTRIPLE getEdgedPixel(int i, int j, int height, int width, RGBTRIPLE image[height][width])
{
    int Gx[3][3] = {{-1, 0, 1}, {-2, 0, 2}, {-1, 0, 1}};
    int GxRed = 0;      int GyRed = 0;
    int GxGreen = 0;    int GyGreen = 0;
    int GxBlue = 0;     int GyBlue = 0;
    for(int k = -1; k <= 1; k++)
    {
        for(int l = -1; l <= 1; l++)
        {
            if(isValidPixel(i + k, j + l, height, width))
            {
            //Calculating Gx and Gy
                int x = Gx[k+1][l+1];
                GxRed += x * image[i+k][j+l].rgbtRed;
                GxGreen += x * image[i+k][j+l].rgbtGreen;
                GxBlue += x * image[i+k][j+l].rgbtBlue;

                int y = Gx[k+1][l+1];
                GyRed += y * image[i+k][j+l].rgbtRed;
                GyGreen += y * image[i+k][j+l].rgbtGreen;
                GyBlue += y * image[i+k][j+l].rgbtBlue;
            }
        }
    }
    RGBTRIPLE pixel;
    pixel.rgbtRed = cap(round(sqrt(GxRed*GxRed + GyRed*GyRed)));
    pixel.rgbtGreen = cap(round(sqrt(GxGreen*GxGreen + GyGreen*GyGreen)));
    pixel.rgbtBlue = cap(round(sqrt(GxBlue*GxBlue + GyBlue*GyBlue)));
    return pixel;
}

//Detect edges
void edges(int height, int width, RGBTRIPLE image[height][width])
{
    RGBTRIPLE GlOW[height][width];
    for(int i = 0; i < height; i++)
    {
        for(int j = 0; j < width; j++)
        {
            GlOW[i][j] = getEdgedPixel(i, j, height, width, image);
        }
    }
    for(int i = 0; i < height; i++)
        for(int j = 0; j < width; j++)
            image[i][j] = GlOW[i][j];
    return;
}
0

I found the mistake and now I submitted with all good in check50. Below the //Calculating Gx and Gy comment, I had to change Gx =[l+1][k+1]; I feel so satisfied

1
  • Hey! Good job on solving it! I'm doing it the 'spaghetti' way at the moment, and was checking cs50 stackexchg for inspiration on how to 'un-spaghetti' my codes. Appreciate if you could please update what you've answered here! Thanks! EDIT few seconds after commenting: I understand how you're doing it. I was doing it the same way except the long a** way. Apparently, there's a relation to Gx & Gy with the way you looped, same as i did. Thanks a lot!
    – domster
    May 26 '20 at 14:04

You must log in to answer this question.

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