Example #1
0
def rectangularize(imgpath):
    img = cv2.imread(imgpath,  cv2.IMREAD_UNCHANGED)
    orig = img.copy()
    img = cv2.resize(img, (0,0), fx=0.1, fy=0.1)

    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    gray = cv2.GaussianBlur(gray, (5,5), 0)
    edged = cv2.Canny(gray, 75, 200)

    (a, contours, b) = cv2.findContours(edged.copy(), cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)
    contours = sorted(contours, key=cv2.contourArea, reverse = True)[:5]

    ctrs = None

    for c in contours:
        peri = cv2.arcLength(c, True)
        approx = cv2.approxPolyDP(c, 0.02 * peri, True)

        if len(approx) == 4:
            ctrs = approx * 10
            break

    # cv2.imshow('outline', img)
    # cv2.waitKey(0)
    # cv2.destroyAllWindows()
    xd = wrap_rectangular(orig, ctrs)
    cv2.drawContours(img, [ctrs], -1, (255,0,0), 2)

    _, buf =  cv2.imencode('.jpg', xd)
    return buf.tobytes()
Example #2
0
    (a, contours, b) = cv2.findContours(edged.copy(), cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)
    contours = sorted(contours, key=cv2.contourArea, reverse = True)[:5]

    ctrs = None

    for c in contours:
        peri = cv2.arcLength(c, True)
        approx = cv2.approxPolyDP(c, 0.02 * peri, True)

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

    # cv2.imshow('outline', img)
    # cv2.waitKey(0)
    # cv2.destroyAllWindows()

    xd = wrap_rectangular(img, ctrs)
    cv2.drawContours(img, [ctrs], -1, (255,0,0), 2)
    cv2.imshow('outline', xd)
    cv2.imshow('img', img)
    cv2.waitKey(0)
    cv2.destroyAllWindows()