Ejemplo n.º 1
0
def run_filter(img_name, filter_param):
    # src
    img_cursor = md.loadimg(md.src, img_name)
    result_images = []

    # prev filter
    img_cursor, result_images = eachFileter(img_cursor, result_images,
                                            filter_param['prev-filter'],
                                            'prev', img_name)

    # gray
    img_cursor = cv2.cvtColor(img_cursor, cv2.COLOR_BGRA2GRAY)
    tmp = md.savetemp('filter', 'gray-' + img_name, img_cursor)
    result_images.append(tmp)

    # after filter
    img_cursor, result_images = eachFileter(img_cursor, result_images,
                                            filter_param['after-filter'],
                                            'after', img_name)

    # cany
    img_cursor = cv2.Canny(img_cursor, filter_param['cany-0'],
                           filter_param['cany-1'])
    tmp = md.savetemp('filter', 'cany-' + img_name, img_cursor)
    result_images.append(tmp)

    # dilate
    dilate_param = filter_param['dilate']
    if dilate_param['on']:
        kernel_size = int(dilate_param['kernel'])
        kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE,
                                           (kernel_size, kernel_size))
        img_cursor = cv2.dilate(img_cursor,
                                kernel=kernel,
                                iterations=dilate_param['it'])
        tmp = md.savetemp('filter', 'dilate-' + img_name, img_cursor)
        result_images.append(tmp)

    contours, _ = cv2.findContours(img_cursor, cv2.RETR_LIST,
                                   cv2.cv2.CHAIN_APPROX_NONE)
    lstCoords = [md.contour2coords(contour) for contour in contours]

    # poly
    polydp = filter_param['polydp']
    polies = [
        md.contour2coords(cv2.approxPolyDP(contour, polydp, False))
        for contour in contours
    ]

    return {'images': result_images, 'lst-coords': lstCoords, 'polies': polies}
Ejemplo n.º 2
0
def proccess1(name):
    src = mdt.loadimg(mdt.src, name)
    height, width = src.shape[:2]  # 이미지 크기 구하기
    mn = min(height, width)
    xxsm = int(mn * 0.01)
    xsm = int(mn * 0.05)
    sm = int(mn * 0.1)
    smm = int(mn * 0.3)
    md = int(mn * 0.4)
    xmd = int(mn * 0.4)

    gblur = cv2.GaussianBlur(src, (7, 7), 1)
    mblur = cv2.medianBlur(gblur, 7)
    mgray = cv2.cvtColor(mblur, cv2.COLOR_BGR2GRAY)
    mcany = cv2.Canny(mgray, 30, 40)
    dkernel = np.array([[0, 1, 0], [0, 1, 0], [0, 1, 0]], dtype=np.uint8)
    dilate = cv2.dilate(mcany, dkernel, iterations=3)
    contours, _ = cv2.findContours(dilate, cv2.RETR_LIST,
                                   cv2.CHAIN_APPROX_SIMPLE)

    # cpoly = np.zeros(shape=(height, width), dtype=np.uint8)
    polies = [cv2.approxPolyDP(con, 2, True) for con in contours]
    # cv2.drawContours(cpoly, polies, -1, cvcolor.white, 1)

    # dpoly = np.zeros(shape=(height, width), dtype=np.uint8)
    dpolies = [poly for poly in polies if len(poly) >= 2]
    # cv2.drawContours(dpoly, dpolies, -1, cvcolor.white, 1)

    verlines = [
        mdt.coords2contour(ver) for poly in dpolies
        for ver in mdt.coords_split(mdt.contour2coords(poly))['ver']
    ]
    cverline = np.zeros(shape=(height, width), dtype=np.uint8)
    cv2.drawContours(cverline, verlines, -1, cvcolor.white, 1)

    dverline = np.zeros(shape=(height, width, 3), dtype=np.uint8)
    for ver in verlines:
        if len(ver) == 1:
            cv2.drawContours(dverline, [ver], -1, cvcolor.red, 1)
            pass
        elif len(ver) == 2:
            cv2.drawContours(dverline, [ver], -1, cvcolor.cyan, 1)
        else:
            cv2.drawContours(dverline, [ver], -1, cvcolor.gray, 1)

    for ver in verlines:
        if len(ver) > 2:
            rect = cv2.minAreaRect(ver)
            ((x, y), (w, h), angle) = rect
            area = (0, h) if w < h else (w, 0)
            box = cv2.boxPoints(((x, y), area, angle))
            box = np.int32([box[1], box[2]] if (
                box[0][0] == box[1][0] and box[0][1] == box[1][1]
            ) else [box[0], box[1]])
            cv2.drawContours(dverline, [box], -1, cvcolor.yellow, 1)

    def 뭉치기(contour):
        if len(contour) == 2: return contour
        else:
            rect = cv2.minAreaRect(contour)
            ((x, y), (w, h), angle) = rect
            area = (0, h) if w < h else (w, 0)
            box = cv2.boxPoints(((x, y), area, angle))
            box = np.int32([[box[1]], [box[2]]] if (
                box[0][0] == box[1][0] and box[0][1] == box[1][1]
            ) else [[box[0]], [box[1]]])
            return box

    verlines = [뭉치기(ver) for ver in verlines if len(ver) > 1]
    comcan = np.zeros(shape=(height, width), dtype=np.uint8)
    cv2.drawContours(comcan, verlines, -1, cvcolor.white, 1)

    wcany = cv2.Canny(mgray, 30, 230)

    return {
        'images': [
            {
                'name': key,
                'image': mdt.savetemp('gorun', key + '-' + name, img)
            } for key, img in {
                'mcany': mcany,
                'dilate': dilate,
                # 'cpoly': cpoly,
                # 'dpoly': dpoly,
                'cverline': cverline,
                'dverline': dverline,
                'comcan': comcan,
                'wcany': wcany
            }.items()
        ],
        'contours': [{
            'name': key,
            'contours': contours
        } for key, contours in {
            'polies': [poly.tolist() for poly in polies],
            'dpolies': [dpoly.tolist() for dpoly in dpolies],
            'verlines': [ver.tolist() for ver in verlines].sort()
        }.items()]
    }
