编程新手;试图实现一个索贝尔边缘检测算法。根据下面的代码片段,我在一个名为 edge 的函数中执行此操作。
library(testthat)
testthat::test_that("Some tests",{
testthat::expect_equal(1,2,info=paste('Test 1 failed at',Sys.time()))
testthat::expect_equal(1,1,info=paste('Test 2 failed at',sys.time()))
})
#> -- Failure (<text>:5:3): Some tests --------------------------------------------
#> 1 not equal to 2.
#> 1/1 mismatches
#> [1] 1 - 2 == -1
#> Test 1 failed at 2021-03-03 17:25:37
edges 函数调用sobel 函数,写成如下。
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];
}
如您所见,输出非常粗糙,边缘未定义。显然,我没有正确实施 sobel 算法,但我不确定我做错了什么。请你能给我一些关于如何解决这个问题的建议吗?
*请注意,我知道我可以在实施 sobel 算法之前将图像转换为灰度,但我正在尝试尽可能多地保留颜色通道。如果我能提供更多信息,请告诉我。
答案 0 :(得分: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]
数组。因此,当 x
或 y
为 -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++(以及许多,但不是所有其他编程语言)中,^
表示按位异或运算符,而不是求幂。你想要:
int n = sqrt(Gx*Gx + Gy*Gy);