我想了解OpenCV fitLine()算法。OpenCV線擬合算法
這是從OpenCV的代碼片斷: icvFitLine2D
功能 - icvFitLine2D
我看到有用來選擇點近似一些隨機函數,然後計算從點距離擬合線(帶choosen分),然後選擇其他點並嘗試通過選擇distType
來最小化距離。
有人可以澄清從this moment會發生什麼,沒有硬數學,並假設沒有偉大的統計知識? OpenCV代碼註釋和變量名稱不能幫助我理解此代碼。
我想了解OpenCV fitLine()算法。OpenCV線擬合算法
這是從OpenCV的代碼片斷: icvFitLine2D
功能 - icvFitLine2D
我看到有用來選擇點近似一些隨機函數,然後計算從點距離擬合線(帶choosen分),然後選擇其他點並嘗試通過選擇distType
來最小化距離。
有人可以澄清從this moment會發生什麼,沒有硬數學,並假設沒有偉大的統計知識? OpenCV代碼註釋和變量名稱不能幫助我理解此代碼。
(這是一個老問題,但主題激起了我的好奇心)
在OpenCV FitLine
implemements兩種不同的機制。
如果參數distType
設置爲CV_DIST_L2
,則使用standard unweighted least squares fit。
如果使用其它distTypes
之一(CV_DIST_L1
,CV_DIST_L12
,CV_DIST_FAIR
,CV_DIST_WELSCH
,CV_DIST_HUBER
),那麼該過程是某種RANSAC擬合:
distType
這是一個更詳細的描述在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
distType = CV_DIST_L12
distType = CV_DIST_FAIR
distType = CV_DIST_WELSCH
distType = CV_DIST_HUBER
不幸的是,我不知道哪個distType
是最適合哪種類型的數據,也許有些人能揭示一些輕。
一些有趣的我注意到:所選擇的範數僅用於迭代重新加權,所發現的那些中的最佳解決方案是根據所述L2範數(線總是挑選的量,未加權總和的至少正方形是最小的)。我不確定這是否正確。