Python中的Opencv四边形检测

2024-04-19 00:24:49 发布

您现在位置:Python中文网/ 问答频道 /正文

我使用的是python2.7和opencv3.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

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

^{pr2}$

我在问我怎样才能省略不规则四边形。谢谢


Tags: 项目true元素数量cv2汽车轮廓矩形
1条回答
网友
1楼 · 发布于 2024-04-19 00:24:49

正如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应该是零。在

相关问题 更多 >