ccs50sobel

Sobel operator 'Edges' function results in an all-white image


I think I've made it somewhat right, but it doesn't seem to work properly. Maybe it went wrong for the edges of picture of something else, I have no clue where the problem is.

It's nearly workable, just getting all-white image, problem somewhere in linking pixels with Gx/Gy arrays.

Expected output

Real output

Definition of Sobel

// Detect edges
void edges(int height, int width, RGBTRIPLE image[height][width])
{
    // Grid 3x3 then every Color * Gy * Gx, new color = G (sqrt(Gx^2 + Gy^2))
   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}};
   RGBTRIPLE temp[height][width]; //temp array

    for (int i = 0; i < height; i++)  //copy to temporary array
    {
        for (int j = 0; j < width; j++)
        {
            temp[i][j].rgbtRed = image[i][j].rgbtRed;
            temp[i][j].rgbtGreen = image[i][j].rgbtGreen;
            temp[i][j].rgbtBlue = image[i][j].rgbtBlue;
        }
    }

    for (int i = 0; i < height; i++)
    {
        for (int j = 0; j < width; j++) //going through array of pixels
        {
            signed int RedX = 0, GreenX = 0, BlueX = 0, RedY = 0, GreenY = 0, BlueY =0; //average color value = 0
            unsigned int TempG = 0;
            for (int k = (i - 1); k <= i + 1; k++) //boundaries of Sobel (3x3)
            {
                for( int l = (j - 1); l <= j + 1; l++) //boundaries
                {
                    if (k >= 0 && k <= (height - 1) && l >= 0 && l <= (width - 1)) //checking if pixel exists
                    {
                        RedX += (temp[k][l].rgbtRed * Gx[k - i][l - j]);  //Calculating value in X direction
                        GreenX += (temp[k][l].rgbtGreen * Gx[k - i][l - j]);
                        BlueX += (temp[k][l].rgbtBlue *  Gx[k - i][l - j]);
//problem here
                        RedY += (temp[k][l].rgbtRed * Gy[k - i][l - j]); // In Y direction
                        GreenY += (temp[k][l].rgbtGreen * Gy[k - i][l - j]);
                        BlueY += (temp[k][l].rgbtBlue *  Gy[k - i][l - j]);
                    }
                }
            }

         TempG = round (sqrt (pow (RedX, 2) + pow (RedY, 2))); //G = SquareRoot(Gx^2 + Gy^2)
         if (TempG > 255)
         {
            TempG = 255; //limiting color value
         }
         image[i][j].rgbtRed = TempG; //assigning


         TempG = round (sqrt (pow (GreenX, 2) + pow (GreenY, 2)));
          if (TempG > 255)
         {
            TempG = 255;
         }
            image[i][j].rgbtGreen = TempG;

         TempG = round (sqrt (pow (BlueX, 2) + pow (BlueY, 2)));
        if (TempG > 255)
         {
            TempG = 255;
         }
            image[i][j].rgbtBlue = TempG;
        }
    }
   return;
}

Solution

  • SOLVED WITH: As I made a loop for (k = (i - 1)) and for (l = (j - 1)) and went through Gx and Gy using same variable it started with value of -1, the variable RedX (for example) has been taking a value of element of Gx with index -1 i.e. Gx[-1][-1]. That is very stupid mistake, but it is what it is, so to solve this I've changed the index by which we call Gx and Gy elements: RedX = Gx[k - i + 1][l - j + 1]. Very appreciate a @Gerhardh help with that one, you're an angel :3. Correct output