2016-06-21 558 views
2

我正在使用Python 2.7和OpenCV 3.0。我正在做一个项目到检测车牌Opencv在Python中检测四边形

我现在检查轮廓的顶点数。如果有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]]] 

对我怎么能省略了不规则四边形我问。谢谢

+0

从我的头顶开始:计算[Convex hull](http://docs.opencv.org/3.1.0/d3/dc0/group__imgproc__shape.html#ga014b28e56cb8854c0de4a211cb2be656&gsc.tab=0)并比较原始多边形到它的凸包。如果它们不相似,那可能不是车牌。 – user1337

回答

1

正如在他的评论中所建议的https://stackoverflow.com/users/4606294/user89161Opencv 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应该是零。

+0

感谢您的回复。我有一个问题,有时'缺陷'将返回NULL – VICTOR

+0

'defect = cv2.convexityDefects(approx,hull) cv2.error:.. \ .. \ .. \ .. \ opencv \ modules \ imgproc \ src \ contours.cpp:1969:error:(-215)ptnum> 3 in function cv :: convexityDefects' – VICTOR

+0

@CLWONG关于错误'ptnum> 3':它不应该发生,因为你做了你的测试'len(approx)== 4',还是没有? –