我对原始图像进行预处理后得到了这张图像。现在,我的问题是如何获得矩形(最大)的四个角的坐标。抱歉,如果这是一个很菜鸟的问题。
更新:由于我正在使用 OpenCV 进行开发,最终使用这个答案 https://stackoverflow.com/a/9841459/1194873
一种简单的方法是:
- 找到所有连接的组件
- 计算每个分量的凸包
- 选择凸包面积最大的组件
- 简化凸包多边形
- 简化多边形的顶点就是您要寻找的点
快速数学解决方案:
(* find all connected components, calculate the convex hull for each component *)
convexHulls = ComponentMeasurements[ColorNegate[Binarize[src]], {"ConvexArea", "ConvexVertices"}];
(* pick the component where the convex hull has the largest area *)
vertices = SortBy[convexHulls[[All, 2]], First][[-1, 2]]
(* simplify the convex hull polygon, by iteratively removing the vertex with the lowest distance to the line through the vertex before and after it *)
distanceToNeighbors[vertices_] := MapThread[Abs[(#1 - #2).Cross[#1 - #3]/Norm[#1 - #3]]&, RotateLeft[vertices, #] & /@ {-1, 0, 1}]
removeVertexWithLowestDistance[vertices_] := With[{removeIndex = Ordering[distanceToNeighbors[vertices], 1]}, Drop[vertices, removeIndex]]
verticesSimplified = NestWhile[removeVertexWithLowestDistance, vertices, Min[distanceToNeighbors[#]] < 10&]
(* the vertices of the simplified polygon are the points you're looking for *)
Show[src, Graphics[
{
{EdgeForm[Red], Transparent, Polygon[verticesSimplified]},
{Red, PointSize[Large], Point[verticesSimplified]}
}]]
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)