【问题标题】:Implementing sobel edge detection algorithm实现sobel边缘检测算法
【发布时间】:2021-06-02 03:39:45
【问题描述】:

刚接触编程;试图实现一个sobel边缘检测算法。根据下面的代码 sn-p,我在一个名为 edges 的函数中执行此操作。

void edges(int height, int width, RGBTRIPLE image[height][width])
{
    // define kernals
    int Gx[3][3];
    Gx[0][0] = -1;
    Gx[0][1] = 0;
    Gx[0][2] = 1;
    Gx[1][0] = -2;
    Gx[1][1] = 0;
    Gx[1][2] = 2;
    Gx[2][0] = -1;
    Gx[2][1] = 0;
    Gx[2][2] = 1;

    int Gy[3][3];
    Gy[0][0] = -1;
    Gy[0][1] = -2;
    Gy[0][2] = -1;
    Gy[1][0] = 0;
    Gy[1][1] = 0;
    Gy[1][2] = 0;
    Gy[2][0] = 1;
    Gy[2][1] = 2;
    Gy[2][2] = 1;

    // define variables
    int Gx_red, Gx_green, Gx_blue;
    int Gy_red, Gy_green, Gy_blue;

    // define temporary array
    RGBTRIPLE edge_image[height][width];

    // loop through columns
    for (int i = 0; i < height; i++)
    {
        // loop through rows
        for (int j = 0; j < width; j++)
        {
            // set/reset sobel values for each colour channel
            Gx_red = Gx_green = Gx_blue = 0;
            Gy_red = Gy_green = Gy_blue = 0;

            // 3x3 kernal around [i][j]
            for (int x = -1; x < 2; x++)
            {
                for (int y = -1; y < 2; y++)
                {
                    // 'pixels' outside of image array treated as black pixels
                    if (i + x > height || i + x < 0 || j + y > width || j + y < 0)
                    {
                        // Gx kernal
                        Gx_red += 0;
                        Gx_green += 0;
                        Gx_blue += 0;

                        // Gy kernal
                        Gy_red += 0;
                        Gy_green += 0;
                        Gy_blue += 0;
                    }

                    // Multiply each channel by corresponding value in convolutional array
                    else if (i + x < height && i + x > 0 && j + y < width && j + y > 0)
                    {
                        // Gx kernal
                        Gx_red += (image[i + x][j + y].rgbtRed * Gx[x][y]);
                        Gx_green += (image[i + x][j + y].rgbtGreen * Gx[x][y]);
                        Gx_blue += (image[i + x][j + y].rgbtBlue * Gx[x][y]);

                        // Gy kernal
                        Gy_red += (image[i + x][j + y].rgbtRed * Gy[x][y]);
                        Gy_green += (image[i + x][j + y].rgbtGreen * Gy[x][y]);
                        Gy_blue += (image[i + x][j + y].rgbtBlue * Gy[x][y]);
                    }
                }
            }
            // Perform sobel operatation and assign each colour channel value to new array
            edge_image[i][j].rgbtRed = sobel(Gx_red, Gy_red);
            edge_image[i][j].rgbtGreen = sobel(Gx_green, Gy_green);
           edge_image[i][j].rgbtBlue = sobel(Gx_blue, Gy_blue);
        }
    }
    // assign temp array to origional array for output
    for (int i = 0; i < height; i++)
        for (int j = 0; j < width; j++)
            image[i][j] = edge_image[i][j];
}

edges函数调用sobel函数,写法如下。

// Calculate sobel value for each channel (capping at 255)
int sobel (int Gx, int Gy)
{
    int n = sqrt(Gx^2 + Gy^2);
    if (n > 255)
    {
        n = 255;
    }
    return n;
}

这是Input image

这是Output image

如您所见,输出非常粗糙,边缘未定义。显然,我没有正确实施 sobel 算法,但我不确定我做错了什么。请你给我一些关于如何解决这个问题的建议吗?

*请注意,我知道我可以在实施 sobel 算法之前将图像转换为灰度,但我正在尝试尽可能地保留颜色通道。如果我能提供更多信息,请告诉我。

【问题讨论】:

  • 你可以用int Gx[3][3] = { { -1, 0 ,1}, {-2, 0, 2}, {-1, 0 , 2}};清理你的初始化
  • 啊,我知道有办法做到这一点 - 干杯!

标签: c detection sobel


【解决方案1】:

首先,这一切都无济于事:

// 'pixels' outside of image array treated as black pixels
if (i + x > height || i + x < 0 || j + y > width || j + y < 0)
{
    // Gx kernal
    Gx_red += 0;
    Gx_green += 0;
    Gx_blue += 0;

    // Gy kernal
    Gy_red += 0;
    Gy_green += 0;
    Gy_blue += 0;
}

加零是无操作的。您可以放心地删除它。

您的第一个主要问题在这里:

// Gx kernal
Gx_red += (image[i + x][j + y].rgbtRed * Gx[x][y]);
Gx_green += (image[i + x][j + y].rgbtGreen * Gx[x][y]);
Gx_blue += (image[i + x][j + y].rgbtBlue * Gx[x][y]);

// Gy kernal
Gy_red += (image[i + x][j + y].rgbtRed * Gy[x][y]);
Gy_green += (image[i + x][j + y].rgbtGreen * Gy[x][y]);
Gy_blue += (image[i + x][j + y].rgbtBlue * Gy[x][y]);

您将x, y 定义为循环通过-1, 0, 1,但将Gx, Gy 定义为int[3][3] 数组。因此,当 xy-1 时,您的索引超出范围。

我建议定义

int kx = x + 1; // Kernel x.
int ky = y + 1; // Kernel y.

然后

// Gx kernel
Gx_red += (image[i + x][j + y].rgbtRed * Gx[kx][sy]);
Gx_green += (image[i + x][j + y].rgbtGreen * Gx[kx][sy]);
Gx_blue += (image[i + x][j + y].rgbtBlue * Gx[kx][sy]);

// Gy kernel
Gy_red += (image[i + x][j + y].rgbtRed * Gy[kx][sy]);
Gy_green += (image[i + x][j + y].rgbtGreen * Gy[kx][sy]);
Gy_blue += (image[i + x][j + y].rgbtBlue * Gy[kx][sy]);

您的第二个主要问题在这里:

int n = sqrt(Gx^2 + Gy^2);

在 C 和 C++(以及许多,但不是所有其他编程语言)中,^ 表示按位 XOR 运算符,而不是求幂。你想要:

int n = sqrt(Gx*Gx + Gy*Gy);

【讨论】:

  • 感谢您的回复,您所说的一切都很有道理。我已经实施了这些更改,一切似乎都有效。再次感谢,一切顺利。
  • @RoryV.S.如果我的回答对您有帮助,请考虑支持并接受它:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-02-25
  • 2013-10-20
  • 2020-04-06
  • 1970-01-01
  • 2014-10-17
  • 2021-10-07
  • 2016-10-03
相关资源
最近更新 更多