本文介紹一種直線的識別方案。code
$$\begin{cases}\frac{\sum_{i=1}^N(x_i,\overline{x})(y_i,\overline{y})}{\sum_{i=1}^N(x_i,\overline{x})^2}\\b=\overline{y}-k\overline{x}\end{cases}$$class
y=kx+b
後,計算全部點到直線的距離,若在閾值範圍內,認爲是直線。/// <summary> /// 最小二乘法求迴歸直線方程 /// </summary> /// <param name="points">輸入數據</param> /// <param name="k">直線斜率</param> /// <param name="b">直線截距</param> /// <param name="type">直線類型 1:水平線 2:垂直線 3:通常直線</param> /// <returns></returns> public static bool IsLine(List<Point> points, out double k, out double b, out int type) { k = 0; b = 0; type = 0; if (points.Count < 2) return false; double averageX = 0, averageY = 0, n = 0; n = points.Count; foreach (Point p in points) { averageX += p.X; averageY += p.Y; } averageX /= n; averageY /= n; double numerator = 0, denominator = 0; foreach (Point p in points) { numerator += (p.X - averageX) * (p.Y - averageY); denominator += (p.X - averageX) * (p.X - averageX); } if (numerator == 0) //平行於X軸爲水平線,返回縱座標平均值 { b = averageY; type = 1; } else if (denominator == 0)//平行於Y軸爲垂直線,返回橫座標平均值 { b = averageX; type = 2; } else { type = 3; } k = numerator / denominator; b = averageY - k * averageX; foreach (Point p in points) { dis = GetPoint2LineDistance(p, k, b, type); if (dis > MAX_POINT_LINE_DIS) return false; //點到擬合直線距離過大 } return true; } /// <summary> /// 計算點到直線的距離 /// </summary> /// <param name="p">待計算點</param> /// <param name="k">直線斜率</param> /// <param name="b">直線截距</param> /// <param name="type">直線類型 1:水平線 2:垂直線 3:通常直線</param> /// <returns>距離</returns> private static double GetPoint2LineDistance(Point p, double k, double b, int type) { if (type == 1) { return Math.Abs(p.Y - b); } else if (type == 2) { return Math.Abs(p.X - b); } else { double numerator = 0, denominator = 0; numerator = Math.Abs(k * p.X - p.Y + b); denominator = Math.Sqrt(k * k + 1); return numerator / denominator; } }