Ejemplo n.º 3
0
#필터이미지 생성
@app.route('/run-filter', methods=['GET', 'POST'])
def img_filter():
    data = md.jsonparse(request.data)
    result = mfilter.run_filter(data['image'], data['filter-param'])
    return jsonify([result])


@app.route('/go-run-task', methods=['GET', 'POST'])
def go_run_task():
    data = md.jsonparse(request.data)
    result = gorun.proccess2(data['image'])
    return jsonify(result)


for name in md.listing(md.uploads):
    print(name)
    img = md.loadimg(md.uploads, name)
    md.saveimg(md.src, name.split('.')[0] + '.png', img)
    _, width, _ = img.shape
    ratio = 150 / width
    md.saveimg('thum',
               name.split('.')[0] + '.png',
               cv2.resize(img, None, fx=ratio, fy=ratio))

# 앱 구동
if __name__ == "__main__":
    # Only for debugging while developing

    app.run(host='0.0.0.0', debug=True, port=80)
Ejemplo n.º 4
0
def proccess2(name):
    src = mdt.loadimg(mdt.src, name)
    sample = mdt.scalingWdith(src, 500)

    gblur = cv2.GaussianBlur(sample, (5, 5), 1)
    gray = cv2.cvtColor(gblur, cv2.COLOR_BGR2GRAY)
    cany = cv2.Canny(gray, 30, 230)
    contours, _ = cv2.findContours(cany, cv2.RETR_LIST,
                                   cv2.CHAIN_APPROX_SIMPLE)

    polies = [cv2.approxPolyDP(con, 5, True) for con in contours]
    polies = [poly for poly in polies if len(poly) >= 2]
    polies_canvas = np.zeros(shape=cany.shape, dtype=np.uint8)
    cv2.drawContours(polies_canvas, polies, -1, cvcolor.white, 1)

    vlines = [
        mdt.coords2contour(ver) for poly in polies
        for ver in mdt.coords_split(mdt.contour2coords(poly))['ver']
    ]
    vlines = [poly for poly in vlines if len(poly) >= 2]
    vline_canvas = np.zeros(shape=cany.shape, dtype=np.uint8)
    cv2.drawContours(vline_canvas, vlines, -1, cvcolor.white, 1)

    def vline_merge(contour):
        if len(contour) == 2: return contour
        else:
            rect = cv2.minAreaRect(contour)
            ((x, y), (w, h), angle) = rect
            area = (0, h) if w < h else (w, 0)
            box = cv2.boxPoints(((x, y), area, angle))
            box = np.int32([[box[1]], [box[2]]] if (
                box[0][0] == box[1][0] and box[0][1] == box[1][1]
            ) else [[box[0]], [box[1]]])
            return box

    merged_vlines = [vline_merge(ver) for ver in vlines]
    mvlines_canvas = np.zeros(shape=cany.shape, dtype=np.uint8)
    cv2.drawContours(mvlines_canvas, merged_vlines, -1, cvcolor.white, 1)

    return {
        'images': [{
            'name': key,
            'image': mdt.savetemp('gorun', key + '-' + name, img)
        } for key, img in {
            'gblur': gblur,
            'gray': gray,
            'cany': cany,
            'polies': polies_canvas,
            'vlines': vline_canvas,
            'mergerd_vlines': mvlines_canvas
        }.items()],
        'contours': [{
            'name': key,
            'contours': contours
        } for key, contours in {
            'controus': [contour.tolist() for contour in contours],
            'polies': [poly.tolist() for poly in polies],
            'vlines': [vline.tolist() for vline in vlines],
            'merged_vlines': [vline.tolist() for vline in merged_vlines]
        }.items()]
    }
