2013-04-11 22:34:04 +08:00
|
|
|
#!/usr/bin/env python
|
2012-11-24 02:57:22 +08:00
|
|
|
|
2012-10-17 07:18:30 +08:00
|
|
|
'''
|
|
|
|
Watershed segmentation
|
|
|
|
=========
|
|
|
|
|
|
|
|
This program demonstrates the watershed segmentation algorithm
|
|
|
|
in OpenCV: watershed().
|
|
|
|
|
|
|
|
Usage
|
|
|
|
-----
|
|
|
|
watershed.py [image filename]
|
|
|
|
|
|
|
|
Keys
|
|
|
|
----
|
|
|
|
1-7 - switch marker color
|
|
|
|
SPACE - update segmentation
|
|
|
|
r - reset
|
|
|
|
a - toggle autoupdate
|
|
|
|
ESC - exit
|
|
|
|
|
|
|
|
'''
|
|
|
|
|
|
|
|
|
2015-12-13 09:43:58 +08:00
|
|
|
# Python 2/3 compatibility
|
|
|
|
from __future__ import print_function
|
2012-10-17 07:18:30 +08:00
|
|
|
|
|
|
|
import numpy as np
|
2017-12-11 17:55:03 +08:00
|
|
|
import cv2 as cv
|
2012-10-17 07:18:30 +08:00
|
|
|
from common import Sketcher
|
|
|
|
|
|
|
|
class App:
|
|
|
|
def __init__(self, fn):
|
2017-12-11 17:55:03 +08:00
|
|
|
self.img = cv.imread(fn)
|
2013-03-06 14:41:02 +08:00
|
|
|
if self.img is None:
|
|
|
|
raise Exception('Failed to load image file: %s' % fn)
|
|
|
|
|
2012-10-17 07:18:30 +08:00
|
|
|
h, w = self.img.shape[:2]
|
|
|
|
self.markers = np.zeros((h, w), np.int32)
|
|
|
|
self.markers_vis = self.img.copy()
|
|
|
|
self.cur_marker = 1
|
|
|
|
self.colors = np.int32( list(np.ndindex(2, 2, 2)) ) * 255
|
|
|
|
|
|
|
|
self.auto_update = True
|
|
|
|
self.sketch = Sketcher('img', [self.markers_vis, self.markers], self.get_colors)
|
|
|
|
|
|
|
|
def get_colors(self):
|
2015-12-13 09:43:58 +08:00
|
|
|
return list(map(int, self.colors[self.cur_marker])), self.cur_marker
|
2012-10-17 07:18:30 +08:00
|
|
|
|
|
|
|
def watershed(self):
|
|
|
|
m = self.markers.copy()
|
2017-12-11 17:55:03 +08:00
|
|
|
cv.watershed(self.img, m)
|
2012-10-17 07:18:30 +08:00
|
|
|
overlay = self.colors[np.maximum(m, 0)]
|
2017-12-11 17:55:03 +08:00
|
|
|
vis = cv.addWeighted(self.img, 0.5, overlay, 0.5, 0.0, dtype=cv.CV_8UC3)
|
|
|
|
cv.imshow('watershed', vis)
|
2012-10-17 07:18:30 +08:00
|
|
|
|
|
|
|
def run(self):
|
2017-12-11 17:55:03 +08:00
|
|
|
while cv.getWindowProperty('img', 0) != -1 or cv.getWindowProperty('watershed', 0) != -1:
|
|
|
|
ch = cv.waitKey(50)
|
2012-10-17 07:18:30 +08:00
|
|
|
if ch == 27:
|
|
|
|
break
|
|
|
|
if ch >= ord('1') and ch <= ord('7'):
|
|
|
|
self.cur_marker = ch - ord('0')
|
2015-12-13 09:43:58 +08:00
|
|
|
print('marker: ', self.cur_marker)
|
2012-10-17 07:18:30 +08:00
|
|
|
if ch == ord(' ') or (self.sketch.dirty and self.auto_update):
|
|
|
|
self.watershed()
|
|
|
|
self.sketch.dirty = False
|
|
|
|
if ch in [ord('a'), ord('A')]:
|
|
|
|
self.auto_update = not self.auto_update
|
2015-12-13 09:43:58 +08:00
|
|
|
print('auto_update if', ['off', 'on'][self.auto_update])
|
2012-10-17 07:18:30 +08:00
|
|
|
if ch in [ord('r'), ord('R')]:
|
|
|
|
self.markers[:] = 0
|
|
|
|
self.markers_vis[:] = self.img
|
|
|
|
self.sketch.show()
|
2017-12-11 17:55:03 +08:00
|
|
|
cv.destroyAllWindows()
|
2012-10-17 07:18:30 +08:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
import sys
|
2013-03-06 14:41:02 +08:00
|
|
|
try:
|
|
|
|
fn = sys.argv[1]
|
|
|
|
except:
|
2014-09-13 22:28:41 +08:00
|
|
|
fn = '../data/fruits.jpg'
|
2015-12-13 09:43:58 +08:00
|
|
|
print(__doc__)
|
2012-10-17 07:18:30 +08:00
|
|
|
App(fn).run()
|