【问题标题】:Opencv detect quadrilateral in PythonOpencv在Python中检测四边形
【发布时间】:2016-06-21 10:37:20
【问题描述】:

我正在使用 Python 2.7 和 OpenCV 3.0。我正在做一个检测车牌的项目。

我现在正在检查轮廓的顶点数。如果有 4 个顶点(大约元素个数),那么它更有可能是 矩形/ 平行四边形/ 四边形。 p>

(cnts, _) = cv2.findContours(edged.copy(), cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)
cnts=sorted(cnts, key = cv2.contourArea, reverse = True)[:10]

# loop over our contours
for c in cnts:
    peri = cv2.arcLength(c, True)
    approx = cv2.approxPolyDP(c, 0.02 * peri, True)
    if len(approx) == 4 and ratio(approx):
        cv2.drawContours(image, [approx], -1, (0,255,0), 3)

我得到了两个带数组的四边形。

但是,您可以看到,有一个不规则多边形。这是数组:

[[[209 198]]

 [[466  94]]

 [[259 153]]

 [[247   1]]]

我问的是如何省略不规则四边形。谢谢

【问题讨论】:

  • 从我的头顶:计算Convex hull并将原始多边形与其凸包进行比较。如果它们不相似,则可能不是车牌。

标签: python image opencv image-processing polygon


【解决方案1】:

注意:如果您知道多项式应该有多少边,例如,如果您正在寻找图像中的凸四边形,那么您只需要调用:

hull = cv2.convexHull(approx, returnPoints = False)

然后你只需要检查len(hull) == num_expected_sides,在四边形的情况下:

len(cv2.convexHull(approx, returnPoints = False)) == 4

【讨论】:

  • user1270710 数周以来,我一直在尝试开发一个 kotlin 应用程序,该应用程序将针对伤口图像中感兴趣的区域。我需要用户使用设备屏幕的触摸来绘制感兴趣区域的周围环境,以便分水岭可以仅分割该区域并将背景颜色更改为黑色。我真的需要这样做,但我无法从任何人那里得到帮助,请你帮我解决这个问题。
【解决方案2】:

正如https://stackoverflow.com/users/4606294/user89161 在他的评论Opencv detect quadrilateral in Python 中所建议的那样 也许您可以在 approx 上添加一个测试,例如:

hull = cv2.convexHull(approx,returnPoints = False)
defects = cv2.convexityDefects(approx,hull)

sum_of_defects=0
for i in range(defects.shape[0]):
    s,e,f,d = defects[i,0]
    sum_of_defects=sum_of_defects+d

if sum_of_defects <= threshold:
    print "approx is convex"
else:
    print "approx is not convex"

你必须正确选择threshold,我会从threshold=3或类似的东西开始。理论上threshold 应该为零。

【讨论】:

  • 感谢您的回复。我遇到了一个问题,有时defects 会返回 NULL
  • ` 缺陷 = cv2.convexityDefects(approx,hull) cv2.error: ..\..\..\..\opencv\modules\imgproc\src\contours.cpp:1969: 错误: (-215) ptnum > 3 in function cv::convexityDefects`
  • @CLWONG 关于错误ptnum &gt; 3:它不应该发生,因为你做了你的测试len(approx) == 4,或者没有?
  • 我尝试过使用和不使用len(approx) == 4,有时会显示该错误
  • @CLWONG 在调用convexityDefects 之前,您可能必须测试hull 中的点数,如果是==3,那么您应该很确定它不是四边形?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-02-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-10-21
  • 2021-11-08
相关资源
最近更新 更多