Image processing 如何在图像中查找矩形的角坐标
我在对原始图像进行预处理后得到了这张图像。现在,我的问题是如何得到矩形(最大)的四个角的坐标。对不起,这是一个很难回答的问题Image processing 如何在图像中查找矩形的角坐标,image-processing,Image Processing,我在对原始图像进行预处理后得到了这张图像。现在,我的问题是如何得到矩形(最大)的四个角的坐标。对不起,这是一个很难回答的问题 更新:由于我是使用OpenCV开发的,所以最终使用了一种简单的方法: 查找所有连接的组件 计算每个组件的凸包 拾取凸面外壳具有最大面积的零部件 简化凸包多边形 简化多边形的顶点就是要查找的点 快速解决方案(&A): (* find all connected components, calculate the convex hull for each component
更新:由于我是使用OpenCV开发的,所以最终使用了一种简单的方法:
(* 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]}
}]]
(*找到所有连接的组件,计算每个组件的凸包*)
convexHulls=组件度量[ColorNegate[Binarize[src]],{“ConvexArea”,“ConvexVertices”}];
(*选择凸面外壳具有最大面积的组件*)
顶点=排序为[convexHulls[[All,2]],First][-1,2]]
(*通过迭代移除穿过前后顶点到直线距离最小的顶点,简化凸包多边形*)
distanceToNeighbors[顶点]:=MapThread[Abs[(#1-#2).Cross[#1-#3]/Norm[#1-#3]]&,RotateLeft[顶点,#]&/{-1,0,1}]
removeVertexWithLowestDistance[顶点]:=具有[{removeIndex=排序[distanceToNeighbors[顶点],1]},拖放[Vertex,removeIndex]]
VerticessSimplified=NestWhile[移除距离最小的顶点,顶点,最小[距离到八个顶点[#]]<10&]
(*简化多边形的顶点是您要查找的点*)
显示[src,图形][
{
{EdgeForm[Red],透明,多边形[VerticessSimplified]},
{红色,点大小[大],点[垂直简化]}
}]]
似乎是一个很好的解决方案。谢谢尽管这看起来是一个完美的解决方案,但你能为它写一个python版本吗?我不知道你用了什么库