hough擬合圓弧

        圓弧檢測之前都需要對數據進行分割,將一系列的點分割成不同的區域,然後計算圓弧的位置。Hough變換

不需要知道某區域是否有圓弧,以類似於投票的機制,某參數獲得的票數越多,則存在圓弧的可能性越大,

大於某閾值時,則可以認爲該處存在圓弧。

x - a = r*cos(theta)

y - b = r*sin(theta)

       對於每一個點x y,有無數個點滿足上式,即有無數個圓在經過該點的,每個圓對應一組(a,b,r)參數,設立一

個票箱,裏面有所有可能的(a,b,r)參數,當某組參數出現一次,就將該票箱中的票數加1,所有的點都掃描之

後,查看票箱,票數最多的點即是圓出現概率最大的情況。此時應該設定一個閾值,如果最多的票數小於該閾值,

則認爲不存在圓,否則認爲有圓存在。

//這是一個簡化的Hough圓算法,假設半徑已知的情況,

  1. int HoughArc(int X[] , int Y[] , int Cnt ,int r, ArcPara * Arc){  
  2.     vector<iPoint>center;  
  3.     vector<int>VoteCnt;  
  4.     double theta;  
  5.     int a,b;  
  6.     int minA,maxA,minB,maxB;  
  7.     int VotedFlag = 0;  
  8.     double deltaTheta = PI/180;//間隔1度  
  9.     double startAngle = 150.0*PI/180;  
  10.     double endAngle = PI*2 + PI/6;  
  11.     center.clear();  
  12.     VoteCnt.clear();  
  13.     minA = maxA = X[0] - r;  
  14.     minB = maxB = X[0]; //theta = 0  
  15.     //計算a,b的最小和最大值  
  16.     for (int i = 0; i < Cnt;i++)  
  17.     {  
  18.         for (theta = startAngle; theta < endAngle;theta += deltaTheta)  
  19.         {  
  20.             a = (int)(X[i] - r*cos(theta) + 0.5);  
  21.             b = (int)(Y[i] - r*sin(theta) + 0.5);  
  22.             if (a > maxA)  
  23.             {  
  24.                 maxA = a;  
  25.             }else if (a < minA)  
  26.             {  
  27.                 minA = a;  
  28.             }  
  29.   
  30.             if (b > maxB)  
  31.             {  
  32.                 maxB = b;  
  33.             }else if (b < minB)  
  34.             {  
  35.                 minB = b;  
  36.             }  
  37.   
  38.         }  
  39.     }  
  40.     //確定a,b的範圍之後,即確定了票箱的大小  
  41.     int aScale = maxA - minA + 1;  
  42.     int bScale = maxB - minB + 1;  
  43.   
  44.     int *VoteBox = new int[aScale*bScale];  
  45.     //VoteBox初始化爲0  
  46.     for (int i = 0; i < aScale*bScale;i++)  
  47.     {  
  48.         VoteBox[i] = 0;  
  49.     }  
  50.     //開始投票  
  51.     for (int i = 0; i < Cnt;i++)  
  52.     {  
  53.         //printf("%d  ",i);  
  54.         for (theta = startAngle; theta < endAngle;theta += deltaTheta)  
  55.         {  
  56.   
  57.             a = (int)(X[i] - r*cos(theta) + 0.5);  
  58.             b = (int)(Y[i] - r*sin(theta) + 0.5);     
  59.             VoteBox[(b - minB)*aScale + a - minA] = VoteBox[(b - minB)*aScale + a - minA] + 1;  
  60.         }  
  61.     }  
  62.   
  63.     //篩選票箱  
  64.     int VoteMax = 0;  
  65.     int VoteMaxX,VoteMaxY;  
  66.     for (int i = 0; i < bScale ;i++)  
  67.     {  
  68.         for (int j = 0; j < aScale ;j++)  
  69.         {  
  70.             if (VoteBox[i*aScale + j] > VoteMax)  
  71.             {  
  72.                 VoteMax = VoteBox[i*aScale + j];  
  73.                 VoteMaxY = i;  
  74.                 VoteMaxX = j;  
  75.             }  
  76.         }  
  77.     }  
  78.       
  79.     int Count = 0;  
  80.     printf("VoteMax: %d",VoteMax);  
  81.     for (int i = 0; i < bScale ;i++)  
  82.     {  
  83.         for (int j = 0; j < aScale ;j++)  
  84.         {  
  85.             if (VoteBox[i*aScale + j] >= VoteMax)  
  86.             {  
  87.                 Count++;  
  88.             }  
  89.         }  
  90.     }  
  91.     printf("   %d \n",Count);  
  92.     //釋放內存  
  93.     delete [] VoteBox;  
  94.     if (VoteMax > 3)  
  95.     {  
  96.         Arc->center.x = VoteMaxX + minA;  
  97.         Arc->center.y = VoteMaxY + minB;  
  98.         Arc->r = r;  
  99.         return 1;  
  100.     }else {  
  101.         return 0;  
  102.     }  
  103.     return 1;  
  104. }  

發佈了30 篇原創文章 · 獲贊 2 · 訪問量 3萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章