首页 > 解决方案 > 边缘检测(Sobel算子)需要C帮助

问题描述

我已经编写了两个不同的程序,但它们都不起作用,有人可以帮忙吗

    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];
        }
    }

该程序将输出作为完全白色的图像。我尝试使用 printf() 诊断错误。而且我看到 G[] 数组中的值是荒谬的,例如 30106 等,这些都是其他形式的二进制。当检查 G[] 是否溢出时,所有这些值都设置为 255,这给了我白色图像。printf() 输出

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

第二个程序:这次我尝试只使用整数。我认为使用有符号到无符号转换可能存在冲突。

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];
        }
    }

这段代码的输出很奇怪,但我似乎取得了一些进展 这是第二个代码的输出。

标签: ccs50

解决方案


推荐阅读