2013-03-06 14:41:02 +08:00
|
|
|
#!/usr/bin/env python
|
2012-11-24 02:57:22 +08:00
|
|
|
|
2012-10-17 07:18:30 +08:00
|
|
|
'''
|
|
|
|
Morphology operations.
|
|
|
|
|
|
|
|
Usage:
|
|
|
|
morphology.py [<image>]
|
|
|
|
|
|
|
|
Keys:
|
|
|
|
1 - change operation
|
|
|
|
2 - change structure element shape
|
|
|
|
ESC - exit
|
|
|
|
'''
|
|
|
|
|
2015-09-14 00:00:22 +08:00
|
|
|
# Python 2/3 compatibility
|
|
|
|
from __future__ import print_function
|
|
|
|
import sys
|
|
|
|
PY3 = sys.version_info[0] == 3
|
|
|
|
|
2012-10-17 07:18:30 +08:00
|
|
|
import numpy as np
|
|
|
|
import cv2
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
2015-09-14 00:00:22 +08:00
|
|
|
print(__doc__)
|
2012-10-17 07:18:30 +08:00
|
|
|
|
|
|
|
import sys
|
|
|
|
from itertools import cycle
|
|
|
|
from common import draw_str
|
|
|
|
|
2013-03-06 14:41:02 +08:00
|
|
|
try:
|
|
|
|
fn = sys.argv[1]
|
|
|
|
except:
|
2014-09-13 22:28:41 +08:00
|
|
|
fn = '../data/baboon.jpg'
|
2013-08-27 17:57:24 +08:00
|
|
|
|
2012-10-17 07:18:30 +08:00
|
|
|
img = cv2.imread(fn)
|
2013-08-27 17:57:24 +08:00
|
|
|
|
2013-03-06 14:41:02 +08:00
|
|
|
if img is None:
|
2015-09-14 00:00:22 +08:00
|
|
|
print('Failed to load image file:', fn)
|
2013-03-06 14:41:02 +08:00
|
|
|
sys.exit(1)
|
2013-08-27 17:57:24 +08:00
|
|
|
|
2012-10-17 07:18:30 +08:00
|
|
|
cv2.imshow('original', img)
|
|
|
|
|
|
|
|
modes = cycle(['erode/dilate', 'open/close', 'blackhat/tophat', 'gradient'])
|
|
|
|
str_modes = cycle(['ellipse', 'rect', 'cross'])
|
2015-09-14 00:00:22 +08:00
|
|
|
|
|
|
|
if PY3:
|
|
|
|
cur_mode = next(modes)
|
|
|
|
cur_str_mode = next(str_modes)
|
|
|
|
else:
|
|
|
|
cur_mode = modes.next()
|
|
|
|
cur_str_mode = str_modes.next()
|
2012-10-17 07:18:30 +08:00
|
|
|
|
|
|
|
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()
|
|
|
|
while True:
|
|
|
|
ch = 0xFF & cv2.waitKey()
|
|
|
|
if ch == 27:
|
|
|
|
break
|
|
|
|
if ch == ord('1'):
|
2015-09-14 00:00:22 +08:00
|
|
|
if PY3:
|
|
|
|
cur_mode = next(modes)
|
|
|
|
else:
|
|
|
|
cur_mode = modes.next()
|
2012-10-17 07:18:30 +08:00
|
|
|
if ch == ord('2'):
|
2015-09-14 00:00:22 +08:00
|
|
|
if PY3:
|
|
|
|
cur_str_mode = next(str_modes)
|
|
|
|
else:
|
|
|
|
cur_str_mode = str_modes.next()
|
2012-10-17 07:18:30 +08:00
|
|
|
update()
|
|
|
|
cv2.destroyAllWindows()
|