2012-07-30 158 views
12

我想了解OpenCV fitLine()算法。OpenCV線擬合算法

這是從OpenCV的代碼片斷: icvFitLine2D功能 - icvFitLine2D

我看到有用來選擇點近似一些隨機函數,然後計算從點距離擬合線(帶choosen分),然後選擇其他點並嘗試通過選擇distType來最小化距離。

有人可以澄清從this moment會發生什麼,沒有硬數學,並假設沒有偉大的統計知識? OpenCV代碼註釋和變量名稱不能幫助我理解此代碼。

回答

17

(這是一個老問題,但主題激起了我的好奇心)

在OpenCV FitLine implemements兩種不同的機制。

如果參數distType設置爲CV_DIST_L2,則使用standard unweighted least squares fit

如果使用其它distTypes之一(CV_DIST_L1CV_DIST_L12CV_DIST_FAIRCV_DIST_WELSCHCV_DIST_HUBER),那麼該過程是某種RANSAC擬合:

  • 重複最多20次:
    • 挑選10個隨機點,做適合他們的最小二乘方
    • 重複最多30次:
  • 返回最好的線裝配

這是一個更詳細的描述在pse udocode:

repeat at most 20 times: 

    RANSAC (line 371) 
    - pick 10 random points, 
    - set their weights to 1, 
    - set all other weights to 0 

    least squares weighted fit (fitLine2D_wods, line 381) 
    - fit only the 10 picked points to the line, using least-squares 

    repeat at most 30 times: (line 382) 
    - stop if the difference between the found solution and the previous found solution is less than DELTA (line 390 - 406) 
     (the angle difference must be less than adelta, and the distance beween the line centers must be less than rdelta) 
    - stop if the sum of squared distances between the found line and the points is less than EPSILON (line 407) 
     (The unweighted sum of squared distances is used here ==> the standard L2 norm) 

     re-calculate the weights for *all* points (line 412) 
     - using the given norm (CV_DIST_L1/CV_DIST_L12/CV_DIST_FAIR/...) 
     - normalize the weights so their sum is 1 
     - special case, to catch errors: if for some reason all weights are zero, set all weight to 1 

     least squares weighted fit (fitLine2D_wods, line 437) 
     - fit *all* points to the line, using weighted least squares 

    if the last found solution is better than the current best solution (line 440) 
     save it as the new best 
     (The unweighted sum of squared distances is used here ==> the standard L2 norm) 

     if the distance between the found line and the points is less than EPSILON 
      break 

return the best solution 

的權重取決於所選distType計算,根據the manual該公式是weight[Point_i] = 1/ p(distance_between_point_i_and_line),其中p爲:

distType = CV_DIST_L1 enter image description here

distType = CV_DIST_L12 enter image description here

distType = CV_DIST_FAIR enter image description here

distType = CV_DIST_WELSCH enter image description here

distType = CV_DIST_HUBER enter image description here

不幸的是,我不知道哪個distType是最適合哪種類型的數據,也許有些人能揭示一些輕。


一些有趣的我注意到:所選擇的範數僅用於迭代重新加權,所發現的那些中的最佳解決方案是根據所述L2範數(線總是挑選的量,未加權總和的至少正方形是最小的)。我不確定這是否正確。