ahmed gamal
ahmed gamal

Reputation: 17

CS50 Edge Detection bad results

I have been stuck on this for a couple of hours now every thing works fine and outputs the value it should but somehow something is wrong I even followed the program in a step by step and calculated the result of a 3 by 3 grid and I had the same results as the program outputs in a pic I took from the calculation sheet in excel and I have no idea whats wrong

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

    int b = 0, g = 0, r = 0,x = 0, y = 0, sumx[3], sumy[3],multi[3],sqr[3]; //arrays are in order of b g r
    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},
    };


    for (int h = 0 ; h < height ; h++)
    {
        for (int w = 0; w < width; w++)
        {
            for (int i = 0 ; i < 3 ; i++) // to reset the sum array
            {
                sumx[i] = 0;
                sumy[i] = 0;
            }

                y = 0;
                for (int i = h-1 ; i <= h+1 ; i++)
                {
                    x = 0;
                   for (int n = w-1 ; n <= w+1 ;n++)
                   {
                        if ( i >= 0 && i <= height && n >= 0 && n <= width)
                        {

                            b = image[i][n].rgbtBlue;
                            g = image[i][n].rgbtGreen;
                            r = image[i][n].rgbtRed;
                            sumx[0] += b*Gx[y][x];
                            sumx[1] += g*Gx[y][x];
                            sumx[2] += r*Gx[y][x];

                            sumy[0] += b*Gy[y][x];
                            sumy[1] += g*Gy[y][x];
                            sumy[2] += r*Gy[y][x];

                        }
                        ++x;
                   }
                   ++y;
                }

                multi[0] = pow(sumx[0],2)+pow(sumy[0],2);
                sqr[0] = round(sqrt(multi[0]));
                multi[1] = pow(sumx[1],2)+pow(sumy[1],2);
                sqr[1] = round(sqrt(multi[1]));
                multi[2] = pow(sumx[2],2)+pow(sumy[2],2);
                sqr[2] = round(sqrt(multi[2]));
                    for (int i = 0 ; i < 3 ;i++)          // to check if sqred is bigger than 255
                    {
                        if (sqr[i]>255)
                        {
                            sqr[i] = 255;
                        }
                    }
                image[h][w].rgbtBlue = sqr[0];
                image[h][w].rgbtGreen = sqr[1];
                image[h][w].rgbtRed = sqr[2];

        }

    }
    return;
}

enter image description here

enter image description here

Upvotes: -1

Views: 78

Answers (2)

ahmed gamal
ahmed gamal

Reputation: 17

for anyone who might face this problem in the future here is the mistake I made ... I figured out at the end that I was reading and writing to the same image not reading from a copy so I was reading values that I have already changed instead of reading the original values from one pic and applying results to the other

Upvotes: 0

Ken Y-N
Ken Y-N

Reputation: 15018

In the line:

if ( i >= 0 && i <= height && n >= 0 && n <= width)

That should be:

if ( i >= 0 && i < height && n >= 0 && n < width)

Otherwise you are going one too far past the right and bottom edges of the image.

Upvotes: 1

Related Questions