2016-09-19 224 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