scan - 0160708.py 文件源码

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

项目:card-scanner 作者: RFVenter 项目源码 文件源码
def get_contours(image, polydb=0.1, contour_range=7, show=False):
    # 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]]])

    if show: #this is for debugging puposes
        new_image = image.copy()
        cv2.drawContours(new_image, [screenCnt], -1, (255, 255, 0), 2)
        cv2.imshow("Contour1 image", new_image)
        cv2.waitKey(0)
        cv2.destroyAllWindows()

    return screenCnt
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号