2012-07-30 21 views
12

私はOpenCV fitLine()アルゴリズムを理解しようとしています。OpenCVラインフィッティングアルゴリズム

これはOpenCVのからのコードの断片である: icvFitLine2D機能 - 私は近似のためのポイントを選択するいくつかのランダム関数であることがわかりicvFitLine2D

、次いで、(選びだし点を有する)フィットラインに点からの距離を計算します他の点を選択し、距離を最小にしようとすると、distTypeが選択されます。

誰かが難しい数学とは大きな統計の知識がないと仮定することなく、this momentから何が起こるかを明確にすることはできます?。 OpenCVコードのコメントと変数名は、このコードの理解に役立ちません。

答えて

17

(これは古い質問ですが、主題は私の好奇心をそそっ)

OpenCVのFitLine implemements二つの異なるメカニズム。

distTypeCV_DIST_L2に設定されている場合は、standard unweighted least squares fitが使用されます。

distTypesの一つは(CV_DIST_L1CV_DIST_L12CV_DIST_FAIRCV_DIST_WELSCHCV_DIST_HUBER)が使用されている場合、手順はRANSACフィットのいくつかの並べ替えです:

  • 繰り返し最大20回で:
    • ランダムな点を10点選び、最小の四角形はそれらの点のみに合うようにする。
    • 最大30回繰り返す:
      • 現在見つかった行を使用して、すべてのポイントの重みを計算し、選択されたdistType
      • はすべての点
      • に適合加重最小二乗を実行してください(これはIteratively reweighted least squares fit又はM-Estimatorである)
  • ベスト検索結果の線を返します

ここでは、pseで詳しく説明しますudocode:

distType = CV_DIST_L1 enter image description here

distType = CV_DIST_L12:

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によれば、そのための式は、pがweight[Point_i] = 1/ p(distance_between_point_i_and_line)、あります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ノルム(最低のラインの非加重合計に応じて選択されます四角は最小です)。これが正しいとは思わない。