python - Opencv在Python中检测四边形

标签 python image opencv image-processing polygon

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

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

(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)

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

enter image description here

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

[[[209 198]]

 [[466  94]]

 [[259 153]]

 [[247   1]]]

请问如何省略不规则四边形谢谢

最佳答案

根据 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 应该为零。

关于python - Opencv在Python中检测四边形,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37942132/

相关文章:

python - 如何将命令的结果存储到变量中?

Python numpy 和 pandas 矩阵维度

Python 将变量从一个脚本发送到另一个脚本

html - Windows 手机模拟器不加载图像

javascript - 在文本链接悬停上显示图像 CSS 或 JS

python - 如何使用 BeautifulSoup 跳过某些标签?

html - 使用css表格中图片的大小

opencv - 错误CS0119:表达式表示为 `type',其中应为 `variable', `value'或 `method group'

json - Python 错误 : argument -c/--conf is required

python - See3CAM/OpenCV 自动白平衡支持