轮廓检测人拿着纸页

时间:2018-06-08 14:03:02

标签: python opencv

我需要检测一个人持有的白纸。这里的问题是手指,它打破了轮廓线。一些轮廓包含某种形状,我称之为噪音,形状。

手指轮廓:
Contours with fingers

原创img:
Original img

import cv2
import imutils
import numpy as np

image = cv2.imread('test.jpg')
ratio = image.shape[0] / 500.0
orig = image.copy()
image = imutils.resize(image, height = 500)

orig = image.copy()

gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
gray = cv2.GaussianBlur(gray, (5,5), 0)
edged = cv2.Canny(gray, 100, 200)

image, cnts, hierarchy = cv2.findContours(edged.copy(), cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)
cnts = sorted(cnts, key = cv2.contourArea, reverse = True)[:10]

for c in cnts:
    # approximate the contour

    peri = cv2.arcLength(c, True)
    approx = cv2.approxPolyDP(c, 0.1 * peri, True)

    #epsilon = 0.1*cv2.arcLength(c,True)
    #approx = cv2.approxPolyDP(c,epsilon,True)
    #approx = cv2.approxPolyDP(c,0.1*cv2.arcLength(c,True),True)

    if len(approx) == 4:
        screenCnt = approx
        break


cv2.drawContours(orig, [screenCnt], -1, (0, 255, 0), 2)
cv2.imshow("Found", image)

cv2.waitKey(0)
cv2.destroyAllWindows()

第二个问题

如何将图像插入轮廓

1 个答案:

答案 0 :(得分:0)

希望此解决方案有助于实现您的目标。 这是解决方案的完整代码:

getTestChildrens 

首先使用手动阈值(200),您可以检测图像中的纸张。

import cv2
import numpy as np
image = cv2.imread('stack.jpg',-1)
paper = cv2.resize(image,(500,500))
ret, thresh_gray = cv2.threshold(cv2.cvtColor(paper, cv2.COLOR_BGR2GRAY),
                        200, 255, cv2.THRESH_BINARY)
image, contours, hier = cv2.findContours(thresh_gray, cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_NONE)
for c in contours:
    rect = cv2.minAreaRect(c)
    box = cv2.boxPoints(rect)
    # convert all coordinates floating point values to int
    box = np.int0(box)
    # draw a green 'nghien' rectangle
    cv2.drawContours(paper, [box], 0, (0, 255, 0),1)

cv2.imshow('paper', paper)
cv2.imwrite('paper.jpg',paper)
cv2.waitKey(0)

输出

之后你应该找到轮廓并获得ret, thresh_gray = cv2.threshold(cv2.cvtColor(paper, cv2.COLOR_BGR2GRAY), 200, 255, cv2.THRESH_BINARY) 。然后你应该获得该矩形的坐标(框)并绘制它。

minAreaRect()

最终输出: