我正在尝试获得以下形状的角:
我所说的角落是指这个(红点):
可以定义此形状的最小点数。
我已经实现了以下内容:
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;
}
这适用于矩形形状,但旋转后的形状非常清晰,因此,此代码效果不佳:
- 蓝色像素(在这张照片和下面的照片中)是方法
vertices
处理的变量Optimize
。 - 绿色像素是检测到的角/边缘(在两张照片上)。
但是形状的锐度只定义了侧倾,那么我该怎么做才能改善呢?
另外,我已经测试了 Accord.NET BaseCornersDetector 继承的类,但是使用HarrisCornersDetector获得了最好的结果,但是:
许多边缘/角落是不必要的,它们不在需要的地方(见第一张照片)。