watershed.py sample added
authorAlexander Mordvintsev <no@email>
Fri, 1 Jul 2011 15:09:38 +0000 (15:09 +0000)
committerAlexander Mordvintsev <no@email>
Fri, 1 Jul 2011 15:09:38 +0000 (15:09 +0000)
samples/python2/watershed.py [new file with mode: 0644]

diff --git a/samples/python2/watershed.py b/samples/python2/watershed.py
new file mode 100644 (file)
index 0000000..6ea20c4
--- /dev/null
@@ -0,0 +1,78 @@
+import numpy as np\r
+import cv2, cv\r
+\r
+help_message = '''\r
+  USAGE: watershed.py [<image>]\r
+\r
+  Use keys 1 - 7 to switch marker color\r
+  SPACE - update segmentation\r
+  r     - reset\r
+  a     - switch autoupdate\r
+  ESC   - exit\r
+\r
+'''\r
+\r
+class App:\r
+    def __init__(self, fn):\r
+        self.img = cv2.imread(fn)\r
+        h, w = self.img.shape[:2]\r
+        self.markers = np.zeros((h, w), np.int32)\r
+        self.markers_vis = self.img.copy()\r
+        self.cur_marker = 1\r
+        self.colors = np.int32( list(np.ndindex(2, 2, 2)) ) * 255\r
+\r
+        cv2.imshow('img', self.markers_vis)\r
+\r
+        self.prev_pt = None\r
+        self.need_update = False\r
+        self.auto_update = True\r
+        cv2.setMouseCallback('img', self.onmouse)\r
+\r
+\r
+    def onmouse(self, event, x, y, flags, param):\r
+        pt = (x, y)\r
+        if event == cv.CV_EVENT_LBUTTONDOWN:\r
+            self.prev_pt = pt\r
+        if self.prev_pt and flags & cv.CV_EVENT_FLAG_LBUTTON:\r
+            color = map(int, self.colors[self.cur_marker])\r
+            cv.Line(self.markers, self.prev_pt, pt, self.cur_marker, 5)\r
+            cv.Line(self.markers_vis, self.prev_pt, pt, color, 5)\r
+            self.need_update = True\r
+            self.prev_pt = pt\r
+            cv2.imshow('img', self.markers_vis)\r
+        else:\r
+            self.prev_pt = None\r
+\r
+    def watershed(self):\r
+        m = self.markers.copy()\r
+        cv2.watershed(self.img, m)\r
+        vis = np.uint8( (self.img + self.colors[np.maximum(m, 0)]) / 2 )\r
+        cv2.imshow('watershed', vis)\r
+        self.need_update = False\r
+\r
+    def run(self):\r
+        while True:\r
+            ch = cv2.waitKey(10)\r
+            if ch == 27:\r
+                break\r
+            if ch >= ord('1') and ch <= ord('7'):\r
+                self.cur_marker = ch - ord('0')\r
+                print 'marker: ', self.cur_marker\r
+            if ch == ord(' ') or (self.need_update and self.auto_update):\r
+                self.watershed()\r
+            if ch in [ord('a'), ord('A')]:\r
+                self.auto_update = not self.auto_update\r
+                print 'auto_update if', ['off', 'on'][self.auto_update]\r
+            if ch in [ord('r'), ord('R')]:\r
+                self.markers = np.zeros(self.img.shape[:2], np.int32)\r
+                self.markers_vis = self.img.copy()\r
+                cv2.imshow('img', self.markers_vis)\r
+                cv2.destroyWindow('watershed')\r
+\r
+\r
+if __name__ == '__main__':\r
+    import sys\r
+    try: fn = sys.argv[1]\r
+    except: fn = '../cpp/fruits.jpg'\r
+    print help_message\r
+    App(fn).run()\r