self.show()\r
else:\r
self.prev_pt = None\r
+\r
+\r
+# palette data from matplotlib/_cm.py\r
+_jet_data = {'red': ((0., 0, 0), (0.35, 0, 0), (0.66, 1, 1), (0.89,1, 1),\r
+ (1, 0.5, 0.5)),\r
+ 'green': ((0., 0, 0), (0.125,0, 0), (0.375,1, 1), (0.64,1, 1),\r
+ (0.91,0,0), (1, 0, 0)),\r
+ 'blue': ((0., 0.5, 0.5), (0.11, 1, 1), (0.34, 1, 1), (0.65,0, 0),\r
+ (1, 0, 0))}\r
+\r
+cmap_data = { 'jet' : _jet_data }\r
+\r
+def make_cmap(name, n=256):\r
+ data = cmap_data[name]\r
+ xs = np.linspace(0.0, 1.0, n)\r
+ channels = []\r
+ eps = 1e-6\r
+ for ch_name in ['blue', 'green', 'red']:\r
+ ch_data = data[ch_name]\r
+ xp, yp = [], []\r
+ for x, y1, y2 in ch_data:\r
+ xp += [x, x+eps]\r
+ yp += [y1, y2]\r
+ ch = np.interp(xs, xp, yp)\r
+ channels.append(ch)\r
+ return np.uint8(np.array(channels).T*255)\r
--- /dev/null
+import numpy as np\r
+import cv2, cv\r
+from common import make_cmap\r
+\r
+help_message = '''USAGE: distrans.py [<image>]\r
+\r
+Keys:\r
+ ESC - exit\r
+ v - toggle voronoi mode\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
+\r
+ img = cv2.imread(fn, 0)\r
+ cm = make_cmap('jet')\r
+ need_update = True\r
+ voronoi = False\r
+\r
+ def update(dummy=None):\r
+ global need_update\r
+ need_update = False\r
+ thrs = cv2.getTrackbarPos('threshold', 'distrans')\r
+ mark = cv2.Canny(img, thrs, 3*thrs)\r
+ dist, labels = cv2.distanceTransform(~mark, cv.CV_DIST_L2, 5)\r
+ if voronoi:\r
+ vis = cm[np.uint8(labels)]\r
+ else:\r
+ vis = cm[np.uint8(dist*2)]\r
+ vis[mark != 0] = 255\r
+ cv2.imshow('distrans', vis)\r
+\r
+ def invalidate(dummy=None):\r
+ global need_update\r
+ need_update = True\r
+\r
+ cv2.namedWindow('distrans') \r
+ cv2.createTrackbar('threshold', 'distrans', 60, 255, invalidate)\r
+ update()\r
+\r
+\r
+ while True:\r
+ ch = cv2.waitKey(50)\r
+ if ch == 27:\r
+ break\r
+ if ch == ord('v'):\r
+ voronoi = not voronoi\r
+ print 'showing', ['distance', 'voronoi'][voronoi]\r
+ update()\r
+ if need_update:\r
+ update()\r
+ \r
gray = cv2.cvtColor(img, cv.CV_BGR2GRAY)\r
thrs1 = cv2.getTrackbarPos('thrs1', 'edge')\r
thrs2 = cv2.getTrackbarPos('thrs2', 'edge')\r
- edge = cv2.canny(gray, thrs1, thrs2, apertureSize=5)\r
+ edge = cv2.Canny(gray, thrs1, thrs2, apertureSize=5)\r
vis = img.copy()\r
vis /= 2\r
vis[edge != 0] = (0, 255, 0)\r