WPF -- 一种圆形识别方案

本文介绍一种圆形的识别方案。

识别流程
  1. 判断是否为封闭图形;
  2. 根据圆的方程,取输入点集中的1/6、3/6、5/6处的三个点,求得圆的方程,获取圆心及半径;
  3. 取点集中的部分点,计算点到圆心的距离与半径的比例,与设定的阈值比较,得出结果。~~~~
实现
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;

    // 取链表上三个点作为判断圆的根据
    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;
}
转载请注明出处,欢迎交流。
原文地址:https://www.cnblogs.com/louzixl/p/14381179.html