首页 > 解决方案 > C语言中的霍夫变换有问题

问题描述

我正在尝试制作一个检测数独网格的代码。这意味着我必须在图像上应用大量过滤器,然后在 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 是负数,那么我不能增加它在累加器数组中的位置,因为索引不能是负数。

谁能帮我解决这个问题?

标签: cimageimage-processingsdlhough-transform

解决方案


cos() 和 sin() 函数期望角度以弧度为单位。根据您的 for 循环范围,它看起来 t 是以度为单位的值。


推荐阅读