我正在尝试使用霍夫变换检测二值图像中的圆。
当我使用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/