如何在图像中找到矩形的角坐标 [英] How to find corner coordinates of a rectangle in an image

查看:225
本文介绍了如何在图像中找到矩形的角坐标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在预处理原始图像后得到了这个图像。现在,我的问题是如何获得矩形的四个角坐标(最大)。很抱歉,如果这是noob问题。

I got this image after preprocessing the original image. Now, my question is how I can get the four corner's coordinates of the rectangle (largest). Sorry if this is so noob question.

更新:由于我正在使用OpenCV进行开发,最后使用的是这个答案

Update: Since I am developing with OpenCV, ended up using this answer

推荐答案

一种简单的方法是:


  1. 查找所有连接的组件

  2. 计算每个组件的凸包

  3. 选择凸包的组件具有最大面积

  4. 简化凸包多边形

  5. 简化多边形的顶点是您正在寻找的点

  1. find all connected components
  2. calculate the convex hull for each component
  3. pick the component where the convex hull has the largest area
  4. simplify the convex hull polygon
  5. the vertices of the simplified polygon are the points you're looking for

快速和肮脏的Mathematica解决方案:

Quick&dirty Mathematica solution:

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

这篇关于如何在图像中找到矩形的角坐标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