如何在图像中找到矩形的角落坐标

3

我通过对原始图像进行预处理得到了这张图片。现在我的问题是如何获得矩形(最大)的四个角点的坐标。如果这是一些初学者的问题,那我很抱歉。

enter image description here

更新:由于我正在使用OpenCV进行开发,所以最终使用这个答案

1个回答

5

一个简单的方法是:

  1. 找到所有连通组件
  2. 为每个组件计算凸包
  3. 选择凸包面积最大的组件
  4. 化简凸包多边形
  5. 化简后的多边形顶点即为所需点

快速且简便的Mathematica解决方案:

(* 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]}
   }]]

Result


看起来是个很棒的解决方案。谢谢! - Tae-Sung Shin
尽管它看起来是完美的解决方案,你能否写一个Python版本的代码呢?我不知道你使用了哪些库,感到困惑。 - dragonLOLz

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接