2016-09-19 279 views
0

我一直在使用PyImageSearch.com的優秀教程來獲得一個Pi(v3)來識別一些紙牌。到目前爲止,它一直在努力,但教程中描述的方法更適用於銳角矩形,當然,撲克牌也是圓角的。這意味着輪廓邊角最終會略微偏移到實際的卡片上,因此我得到的裁剪和去扭曲圖像會稍微旋轉一點,這會略微影響相框識別。 綠色輪廓由OpenCV提供,您可以看到與我繪製的紅色線相比較,以標記它偏移/旋轉的實際邊界。我的問題是;我怎樣才能讓它遵循那些紅線即檢測邊緣?使用OpenCV和圓角卡進行更好的邊緣檢測

這是目前運行得到這一結果的代碼:

frame = vs.read() 
 
    frame = cv2.flip(frame, 1) 
 
    frame = imutils.resize(frame, width=640) 
 
    image = frame.copy() #copy frame so that we don't get funky contour problems when drawing contours directly onto the frame. 
 
    
 
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) 
 

 
    gray = cv2.bilateralFilter(gray, 11, 17, 17) 
 
    edges = imutils.auto_canny(gray) 
 

 
    cv2.imshow("Edge map", edges) 
 

 
    #find contours in the edged image, keep only the largest 
 
    # ones, and initialize our screen contour 
 
    _, cnts, _ = cv2.findContours(edges.copy(), cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) 
 
    cnts = sorted(cnts, key = cv2.contourArea, reverse = True)[:3] 
 
    screenCnt = None 
 

 
    # loop over our contours 
 
    for c in cnts: 
 
     # approximate the contour 
 
     peri = cv2.arcLength(c, True) 
 
     approx = cv2.approxPolyDP(c, 0.05 * peri, True) 
 
     
 
     # if our approximated contour has four points, then 
 
     # we can assume that we have found our card 
 
     if len(approx) == 4: 
 
      screenCnt = approx 
 
      break 
 

 
    cv2.drawContours(image, [screenCnt], -1, (0, 255, 0), 3)

回答

0

原來我只是需要讀取OpenCV contour docs多一點。什麼我基本上是在尋找大約是我的等值線的最小面積框:

rect = cv2.minAreaRect(cnt) # get a rectangle rotated to have minimal area 
box = cv2.boxPoints(rect) # get the box from the rectangle 
box = np.int0(box) # the box is now the new contour. 

在我的情況下,screenCnt所有實例現在變成了box變量和我的代碼的其餘部分繼續正常進行。

+0

你如何應對視角失真? – jtlz2