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}
def eachFileter(img_cursor, result_images, cmdFilter, tag, name): for idx, filter in enumerate(cmdFilter): param = filter['param'] if filter['name'] == 'box': ksize = int(param['ksize']) img_cursor = cv2.boxFilter(img_cursor, ddepth=-1, ksize=ksize) elif filter['name'] == 'bilateral': d = int(param['d']) sColor = int(param['s-color']) sSpace = int(param['s-space']) img_cursor = cv2.bilateralFilter(img_cursor, d, sColor, sSpace) elif filter['name'] == 'median': ksize = int(param['ksize']) img_cursor = cv2.medianBlur(img_cursor, ksize=ksize) elif filter['name'] == 'blur': ksize = int(param['ksize']) img_cursor = cv2.blur(img_cursor, ksize=(ksize, ksize)) elif filter['name'] == 'gaussian': ksize = int(param['ksize']) sigmaX = int(param['sigmaX']) img_cursor = cv2.GaussianBlur(img_cursor, (ksize, ksize), sigmaX) tmp = md.savetemp('filter', '%s-%d-%s' % (tag, idx, name), img_cursor) result_images.append(tmp) return img_cursor, result_images
def img_banding(): data = md.jsonparse(request.data) result = bender.bend(data['image'], data['coord'], data['bound']) send = [] for key in result: md.wipetemp(key, data['image']) tmp = md.savetemp(key, data['image'], result[key]) send.append(key + '/' + tmp) return jsonify(send)
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()] }
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()] }