0

I have written two different programs but none of them works can somebody please help

    int16_t gx[3] = {0, 0, 0};
    int16_t gy[3] = {0, 0, 0};
    int8_t matrix_gx[3][3] = {{-1,0,1},{-2,0,2},{-1,0,1}};
    int8_t matrix_gy[3][3] = {{-1,-2,-1},{0,0,0},{1,2,1}};

    for(int i = 0; i < height; i++)
    {
        for(int j = 0; j < width; j++)
        {
            for(int posx = -1; posx < 2; posx++) //-1 to +1 along x axis
            {
                for (int posy = -1; posy < 2; posy++) //-1 to +1 along y axis
                {
                    //should do 9 elements 3x3 =9
                    if (((posx+i) >= 0) && ((posy+j) >= 0) && ((posx+i) <= height) && ((posy+j) <= width))
                    {
                        gx[0] += image[i + posx][j + posy].rgbtRed   *  matrix_gx[posx + 1][posy + 1];
                        gx[1] += image[i + posx][j + posy].rgbtGreen *  matrix_gx[posx + 1][posy + 1];
                        gx[2] += image[i + posx][j + posy].rgbtBlue  *  matrix_gx[posx + 1][posy + 1];

                        gy[0] += image[i + posx][j + posy].rgbtRed   *  matrix_gy[posx + 1][posy + 1];
                        gy[1] += image[i + posx][j + posy].rgbtGreen *  matrix_gy[posx + 1][posy + 1];
                        gy[2] += image[i + posx][j + posy].rgbtBlue  *  matrix_gy[posx + 1][posy + 1];
                    }
                    else
                    {
                        //adding 0 for the pixels that dont exist
                        gx[0] += (BYTE) 0;
                        gx[1] += (BYTE) 0;
                        gx[2] += (BYTE) 0;

                        gy[0] += (BYTE) 0;
                        gy[1] += (BYTE) 0;
                        gy[2] += (BYTE) 0;
                    }
                }
                //printf("%i %i", (BYTE) gx[0], (BYTE) gy[0]);
                //printf(" %i %i", (BYTE) gx[1], (BYTE) gy[1]);
                //printf(" %i %i \n",(BYTE) gx[2], (BYTE) gy[2]);
            }

            int G[3] = {0, 0, 0};
            for (int o = 0; o < 3; o++)
            {
                G[o] = round(sqrt((gx[o] * gx[o]) + (gy[o] * gy[o])));
               // printf("%i\n",G[o]);
                if (G[o] > 256)
                {
                    G[o] = 255;
                }
                else
                {
                    G[o] = (BYTE) abs(G[o]);
                }
            }
            //printf("%i ", (BYTE) G[0]);
            image[i][j].rgbtRed = (BYTE) G[0];
            //printf(" %i", (BYTE) G[1]);
            image[i][j].rgbtGreen = (BYTE) G[1];
            //printf(" %i\n", (BYTE) G[2]);
            image[i][j].rgbtBlue = (BYTE) G[2];
        }
    }

This program gives output as a completely white image. I have tried diagnosing the error using printf(). And i see that the values in G[] array are absurd eg 30106, etc these are in binary of some other form. and when G[] is checked for overflow all of these values get set to 255 which gives me the white image. printf()output

228 58 36 208 188 218 
84 58 212 208 134 218 
95 97 237 87 153 153 
36365 //G[0] red pixel 
29458 //G[1] green pixel
6577  //G[2] blue pixel
255  255 255 // notice how the pixels are set to 255 due to the overflow statement

Second program: i have tried using only integers this time. I thought maybe there a conflict in using signed to unsigned conversions.

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

    int gx[3] = {0, 0, 0};
    int gy[3] = {0, 0, 0};
    for (int h = 0; h < height; h++)
    {
        for (int w = 0; w < width; w++)
        {
            for (int x = -1; x < 2; x++) // grid with x from p-1 to p+1
            {
                for (int y = -1; y < 2; y++) // grid with y from p-1 to p+1
                {
                    if ((h+x > 0)&&(h+x < height)&&(w+y > 0)&&(w+y < width))
                    {
                        //edge detection in x direction
                        //red color
                        gx[0] += image[h+x][w+y].rgbtRed * matrix_gx[x+1][y+1];
                        //green color
                        gx[1] += image[h+x][w+y].rgbtGreen * matrix_gx[x+1][y+1];
                        //blue color
                        gx[2] += image[h+x][w+y].rgbtBlue * matrix_gx[x+1][y+1];

                        //edge detection in the y direction
                        //red color
                        gy[0] += image[h+x][w+y].rgbtRed * matrix_gy[x+1][y+1];
                        //green color
                        gy[1] += image[h+x][w+y].rgbtGreen * matrix_gy[x+1][y+1];
                        //blue color
                        gy[2] += image[h+x][w+y].rgbtBlue * matrix_gy[x+1][y+1];
                    }
                    else
                    {
                        gx[0] = 0;
                        gx[1] = 0;
                        gx[2] = 0;

                        gy[0] = 0;
                        gy[1] = 0;
                        gy[2] = 0;
                    }
                }
            }
            int G[3] = {0,0,0};
            for (int i = 0; i < 3; i++)
            {
                G[i] = round(sqrt((float) (gx[i] * gx[i]) + (gy[i] * gy[i])));
                 //printf("%i ", G[i]);

                if(G[i] > 256)
                {
                    G[i] = 255;
                }
                //printf("%i\n", G[i]);
            }

            //assigning the pixels
            image[h][w].rgbtRed = (BYTE) G[0];
            image[h][w].rgbtGreen = (BYTE) G[1];
            image[h][w].rgbtBlue = (BYTE) G[2];
        }
    }

the output in this code is weird but i seem to have made some progress This is the output of the second code.

  • "//adding 0 for the pixels that dont exist" This makes no sense whatsoever. What do you mean exactly when you say that a pixel doesn't exist, and what adding zero is supposed to accomplish? – n. m. could be an AI Jun 10 '21 at 06:21
  • Well if this means "indices are outside the image boundaries" then still adding zero accomplishes nothing. Your other program has `gy[0] = 0` instead of `+= 0` but this doesn't seem right either. At any rate this only applies at the edges of the image which are not really important. There are two key errors. (1) You are trying to update the image in-place, this ain't gonna work, you are changing pixels that are not fully processed yet. Create a new image. (2) You initialise gx and gy outside of the loop, you need to do this for each pixel. – n. m. could be an AI Jun 10 '21 at 06:46

0 Answers0