2016-06-21 1043 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',還是沒有? –