# HOUGH变换检测圆

6 篇文章 0 订阅

#include<cv.h>
#include <highgui.h>
#include <math.h>
#include <iostream>
using namespace std;
//搜索最大概率圆
int selectmax(int count_1[],int a0,int b0,int r0)
{
int time,max,temp;
time=a0*b0*r0;
max=count_1[0];
temp=0;
for(int i=0;i<time;i++)
{
if(count_1[i]>max)
{max=count_1[i];temp=i;}
}
return temp;
}
int main(){
IplImage * image, *image2;
cvNamedWindow("image",1);
cvShowImage("image",image);
image2 = cvCreateImage(cvSize(image->width,image->height),image->depth,1);
int i ,j,index_1;
int temp = 0;
unsigned char *ptr, *dst;
int a = 0,b = 0,radius = 0;
//累加器
int A0 =   image->height;
int B0 =   image->width;
int R0 = (image->width > image->height)? 2*image->width : 2*image->height;//R0取长宽的最大值的2倍
int countLength = A0*B0*R0;
int * count = new int[countLength];

int index  = A0 * B0 *radius + A0*b + a;
for (i= 0;i<countLength;i++)
{
count[i]=0;
}
for (i = 0 ; i< image->height; i++)
{
for (j = 0 ; j< image->width ; j++)
{
ptr = (unsigned char *)image->imageData + i*image->widthStep + j;
if (*ptr == 0 )
{
for (a = 0 ; a< A0;a++)
{
for (b=0;b<B0;b++)
{
index  = A0 * B0 *radius + A0*b + a;

count[index]++;

temp=count[index];
}
}
}
}
printf("%d\n",i);
}

for (i=0;i<image2->height;i++)
{
for (j=0;j<image2->width;j++)
{
dst=(unsigned char*)image2->imageData+i*image->widthStep+j;
*dst=0;
}
}
index_1=selectmax(count,A0,B0,R0);

for (a = 0 ; a < A0 ; a++)
{
for (b = 0 ; b< B0; b++)
{
{
index  = A0 * B0 *radius + A0*b + a;
if (count[index]==count[index_1])
{
printf("a: %d  b:  %d  r:  %d  num:  %d\n",a,b,radius,count[index_1]);
//在image2中绘制该圆
for (j = 0 ; j< image->width;j++)
{
i = (int)(sqrt(pow((double)radius,2)- pow((double)(j-b),2)) + a);
if ((i< image2->height)  && (i >= 0))
{
dst = (unsigned char*)(image2->imageData + i * image2->widthStep + j);
*dst = 255;
}
//i有两个值，
if ((i< image2->height)  && (i >= 0))
{
dst = (unsigned char*)(image2->imageData + i * image2->widthStep + j);
*dst = 255;
}

}
}
}
}
}
cvNamedWindow("image2",1);
cvShowImage("image2",image2);
cvSaveImage("E:\\image\\circleHough2.bmp",image2);
cvWaitKey(0);
return  0 ;
}



• 2
点赞
• 4
收藏
觉得还不错? 一键收藏
• 打赏
• 0
评论
07-25
11-05 3万+
04-23 8542
11-12 8561
01-23
03-01 3万+
09-29 7324
06-08 5818
09-10
01-12
05-26
12-06 2612

### “相关推荐”对你有帮助么？

• 非常没帮助
• 没帮助
• 一般
• 有帮助
• 非常有帮助

¥1 ¥2 ¥4 ¥6 ¥10 ¥20

1.余额是钱包充值的虚拟货币，按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载，可以购买VIP、付费专栏及课程。