From a5500034cf027ff6fec0d1dc236b36d3b1d82778 Mon Sep 17 00:00:00 2001 From: Tomas Musil Date: Tue, 22 Oct 2013 14:53:41 +0200 Subject: [PATCH 1/1] better clustering --- imago_pack/intrsc.py | 18 ++++++++++++------ imago_pack/k_means.py | 5 +++-- 2 files changed, 15 insertions(+), 8 deletions(-) diff --git a/imago_pack/intrsc.py b/imago_pack/intrsc.py index ce89cb2..04ef350 100644 --- a/imago_pack/intrsc.py +++ b/imago_pack/intrsc.py @@ -50,12 +50,16 @@ def board(image, lines, show_all, do_something): if show_all: import matplotlib.pyplot as pyplot pyplot.scatter(luma, saturation, - color=[(s[2][0]/255., s[2][1]/255., s[2][2]/255., 1.) - for s in board_raw]) + color=[(s[2][0]/255., + s[2][1]/255., + s[2][2]/255., 1.) + for s in board_raw]) + pyplot.xlim(0,1) + pyplot.ylim(0,1) pyplot.show() clusters = k_means.cluster(3, 2,zip(zip(luma, saturation), range(len(luma))), - [[0., 0.], [0.5, 0.5], [1., 1.]]) + [[0., 0.], [0.5, 0.5], [1., 0.]]) #clusters.sort(key=mean_luma) if show_all: @@ -65,6 +69,8 @@ def board(image, lines, show_all, do_something): color=(0,1,0,1)) pyplot.scatter([d[0][0] for d in clusters[2]], [d[0][1] for d in clusters[2]], color=(0,0,1,1)) + pyplot.xlim(0,1) + pyplot.ylim(0,1) pyplot.show() clusters[0] = [(p[1], 'B') for p in clusters[0]] @@ -109,12 +115,12 @@ def intersections_from_angl_dist(lines, size, get_all=True): return intersections def RGBtoSat(c): + """Using the HSI color model.""" max_diff = max(c) - min(c) if max_diff == 0: return 0 else: - #TODO simplify this - return max_diff / float(255. - abs(max(c) + min(c) - 255)) + return 1. - ((3. * min(c)) / sum(c)) def stone_color_raw(image, (x, y)): """Given image and coordinates, return stone color.""" @@ -127,7 +133,7 @@ def stone_color_raw(image, (x, y)): pass luma = sum([0.30 * sum(s[0] for s in suma) / 25., 0.59 * sum(s[1] for s in suma) / 25., 0.11 * sum(s[2] for s in suma) / 25.]) / 255. - saturation = sum(RGBtoSat(s) for s in suma) / (25. * 255.) + saturation = sum(RGBtoSat(s) for s in suma) / 25. color = [sum(s[0] for s in suma) / 25., sum(s[1] for s in suma) / 25., sum(s[2] for s in suma) / 25.] return luma, saturation, color diff --git a/imago_pack/k_means.py b/imago_pack/k_means.py index 0019597..c5ddba5 100644 --- a/imago_pack/k_means.py +++ b/imago_pack/k_means.py @@ -1,8 +1,9 @@ -"""K-means module""" +"""K-means module.""" import random def cluster(k, d, data, i_centers=None): + """Find *k* clusters on *d* dimensional *data*.""" borders = [(min(p[0][i] for p in data), max(p[0][i] for p in data)) for i in range(d) ] if i_centers: @@ -39,5 +40,5 @@ def centroid(cluster): return [sum(c[0][i] for c in cluster) / l for i in range(d)] def delta(c1, c2): - return sum( (sum(abs(cc1 - cc2) for (cc1, cc2) in zip (ccc1, ccc2)) if ccc2 + return sum((sum(abs(cc1 - cc2) for (cc1, cc2) in zip (ccc1, ccc2)) if ccc2 else 0.) for (ccc1, ccc2) in zip(c1, c2)) -- 2.4.2