【问题标题】:OpenCV line fitting algorithmOpenCV 线拟合算法
【发布时间】:2012-07-28 04:25:10
【问题描述】:

我正在尝试理解 OpenCV fitLine() 算法。

这是来自 OpenCV 的代码片段: icvFitLine2D 函数 - icvFitLine2D

我看到有一些随机函数选择近似点,然后计算点到拟合线的距离(使用选择的点),然后选择其他点并尝试使用选择的 distType 最小化距离。

有人可以澄清this moment 会发生什么,而无需经过严格的数学运算并假设没有出色的统计知识吗? OpenCV 代码 cmets 和变量名对我理解这段代码没有帮助。

【问题讨论】:

    标签: algorithm opencv statistics curve-fitting


    【解决方案1】:

    (这是一个老问题,但这个话题激起了我的好奇心)

    OpenCV FitLine 实现了两种不同的机制。

    如果参数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 次:
    • 返回找到的最佳线型

    下面是更详细的伪代码描述:

    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 范数(未加权和最小二乘是最小的)。我不确定这是否正确。

    【讨论】:

      猜你喜欢
      • 2016-06-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-06-12
      • 1970-01-01
      • 2019-10-15
      • 1970-01-01
      • 2020-10-10
      相关资源
      最近更新 更多