2017-02-01 23 views
0

私は thisクロップ矩形

のようなイメージを持っていると私は棚から各書籍をトリミングしたいです。私はこのコードで始めました。

thresh = cv2.adaptiveThreshold(blur, 255, 1, 1, 11, 2) 

cv2.imshow("Gray", gray) 
cv2.waitKey(0) 

cv2.imshow("Blurred", blur) 
cv2.waitKey(0) 

# detect edges in the image 
edged = cv2.Canny(img, 10, 250) 
cv2.imshow("Edged", edged) 
cv2.waitKey(0) 

# construct and apply a closing kernel to 'close' gaps between 'white' 
# pixels 
kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (1, 6)) 
closed = cv2.morphologyEx(edged, cv2.MORPH_CLOSE, kernel) 
cv2.imshow("Closed", closed) 
cv2.waitKey(0) 

# loop over the contours 
for contour in contours: 

    # peri = cv2.arcLength(contour, True) 
    # approx = cv2.approxPolyDP(contour, 0.02 * peri, True) 
    # r = cv2.boundingRect(contour) 
    if len(contour) >= 4: 
     index += 1 
     x, y, w, h = cv2.boundingRect(contour) 
     roi = img[y:y+h, x:x+w] 
     # cv2.imwrite("a/" + "book - " + str(index) + '.jpg', roi) 
     draw_contour = cv2.drawContours(img, [contour], -1, (255, 140, 240), 2) 
     total += 1 

print contour 

cv2.imshow("Drawed Contour", img) 
cv2.waitKey(0) 

私は棚から本のそれぞれにおけるバウンディングボックスを作成したが、残念ながらこれは私にoutputを与えます。ブックのサイド/コーナーにバウンディングボックスを描き、バウンディングボックスから切り抜きたいだけです。

+0

私はhough transformを使用して長方形を検出し、方向とサイズでフィルタリングします。私は単純なエッジ検出器があなたのケースで動作するとは思わない。 –

+0

よろしくお願いします。 – whaangbuu

答えて

3

このコードで明示的に唯一の本を特定することはできませんが、コードですばやく改善できる点は、ある値より大きな領域を持つ輪郭を描画することです。次のコードスニペット

if len(contour) >= 4: 
    index += 1 
    x, y, w, h = cv2.boundingRect(contour) 
    roi = img[y:y+h, x:x+w] 
    # cv2.imwrite("a/" + "book - " + str(index) + '.jpg', roi) 
    if cv2.contourArea(contour) > 200: 
     draw_contour = cv2.drawContours(img, [contour], -1, (255, 140, 240), 2) 
     total += 1 
+0

私はあなたの提案を適用します。ヘッドアップありがとう – whaangbuu

+0

ありがとう@Nilayその作業。しかし、私は辞書のような大きな本の背骨のためにいくつかの調整を行いました。もう一度、ありがとう! – whaangbuu