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

関連する問題