c - 霍夫变换: improving algorithm efficiency over OpenCL

标签 c opencl transform detection geometry

我正在尝试使用霍夫变换检测二值图像中的圆。

当我使用Opencv的内置函数进行圆霍夫变换时,没问题,我可以找到圆。

现在我尝试编写自己的“内核”代码来进行霍夫变换,但速度非常慢:

 kernel void hough_circle(read_only image2d_t imageIn, global int* in,const int w_hough,__global int * circle)
 {
     sampler_t sampler=CLK_NORMALIZED_COORDS_FALSE | CLK_ADDRESS_CLAMP_TO_EDGE | CLK_FILTER_NEAREST;
     int gid0 = get_global_id(0);
     int gid1 = get_global_id(1);
     uint4 pixel;
     int x0=0,y0=0,r;
     int maxval=0;
     pixel=read_imageui(imageIn,sampler,(int2)(gid0,gid1));
     if(pixel.x==255)
     {
     for(int r=20;r<150;r+=2)
     {
    // int r=100;

              for(int theta=0; theta<360;theta+=2)
              {

                              x0=(int) round(gid0-r*cos( (float) radians( (float) theta) ));
                            y0=(int) round(gid1-r*sin( (float) radians( (float) theta) ));
                           if((x0>0) && (x0<get_global_size(0)) && (y0>0)&&(y0<get_global_size(1)))
                            atom_inc(&in[w_hough*y0+x0]);
              }
              if(maxval<in[w_hough*y0+x0])
              {
              maxval=in[w_hough*y0+x0];
                circle[0]=gid0;
                circle[1]=gid1;
                circle[2]=r;
              }

              }

     }

 }

hough opencl 库和 opencv 都有源代码,但我很难提取对我有帮助的特定函数。

任何人都可以提供更好的源代码示例,或者帮助我理解为什么效率如此低下吗? 代码main.cpp和kernel.cl压缩在rar文件中http://www.files.com/set/527152684017e 使用opencv lib读取和显示图像>

最佳答案

重复调用 sin()cos() 的计算成本很高。由于您仅使用相同的 180 个 theta 值调用这些函数,因此您可以通过预先计算这些值并将它们存储在数组中来加快速度。

更可靠的方法是使用 midpoint circle algorithm通过简单的整数算术求出这些圆的周长。

关于c - 霍夫变换: improving algorithm efficiency over OpenCL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19667582/

相关文章:

c++ - 在 C/C++ 中通过套接字发送多条消息

c - 为什么宏函数ListView_GetSelectedColumn()不起作用?它什么也不返回

javascript - 为什么水平滚动条可见而垂直滚动条不可见?

html - 左侧边框上的 css 过渡适用于悬停但不适用于鼠标移出

objective-c - CGAffineTransform 身份和自动布局

c - 使用 line...file 指令

直接引用数字地址的 C 程序,这些进程物理上引用同一个地方?

ios - 在Mac上通过xCode使用OpenCL

c++ - OpenCl 代码在一台机器上工作,但我在另一台机器上得到 CL_INVALID_KERNEL_ARGS

c - 在 OpenCL 中切片字符串