python Sketcher utility class
authorAlexander Mordvintsev <no@email>
Fri, 1 Jul 2011 17:33:29 +0000 (17:33 +0000)
committerAlexander Mordvintsev <no@email>
Fri, 1 Jul 2011 17:33:29 +0000 (17:33 +0000)
samples/python2/common.py
samples/python2/watershed.py

index 5981fa9..0926ab7 100644 (file)
@@ -1,5 +1,5 @@
 import numpy as np\r
-import cv2\r
+import cv2, cv\r
 import os\r
 \r
 image_extensions = ['.bmp', '.jpg', '.jpeg', '.png', '.tif', '.tiff', '.pbm', '.pgm', '.ppm']\r
@@ -57,3 +57,29 @@ def mtx2rvec(R):
 def draw_str(dst, (x, y), s):\r
     cv2.putText(dst, s, (x+1, y+1), cv2.FONT_HERSHEY_PLAIN, 1.0, (0, 0, 0), thickness = 2, linetype=cv2.CV_AA)\r
     cv2.putText(dst, s, (x, y), cv2.FONT_HERSHEY_PLAIN, 1.0, (255, 255, 255), linetype=cv2.CV_AA)\r
+\r
+class Sketcher:\r
+    def __init__(self, windowname, dests, colors_func):\r
+        self.prev_pt = None\r
+        self.windowname = windowname\r
+        self.dests = dests\r
+        self.colors_func = colors_func\r
+        self.dirty = False\r
+        self.show()\r
+        cv2.setMouseCallback(self.windowname, self.on_mouse)\r
+\r
+    def show(self):\r
+        cv2.imshow(self.windowname, self.dests[0])\r
+\r
+    def on_mouse(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
+            for dst, color in zip(self.dests, self.colors_func()):\r
+                cv.Line(dst, self.prev_pt, pt, color, 5)\r
+            self.dirty = True\r
+            self.prev_pt = pt\r
+            self.show()\r
+        else:\r
+            self.prev_pt = None\r
index 6ea20c4..6f72d1c 100644 (file)
@@ -1,5 +1,6 @@
 import numpy as np\r
 import cv2, cv\r
+from common import Sketcher\r
 \r
 help_message = '''\r
   USAGE: watershed.py [<image>]\r
@@ -21,34 +22,17 @@ class App:
         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
+        self.sketch = Sketcher('img', [self.markers_vis, self.markers], self.get_colors)\r
+        \r
+    def get_colors(self):\r
+        return map(int, self.colors[self.cur_marker]), self.cur_marker\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
@@ -58,15 +42,16 @@ class App:
             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
+            if ch == ord(' ') or (self.sketch.dirty and self.auto_update):\r
                 self.watershed()\r
+                self.sketch.dirty = False\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
+                self.markers[:] = 0\r
+                self.markers_vis[:] = self.img\r
+                self.sketch.show()\r
                 cv2.destroyWindow('watershed')\r
 \r
 \r