fix bug in color conversion
[imago.git] / imago_pack / intrsc.py
index 445c447..ce89cb2 100644 (file)
@@ -6,6 +6,7 @@ from operator import itemgetter
 import ImageDraw
 
 import k_means
 import ImageDraw
 
 import k_means
+import output
 
 def dst(line):
     """Return normalized line."""
 
 def dst(line):
     """Return normalized line."""
@@ -76,15 +77,16 @@ def board(image, lines, show_all, do_something):
     
     board_r = []
 
     
     board_r = []
 
+    #TODO 19 should be a size parameter
     try:
         for i in xrange(19):
     try:
         for i in xrange(19):
-            board_r.append([])
             for _ in xrange(19):
             for _ in xrange(19):
-                board_r[i].append(board_rg.next())
+                board_r.append(board_rg.next())
     except StopIteration:
         pass
     except StopIteration:
         pass
+    
 
 
-    return board_r
+    return output.Board(19, board_r)
 
 def mean_luma(cluster):
     return sum(c[0][0] for c in cluster) / float(len(cluster))
 
 def mean_luma(cluster):
     return sum(c[0][0] for c in cluster) / float(len(cluster))
@@ -106,6 +108,14 @@ def intersections_from_angl_dist(lines, size, get_all=True):
         intersections.append(line)
     return intersections
    
         intersections.append(line)
     return intersections
    
+def RGBtoSat(c):
+    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))
+
 def stone_color_raw(image, (x, y)):
     """Given image and coordinates, return stone color."""
     suma = []
 def stone_color_raw(image, (x, y)):
     """Given image and coordinates, return stone color."""
     suma = []
@@ -117,8 +127,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.
                 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(max(s) - min(s) / float(255. - abs(max(s) + min(s) - 255))
-                     for s in suma) / (25. * 255.)
+    saturation = sum(RGBtoSat(s) for s in suma) / (25. * 255.)
     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
     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