Python imutils.contours.sort_contours() Examples

The following are 2 code examples of imutils.contours.sort_contours(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module imutils.contours , or try the search function .
Example #1
Source File: utils.py    From answer-sheet-scan with MIT License 6 votes vote down vote up
def get_top_bottom(cnts):
    sort_res = contours.sort_contours(cnts, method="left-to-right")
    cents_pos = sort_res[1]
    que_cnts = list(sort_res[0])
    choice_row_count = get_choice_row_count()
    num = len(cents_pos) - choice_row_count + 1
    dt = {}
    for i in range(num):
        distance = 0
        for j in range(i, i + choice_row_count - 1):
            distance += cents_pos[j + 1][0] - cents_pos[j][0]
        dt[distance] = cents_pos[i:i + choice_row_count]
    keys = dt.keys()
    key_min = min(keys)
    if key_min >= 10:
        raise
    w = sorted(dt[key_min], key=lambda x: x[1])
    top, bottom = w[0][1] - w[0][3] * 0.5, w[-1][1] + w[-1][3] * 0.5
    count = 0
    for i, c in enumerate(cents_pos):
        if c[1] < top or c[1] > bottom:
            que_cnts.pop(i - count)
            count += 1
    return que_cnts 
Example #2
Source File: utils.py    From answer-sheet-scan with MIT License 5 votes vote down vote up
def get_left_right(cnts):
    sort_res = contours.sort_contours(cnts, method="top-to-bottom")
    cents_pos = sort_res[1]
    que_cnts = list(sort_res[0])
    que_cnts = delete_rect(cents_pos, que_cnts)

    sort_res = contours.sort_contours(que_cnts, method="top-to-bottom")
    cents_pos = sort_res[1]
    que_cnts = list(sort_res[0])

    num = len(cents_pos) - CHOICE_COL_COUNT + 1
    dt = {}
    for i in range(num):
        distance = 0
        for j in range(i, i + CHOICE_COL_COUNT - 1):
            distance += cents_pos[j + 1][1] - cents_pos[j][1]
        dt[distance] = cents_pos[i:i + CHOICE_COL_COUNT]
    keys = dt.keys()
    key_min = min(keys)
    if key_min >= 10:
        raise
    w = sorted(dt[key_min], key=lambda x: x[0])
    lt, rt = w[0][0] - w[0][2] * 0.5, w[-1][0] + w[-1][2] * 0.5
    count = 0
    for i, c in enumerate(cents_pos):
        if c[0] < lt or c[0] > rt:
            que_cnts.pop(i - count)
            count += 1
    return que_cnts