HOUGH变换检测圆

本文的算法是实现HOUGH变换对圆的检测
 
#include  
#include  
#include   
#include   
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;imax)
		{max=count_1[i];temp=i;}
	}
	return temp;
}
int main(){ 
   IplImage * image, *image2; 
   image = cvLoadImage("G:\\guangshan_2.jpg",0);  
   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;  
 //参数空间的参数 圆心O(a,b) 半径radius  
    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;iheight; 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;bheight;i++)  
	   {  
            for (j=0;jwidth;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++)  
          {  
              for (radius = 0 ; radius < R0; radius++)  
              {  
                   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有两个值,  
                          i = a -(int)(sqrt(pow((double)radius,2)- pow((double)(j-b),2)));  
                          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 ;  
}

			 




 

你可能感兴趣的:(数字图像处理)