【发布时间】: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