Wrong value in Edges-filter in cs50

15 Views Asked by At

I'm stuck with the edges case. I think I'm missing something subtle. All the first few values are correct but when it gets further into the image, it all returns the wrong values. I think all of the logic and flow is correct but the way that I'm manipulating the image is the problem.

void edges(int height, int width, RGBTRIPLE image[height][width])
{
RGBTRIPLE store[height][width];
RGBTRIPLE gx[height][width];
RGBTRIPLE gy[height][width];

float ared = 0;
float ablue = 0;
float agreen = 0;

for (int i = 0; i < height; i++)
{
    for (int j = 0; j < width; j++)
    {
        store[i][j] = image[i][j];
    }
}
for (int i = 0; i < height; i++)
{
    for (int j = 0; j < width; j++)
    {
        get_gx(height, width, i, j, store, image, gx);
        get_gy(height, width, i, j, store, image, gy);
    }
}

for (int i = 0; i < height; i++)
{
    for (int j = 0; j < width; j++)
    {
        ared = round(sqrt((float)pow(gx[i][j].rgbtRed,2) + (float)pow(gy[i][j].rgbtRed,2)));
        ablue = round(sqrt((float)pow(gx[i][j].rgbtBlue,2) + (float)pow(gy[i][j].rgbtBlue,2)));
        agreen = round(sqrt((float)pow(gx[i][j].rgbtGreen,2) + (float)pow(gy[i][j].rgbtGreen,2)));

        ared = fmin (ared, 255);
        ared = fmax(ared, 0);
        ablue = fmax(ablue, 0);
        ablue = fmin (ablue, 255);
        agreen = fmax(agreen, 0);
        agreen = fmin (agreen, 255);

        image[i][j].rgbtRed = (int)ared;
        image[i][j].rgbtBlue = (int)ablue;
        image[i][j].rgbtGreen = (int)agreen;
    }
}
return;
}

void get_gx (int height, int width, int i, int j, RGBTRIPLE store[height][width], 
RGBTRIPLE gx[height][width] )
{
int gxred = 0;
int gxblue = 0;
int gxgreen = 0;

int matrix [3][3] = {{-1,0,1}, {-2,0,2}, {-1,0,1}};
int r = 0;
int c = 0;

for ( int row = i - 1; row < i+2; row++)
{
    for (int col = j -1; col < j +2; col++)
        {

            if ( row >= height || row < 0 || col >= width || col < 0)
            {
                c += 1;
                continue;
            }

            gxred = gxred + (store[row][col].rgbtRed * matrix[r][c]) ;
            gxblue = gxblue + (store[row][col].rgbtBlue * matrix[r][c]);
            gxgreen = gxgreen + (store[row][col].rgbtGreen * matrix[r][c]);

            c += 1;
        }
    c = 0;
    r += 1;
}
gxred = round(gxred);
gxblue = round(gxblue);
gxgreen = round(gxgreen);


gxred = fmin (gxred, 255);
gxred = fmax(gxred, 0);
gxblue = fmax(gxblue, 0);
gxblue = fmin (gxblue, 255);
gxgreen = fmax(gxgreen, 0);
gxgreen = fmin (gxgreen, 255);

gx[i][j].rgbtRed = gxred;
gx[i][j].rgbtBlue = gxblue;
gx[i][j].rgbtGreen = gxgreen;

return;
}


void get_gy (int height, int width, int i, int j, RGBTRIPLE store[height][width], 
RGBTRIPLE image[height][width],RGBTRIPLE gy[height][width] )
{
int gyred = 0;
int gyblue = 0;
int gygreen = 0;

int matrix [3][3] = {{-1,-2,-1}, {0,0,0}, {1,2,1}};
int r = 0;
int c = 0;

for ( int row = i - 1; row < i+2; row++)
{
    for (int col = j -1; col < j +2; col++)
        {
            if ( row >= height || row < 0 || col >= width || col < 0)
            {
                c += 1;
                continue;
            }

            gyred = gyred + (store[row][col].rgbtRed * matrix[r][c]) ;
            gyblue = gyblue + (store[row][col].rgbtBlue * matrix[r][c]) ;
            gygreen = gygreen + (store[row][col].rgbtGreen * matrix[r][c]) ;

            c += 1;
        }
    c = 0;
    r += 1;
}

gyred = round(gyred);
gyblue = round(gyblue);
gygreen = round(gygreen);

gyred = fmax(gyred, 0);
gyred = fmin (gyred, 255);
gyblue = fmax(gyblue, 0);
gyblue = fmin (gyblue, 255);
gygreen = fmax(gygreen, 0);
gygreen = fmin (gygreen, 255);

gy[i][j].rgbtRed = gyred;
gy[i][j].rgbtBlue = gyblue;
gy[i][j].rgbtGreen = gygreen;

return;

when i run check50, it only shows that edges and corners are correct

:( edges correctly filters middle pixel
expected "210 150 60\n", not "210 150 25\n"
:) edges correctly filters pixel on edge
:) edges correctly filters pixel in corner
:( edges correctly filters 3x3 image
expected "76 117 255\n21...", not "76 117 255\n21..."
:( edges correctly filters 4x4 image
expected "76 117 255\n21...", not "76 117 255\n21..."
0

There are 0 best solutions below