【发布时间】:2021-12-09 09:36:12
【问题描述】:
我正在尝试制作一个检测数独网格的代码。这意味着我必须在图像上应用大量滤镜,然后在 Sobel 算法的输出之后对其应用霍夫变换算法(这使得边缘在黑色图像上显示为白色像素)。
我有点理解它的概念以及为什么使用Rho = x*cos(Theta) + y*sin(Theta) 来获取参数空间中的边坐标很重要。
但是,我遇到了 Rho 和我的累加器阵列的问题。 到目前为止,这是我的代码:
void hough(SDL_Surface* image_surface)
{
unsigned int width = image_surface->w;
unsigned int height = image_surface->h;
unsigned int Rhos, Thetas;
Rhos = sqrt(width * width + height * height);
Thetas = 180;
//initialise accumulator array
unsigned int acc_array[Rhos][Thetas];
for (size_t i = 0; i < Rhos; i++)
{
for (size_t j = 0; j < Thetas; j++)
acc_array[i][j] = 0;
}
Uint32 pixel;
Uint8 r, g, b;
//go through each pixels
for (size_t x = 0; x < width; x++)
{
for (size_t y = 0; y < height; y++)
{
pixel = get_pixel(image_surface, x, y);
SDL_GetRGB(pixel, image_surface->format, &r, &g, &b);
//if white
if (r+g+b == 765)
{
//p = x*cos(t) + y*sin(t)
//check for every t
for (int t = 0; t < 180;t++)
{
unsigned int p = x*cos(t) + y*sin(t);
acc_array[p][t]++;
}
}
}
}
//rest of the code below...
我的问题是,例如,当我在图像的像素 (20, 1882) 上并且我的 theta(或 t)= 4 时,p(或 Rho)变为 -1437。 如果 Rho 是负数,那么我不能增加它在累加器数组中的位置,因为索引不能是负数。
谁能帮我解决这个问题?
【问题讨论】:
-
您的示例代码中有不包含定义的组件,即结构参数
SDL_Surface、输入值、预期输出等。请编辑以包含可编译的缩小使用示例,这也会导致问题发生。 (minimal reproducible example) -
为什么
p需要成为索引?为什么不能将其线性转化为索引? -
我不确定我是否理解。我在网上看到的霍夫变换算法都有一个带有 p 和 theta 的累加器数组。抱歉,我对 C 代码有点陌生。
标签: c image image-processing sdl hough-transform