Modified Least Square Method Fit Circle from Data

In OpenCv, it only provide the function fitEllipse to fit Ellipse, but doesn‘t provide function to fit circle, so i read some paper, and write a function to do it.

template<typename _tp>
struct Circle_
{
    _tp x;
    _tp y;
    _tp r;
};

typedef Circle_<float> Circle3f;

// This function is based on Modified Least Square Methods from Paper
// "A Few Methods for Fitting Circles to Data".
void FitCircle(const std::vector<cv::Point2f> &vecPoints, Circle3f &circle)
{
    double Sx = 0., Sy = 0., Sxx = 0., Sx2 = 0., Sy2 = 0., Sxy = 0., Syy = 0., Sx3 = 0., Sy3 = 0., Sxy2 = 0., Syx2 = 0.;
    for ( const auto &point : vecPoints )   {
        Sx   += point.x;
        Sy   += point.y;
        Sx2  += point.x * point.x;
        Sy2  += point.y * point.y;
        Sxy  += point.x * point.y;
        Sx3  += point.x * point.x * point.x;
        Sy3  += point.y * point.y * point.y;
        Sxy2 += point.x * point.y * point.y;
        Syx2 += point.y * point.x * point.x;
    }

    double A, B, C, D, E;
    int n = vecPoints.size();
    A = n * Sx2 - Sx * Sx;
    B = n * Sxy - Sx * Sy;
    C = n * Sy2 - Sy * Sy;
    D = 0.5 * ( n * Sxy2 - Sx * Sy2 + n * Sx3 - Sx * Sx2 );
    E = 0.5 * ( n * Syx2 - Sy * Sx2 + n * Sy3 - Sy * Sy2 );

    auto AC_B2 = ( A * C - B * B);  // The variable name is from AC - B^2
    auto am = ( D * C - B * E ) / AC_B2;
    auto bm = ( A * E - B * D ) / AC_B2;

    double rSqureSum = 0.f;
    for ( const auto &point : vecPoints )
    {
        rSqureSum += sqrt ( ( point.x - am ) * ( point.x - am ) + ( point.y - bm) * ( point.y - bm) );
    }
    auto r = rSqureSum / n;
    circle.x = static_cast<float>( am );
    circle.y = static_cast<float>( bm );
    circle.r = static_cast<float>( r );
}

void TestFitCircle()
{
    std::vector<cv::Point2f> vecPoints;
    vecPoints.push_back(cv::Point2f(0, 10));
    vecPoints.push_back(cv::Point2f(0.1f, 10.1f));
    vecPoints.push_back(cv::Point2f(10, 0));
    vecPoints.push_back(cv::Point2f(10, 20));
    vecPoints.push_back(cv::Point2f(20, 10));

    Circle3f circle;
    FitCircle(vecPoints, circle);

    cout << "X, Y " <<circle.x << ", " << circle.y << "    r " << circle.r << endl;
}
时间: 2024-11-05 06:02:04

Modified Least Square Method Fit Circle from Data的相关文章

Least square method using matlab

download linux-matlab: after the installing step: 1), change the current folder for working folder: add the content to ***/installed folder/toolbox/local/matlabrc.m cd 'cd '/home/groot/Music/java_2017/hadoop_r-base/matlab_plot' 2), deal with error af

定义抽象类Shape,抽象方法为showArea(),求出面积并显示,定义矩形类Rectangle,正方形类Square,圆类 Circle,根据各自的属性,用showArea方法求出各自的面积,在main方法中构造3个对象,调用showArea方法。(体现多态)

实现多态的三个条件:1.要有继承2.要有抽象方法重写3.用父类指针(引用)指向子类对象 重载重写重定义的区别: 1.重载:在同一个类中进行; 编译时根据参数类型和个数决定方法调用; 子类无法重载父类; 父类同名方法被子类该方法覆盖. 2.重写:在父类和子类之间进行; 父类与子类方法有完全相同类型; 在运行时根据具体对象类型决定方法调用; 3.在重写中有抽象方法的会产生多态;没有使用抽象方法叫重定义 以下具体代码具体分析: package test3;abstract class Shape{ /

The parameter to the method is the basic data type

1 package method.invocation; 2 3 public class TheParameterToTheMethodIsTheBasicDataType { 4 public static void main(String[] args) { 5 int a = 10; 6 int b = 20; 7 8 System.out.println(a+" "+b); 9 10 change(a, b); 11 12 System.out.println(a+"

Indexing Sensor Data

In particular embodiments, a method includes, from an indexer in a sensor network, accessing a set of sensor data that includes sensor data aggregated together from sensors in the sensor network, one or more time stamps for the sensor data, and metad

Method and Apparatus for Providing Highly-Scalable Network Storage for Well-Gridded Objects

An apparatus comprising a plurality of storage nodes comprising a plurality of corresponding storage disks and configured to store data in a distributed manner between the storage disks that achieves a Redundant Array of Independent Disks-0 (RAID0) l

python data analysis | python数据预处理(基于scikit-learn模块)

原文:http://www.jianshu.com/p/94516a58314d Dataset transformations| 数据转换 Combining estimators|组合学习器 Feature extration|特征提取 Preprocessing data|数据预处理 1 Dataset transformations scikit-learn provides a library of transformers, which may clean (see Preproce

Putting Apache Kafka To Use: A Practical Guide to Building a Stream Data Platform-part 1

转自: http://www.confluent.io/blog/stream-data-platform-1/ These days you hear a lot about "stream processing", "event data", and "real-time", often related to technologies like Kafka, Storm, Samza, or Spark's Streaming module.

Data of Ch5 --Dual rotor

* Results *Conclusion*- little effect of rear rotor on Cp_1- Cp1 is independent of TI** TI effect on single-rotor, front, | cp | ct | TI | TSR ||    |    | 1  |     ||    |    | 15 |     |** Dual rotor X=4D, TI 15% -- CFD-RANS# TI 15%, RANS results#

jquery data方法

jquery.data()文档:http://api.jquery.com/jQuery.data/ html5有个data-*属性,跟这个功能一样. Note: This is a low-level method; a more convenient .data() is also available. The jQuery.data() method allows us to attach data of any type to DOM elements in a way that is