diff --git a/samples/python2/common.py b/samples/python2/common.py index b32d60b136..5981fa99c1 100644 --- a/samples/python2/common.py +++ b/samples/python2/common.py @@ -54,3 +54,6 @@ def mtx2rvec(R): axis = np.cross(vt[0], vt[1]) return axis * np.arctan2(s, c) +def draw_str(dst, (x, y), s): + cv2.putText(dst, s, (x+1, y+1), cv2.FONT_HERSHEY_PLAIN, 1.0, (0, 0, 0), thickness = 2, linetype=cv2.CV_AA) + cv2.putText(dst, s, (x, y), cv2.FONT_HERSHEY_PLAIN, 1.0, (255, 255, 255), linetype=cv2.CV_AA) diff --git a/samples/python2/morphology.py b/samples/python2/morphology.py index f651f4f421..5192ba4be4 100644 --- a/samples/python2/morphology.py +++ b/samples/python2/morphology.py @@ -1,19 +1,58 @@ import numpy as np import cv2 -img = cv2.imread('../cpp/baboon.jpg', False) -def callback(k): - k = 2*(k-10) - st = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (abs(k), abs(k))) - op = cv2.MORPH_BLACKHAT - if k > 0: - op = cv2.MORPH_TOPHAT - res = cv2.morphologyEx(img, op, st) - cv2.imshow('img', res) +if __name__ == '__main__': + import sys + from itertools import cycle + from common import draw_str -callback(20) -cv2.createTrackbar('k', 'img', 10, 20, callback) + try: fn = sys.argv[1] + except: fn = '../cpp/baboon.jpg' + img = cv2.imread(fn) + modes = cycle(['erode/dilate', 'open/close', 'blackhat/tophat', 'gradient']) + str_modes = cycle(['ellipse', 'rect', 'cross']) + cur_mode = modes.next() + cur_str_mode = str_modes.next() -cv2.waitKey() + def update(dummy=None): + sz = cv2.getTrackbarPos('op/size', 'morphology') + iters = cv2.getTrackbarPos('iters', 'morphology') + opers = cur_mode.split('/') + if len(opers) > 1: + sz = sz - 10 + op = opers[sz > 0] + sz = abs(sz) + else: + op = opers[0] + sz = sz*2+1 + + str_name = 'MORPH_' + cur_str_mode.upper() + oper_name = 'MORPH_' + op.upper() + st = cv2.getStructuringElement(getattr(cv2, str_name), (sz, sz)) + res = cv2.morphologyEx(img, getattr(cv2, oper_name), st, iterations=iters) + + draw_str(res, (10, 20), 'mode: ' + cur_mode) + draw_str(res, (10, 40), 'operation: ' + oper_name) + draw_str(res, (10, 60), 'structure: ' + str_name) + draw_str(res, (10, 80), 'ksize: %d iters: %d' % (sz, iters)) + cv2.imshow('morphology', res) + + cv2.namedWindow('morphology') + cv2.createTrackbar('op/size', 'morphology', 12, 20, update) + cv2.createTrackbar('iters', 'morphology', 1, 10, update) + update() + print "Controls:" + print " 1 - change operation" + print " 2 - change structure element shape" + print + while True: + ch = cv2.waitKey() + if ch == 27: + break + if ch == ord('1'): + cur_mode = modes.next() + if ch == ord('2'): + cur_str_mode = str_modes.next() + update()