Ejemplo n.º 5
0
def bend(name, coord, bound):
    img = md.loadimg(md.src, name)
    height, width = img.shape[:2]

    controus = md.coords2contour(coord)
    corners = coords4corner(coord)

    left = bound['left']
    top = bound['top']
    right = bound['left'] + bound['width']
    bottom = bound['top'] + bound['height']

    M = cv2.getPerspectiveTransform(
        np.float32([[p['x'], p['y']] for p in corners]),
        np.float32([
            [ left, top ],
            [ right, top],
            [right, bottom],
            [left, bottom]
        ])
    )

    canvas = np.zeros(shape=(height, width), dtype=np.uint8)
    cv2.drawContours(canvas, [controus], -1, cvcolor.white, 2)
    canvas = cv2.warpPerspective(canvas, M, (
        bound['width'] + bound['left'] * 2,
        bound['height'] + bound['top'] * 2
    ))
    img = cv2.warpPerspective(img, M, (
        bound['width'] + bound['left'] * 2,
        bound['height'] + bound['top'] * 2
    ))

    # 10간격으로 위선을 다시 잘라 봅니다.
    verline = []
    for i in range(left, right + 1, 10):
        ver = []
        for y in range(0, bottom + top):
            if not canvas[y, i] == 0:
                cv2.circle(canvas, (i, y), 5, cvcolor.white, 2)
                ver.append([[i, y]])
                break
        for y in range(bottom + top - 1, -1, -1):
            if not canvas[y, i] == 0:
                cv2.circle(canvas, (i, y), 5, cvcolor.white, 2)
                ver.append([[i, y]])
                break
        verline.append(ver)

    rects = [np.array([verline[i][0], verline[i+1][0], verline[i+1][1], verline[i][1]]) for i in range(0, len(verline)-1)]
    cv2.drawContours(canvas, rects, -1, cvcolor.white, 2)

    sp = []
    for rect in rects:
        M = cv2.getPerspectiveTransform(
            np.float32([[p[0][0], p[0][1]] for p in rect]),
            np.float32([[0, 0], [10, 0], [10, bottom - top], [0, bottom - top]])
        )
        sp.append(cv2.warpPerspective(img, M, (10, bottom - top)))

    return {
        'flat': np.hstack(sp),
        'frame': canvas
    }