我正在使用 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)
我得到了两个带数组的四边形。
但是,你可以看到,有一个不规则的多边形。这是数组:
[[[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/