|
最小二乘法拟合圆
[1]https://blog.csdn.net/u014090257/article/details/108899982
[2]https://www.cnblogs.com/zxtang/p/15519549.html
- private void FittingCircle1D(List<System.Drawing.Point> pPointList, out double X_c, out double Y_c, out double R)
- {
- X_c = 0;
- Y_c = 0;
- R = 0;
- if (pPointList.Count < 3)
- {
- return; // 最少3个点进行拟合
- }
- double X1 = 0;
- double Y1 = 0;
- double X2 = 0;
- double Y2 = 0;
- double X3 = 0;
- double Y3 = 0;
- double X1Y1 = 0;
- double X1Y2 = 0;
- double X2Y1 = 0;
- for (int i = 0; i < pPointList.Count; i++)
- {
- X1 = X1 + pPointList[i].X;
- Y1 = Y1 + pPointList[i].Y;
- X2 = X2 + pPointList[i].X * pPointList[i].X;
- Y2 = Y2 + pPointList[i].Y * pPointList[i].Y;
- X3 = X3 + pPointList[i].X * pPointList[i].X * pPointList[i].X;
- Y3 = Y3 + pPointList[i].Y * pPointList[i].Y * pPointList[i].Y;
- X1Y1 = X1Y1 + pPointList[i].X * pPointList[i].Y;
- X1Y2 = X1Y2 + pPointList[i].X * pPointList[i].Y * pPointList[i].Y;
- X2Y1 = X2Y1 + pPointList[i].X * pPointList[i].X * pPointList[i].Y;
- }
- double C, D, E, G, H, N;
- double a, b, c;
- N = pPointList.Count;
- C = N * X2 - X1 * X1;
- D = N * X1Y1 - X1 * Y1;
- E = N * X3 + N * X1Y2 - (X2 + Y2) * X1;
- G = N * Y2 - Y1 * Y1;
- H = N * X2Y1 + N * Y3 - (X2 + Y2) * Y1;
- a = (H * D - E * G) / (C * G - D * D);
- b = (H * C - E * D) / (D * D - G * C);
- c = -(a * X1 + b * Y1 + X2 + Y2) / N;
- X_c = a / (-2);
- Y_c = b / (-2);
- R = Math.Sqrt(a * a + b * b - 4 * c) / 2;
- }
-
复制代码
|
|