【问题标题】:Find corners/edges on a shape (minimum vertices that can define that shape)查找形状上的角/边(可以定义该形状的最小顶点)
【发布时间】:2019-03-27 00:50:36
【问题描述】:

我正在尝试获取以下形状的角:

我指的是角落(红点):

可以定义此形状的最小点数。

我已经实现了以下内容:

    public Shape Optimize()
    {
        // If the vertices are null or empty this can't be executed
        if (vertices.IsNullOrEmpty())
            return this; // In this case, return the same instance.

        if (!edges.IsNullOrEmpty())
            edges = null; //Reset edges, because a recalculation was requested

        // The corners available on each iteration
        var corners = new Point[] { Point.upperLeft, Point.upperRight, Point.downLeft, Point.downRight };

        //The idea is to know if any of the following or previous vertice is inside of the the array from upside, if it is true then we can add it.

        Point[] vs = vertices.ToArray();

        for (int i = 0; i < vertices.Count - 1; ++i)
        {
            Point backPos = i > 0 ? vs[i - 1] : vs[vertices.Count - 1],
                  curPos = vs[i], //Punto actual
                  nextPos = i < vertices.Count - 1 ? vs[i + 1] : vs[0];

            // We get the difference point between the actual point and the back & next point
            Point backDiff = backPos - curPos,
                  nextDiff = nextPos - curPos,
                  totalDiff = nextPos - backPos;

            if (corners.Contains(backDiff) || corners.Contains(nextDiff) || corners.Contains(totalDiff))
                AddEdge(curPos, center); // If any of the two points are defined in the corners of the point of before or after it means that the actual vertice is a edge/corner
        }

        return this;
    }

这适用于矩形形状,但旋转后的形状非常锐利,因此,此代码效果不佳:

  • 蓝色像素(在这张照片和下面的照片中)是在Optimize 方法上处理的vertices 变量。
  • 绿色像素是检测到的角/边缘(在两张照片上)。

但是形状的锐度只定义了侧倾,那么我可以做些什么来改善呢?

另外,我已经测试过Accord.NET BaseCornersDetector inherited classes,但最好的结果是用HarrisCornersDetector,但是:

许多边缘/角落是不必要的,它们不在需要的位置(见第一张照片)。

【问题讨论】:

    标签: c# image-processing bresenham douglas-peucker


    【解决方案1】:

    好吧,经过数小时的研究,我发现了一个名为 Simplify.NET 的库,它在内部运行 Ramer–Douglas–Peucker algorithm

    另外,您可能对Bresenham algorithm 感兴趣,使用此算法您可以draw a line using two Points

    使用这个算法,你可以检查你的容忍度是否太高,比较实际点和这个算法输出的点,并制作某种相似度百分比计算器。

    最后,值得一提的是Concave HullConvex Hull 算法。

    这一切都与 Unity3D 有关。

    我的输出:

    还有my implementation

    非常重要的一点是,需要对点进行排序以强制它们连接。如果形状是凹形的,如您在第二张照片中看到的那样,您可能需要迭代形状的墙壁。

    您可以查看实现示例here。感谢@Bunny83。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-12-06
      • 1970-01-01
      • 2010-10-11
      • 2011-09-19
      • 2016-05-18
      • 2016-09-21
      • 2015-06-12
      • 1970-01-01
      相关资源
      最近更新 更多