scan.py 文件源码

python
阅读 31 收藏 0 点赞 0 评论 0

项目:card-scanner 作者: RFVenter 项目源码 文件源码
def get_contours(image, polydb=0.1, contour_range=7):
    # find the contours in the edged image, keeping only the largest ones, and initialize the screen contour
    contours = _findContours(image, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)
    cnts = sorted(contours, key = cv2.contourArea, reverse = True)[:contour_range]
    # loop over the contours
    screenCnt = None
    for c in cnts:
        # approximate the contour
        peri = cv2.arcLength(c, True) #finds the Contour Perimeter
        approx = cv2.approxPolyDP(c, polydb * peri, True)
        # if our approximated contour has four points, then we can assume that we have found our screen
        if len(approx) == 4:
            screenCnt = approx
            break
    if screenCnt is None:
        raise EdgeNotFound()
    # sometimes the algorythm finds a strange non-convex shape. The shape conforms to the card but its not complete, so then just complete the shape into a convex form
    if not cv2.isContourConvex(screenCnt):
        screenCnt = cv2.convexHull(screenCnt)
        x,y,w,h = cv2.boundingRect(screenCnt)
        screenCnt = numpy.array([[[x, y]], [[x+w, y]], [[x+w, y+h]], [[x, y+h]]])
    return screenCnt
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号