图像处理 - 使用 C# 的光学标记识别



这是一个函数,用于确定一个点是否在图像的边界内,并检查它是否与其他圆重叠。

如果它返回 true,那么我检查填充黑色的圆圈的阈值,然后将填充超过 90% 的点保存到点列表中。

我的程序分两步工作

1)如果它形成一个没有重叠的圆。

2)如果它被90%填充。

我遇到一个错误,如果我传递一个只有 1 个圆圈的图像,它会保存 1408 个圆圈。我不知道我做错了什么。

下面是按钮单击事件

        for (int i = 0; i < 50; i++)
        {
            for (int j = 0; j < 50; j++)
            {
                p.X = j;
                p.Y = i;
                if  (isCircle(p))
                {
                    if (checkThreshold(p) > 90)
                    {
                        pts.Insert(0, p);
                    }
                }
            }
        }

下面给出的是函数

private bool isCircle(Point p)
    {
        double count = 0;
        Point curP = new Point();
        //Point centre = new Point(24, 20);
        int a = 0;
        boundary.X = 50;
        boundary.Y = 50;
        for (int x = (p.X - radius); x <= (p.X - radius); x++)
        {
            for (int y = (p.Y - radius); y <= (p.Y - radius); y++)
            {
                if ((x < boundary.X) && (y < boundary.Y) && (x + radius < boundary.X) && (y + radius < boundary.Y))
                {
                    curP.X = 0;
                    curP.Y = 0;
                    curP.X = x;
                    curP.Y = y; //stores new point to be sent in curP
                    while (a < pts.Count)
                    {
                        //point , centre, radius
                        if (checkOverlap(curP, pts[a], radius) == false) //send point to check if it overlaps or not
                        {
                            // MessageBox.Show("yellow");
                            count = 1;
                            break;
                        }
                        else
                        {
                            a++;
                        }
                    }
                }
                if (count == 1)
                    break;
            }
            if (count == 1)
                break;
        }
        if (count == 1)
            return true;
        else return false;
    }

下面给出的是检查重叠函数

    private bool checkOverlap(Point p, Point c, int radii)
    {
        Point listPoint;
        listPoint = p;
        //the following if condition checks if the point resides in the list or not
        if ((((c.X - radii) < listPoint.X) && (listPoint.X > (c.X - radii))) && (((c.Y - radii) < listPoint.Y) && (listPoint.Y > (c.Y - radii))))
        {
            if ((((p.X - c.X) * (p.X - c.X)) - ((p.Y - c.Y) * (p.Y - c.Y))) < (radius * radius))
            {
                return false;
            }
            return true;
        }
        else
            return true;
    }

不确定这是否是问题所在,但由于您测试相等性的方式,您的count变量应该是一个int

最新更新