c# 圓形識別方案和直線識別方案的參考示例
更新時間:2021年03月15日 09:52:31 作者:louzi
這篇文章主要介紹了c# 圓形識別方案和直線識別方案的實現(xiàn)示例,幫助大家更好的理解和學習使用c#,感興趣的朋友可以了解下
圓形識別方案
識別流程
- 判斷是否為封閉圖形;
- 根據(jù)圓的方程,取輸入點集中的1/6、3/6、5/6處的三個點,求得圓的方程,獲取圓心及半徑;
- 取點集中的部分點,計算點到圓心的距離與半徑的比例,與設定的閾值比較,得出結果。~~~~
實現(xiàn)
public static bool IsCircle(List<Point> points, out Point center, out double radius)
{
int len = points.Count;
center = new Point();
radius = 0;
// 判斷是否為封閉圖形
if (!IsClosedFigure(points))
return false;
int judgePointNum = len * 50 / 100;
if (len < judgePointNum)
return false;
// 取鏈表上三個點作為判斷圓的根據(jù)
Point p1 = points[len / 6];
Point p2 = points[len / 2];
Point p3 = points[len * 5 / 6];
if ((Math.Abs(p1.X - p2.X) < 100 && Math.Abs(p1.Y - p2.Y) < 100)
|| (Math.Abs(p1.X - p3.X) < 100 && Math.Abs(p1.Y - p3.Y) < 100)
|| (Math.Abs(p2.X - p3.X) < 100 && Math.Abs(p2.Y - p3.Y) < 100))
return false;
// 三個點確定圓的方程,獲取圓心坐標及半徑
GetCircle(p1, p2, p3, out center, out radius);
// 獲取圓上平均分部的多個點,判斷其到圓心的距離與半徑之差是否在精度內
for (int i = 0; i < judgePointNum; ++i)
{
// 獲取圓上點
Point p = points[len * i / judgePointNum];
double deviation = Math.Abs(GetDistance(center, p) - radius);
// 點在圓上的偏移量與半徑的比值若大于固定值,則不為圓
if (deviation/radius > MaxRatio)
return false;
}
return true;
}
直線識別方案
步驟
1.使用最小二乘法回歸直線:

2.得到直線方程y=kx+b后,計算所有點到直線的距離,若在閾值范圍內,認為是直線。
實現(xiàn)
/// <summary>
/// 最小二乘法求回歸直線方程
/// </summary>
/// <param name="points">輸入數(shù)據(jù)</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;
}
}
以上就是c# 圓形識別方案和直線識別方案的實現(xiàn)示例的詳細內容,更多關于c# 圓形識別方案和直線識別方案的資料請關注腳本之家其它相關文章!
相關文章
C#使用WMI實現(xiàn)監(jiān)聽進程的啟動和關閉
Windows Management Instrumentation(WMI)是用于管理基于 Windows 操作系統(tǒng)的數(shù)據(jù)和操作的基礎結構,本文將使用WMI實現(xiàn)監(jiān)聽進程的啟動和關閉,感興趣的可以了解下2024-01-01
C# winfroms使用socket客戶端服務端的示例代碼
這篇文章主要為大家詳細介紹了C# winfroms使用socket客戶端服務端的相關知識,文中的示例代碼講解詳細,感興趣的小伙伴可以跟隨小編一起學習一下2024-02-02
Silverlight文件上傳下載實現(xiàn)方法(下載保存)
這篇文章主要介紹了Silverlight文件上傳下載實現(xiàn)方法(下載保存) ,需要的朋友可以參考下2015-11-11

