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
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
import numpy as np\r
import cv2, cv\r
+from common import Sketcher\r
\r
help_message = '''\r
USAGE: watershed.py [<image>]\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
+ 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
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