minor changes
authorTomas Musil <tomik.musil@gmail.com>
Sat, 25 Aug 2012 18:17:07 +0000 (20:17 +0200)
committerTomas Musil <tomik.musil@gmail.com>
Sat, 25 Aug 2012 18:23:38 +0000 (20:23 +0200)
filters.py
imago.py
tests/batch

index 2f3c12d..c2648ea 100644 (file)
@@ -57,8 +57,8 @@ def components(image):
     new_image = Image.new('L', image.size)
     new_image_l = new_image.load()
 
-    components = []
-    comp_counter = 0
+    components = [None]
+    comp_counter = 1
 
     for y in xrange(1, image.size[1] - 1):
         for x in xrange(1, image.size[0] - 1):
@@ -99,7 +99,63 @@ def components(image):
                 x_c += x
                 y_c += y
                 c += 1
-        new_image_l[int(round(float(x_c)/c)), int(round(float(y_c)/c))] = 255
+            new_image_l[int(round(float(x_c)/c)), int(round(float(y_c)/c))] = 255
+
+
+    return new_image
+
+def components2(image):
+    image_l = image.load()
+    new_image = Image.new('L', image.size)
+    new_image_l = new_image.load()
+
+    components = [None]
+    comp_counter = 1
+
+    for y in xrange(2, image.size[1] - 2):
+        for x in xrange(2, image.size[0] - 2):
+            if image_l[x, y]:
+
+                s = {0}
+                for (a, b) in [(a,b) for a in range(x - 2, x + 3) 
+                          for b in range(y - 2, y + 1)]:
+                    if not (b == y and a >= x):
+                        s.add(new_image_l[a, b])
+
+                if len(s) == 1:
+                    components.append(set())
+                    new_image_l[x, y] = comp_counter
+                    components[comp_counter].add((x, y))
+                    comp_counter += 1
+                elif len(s) == 2:
+                    s.remove(0)
+                    c = s.pop()
+                    new_image_l[x, y] = c
+                    components[c].add((x,y))
+                else:
+                    s.remove(0)
+                    c1 = s.pop()
+                    components[c1].add((x, y))
+                    new_image_l[x, y] = c1
+                    for c2 in s:
+                        for (x1, y1) in components[c2]:
+                            new_image_l[x1, y1] = c1
+                        components[c1] = components[c1] | components[c2]
+                        components[c2] = None
+
+    new_image = Image.new('L', image.size)
+    new_image_l = new_image.load()
+
+    for component in components:
+        if component:
+            x_c = 0
+            y_c = 0
+            c = 0
+            for (x, y) in component:
+                x_c += x
+                y_c += y
+                c += 1
+            new_image_l[int(round(float(x_c)/c)), int(round(float(y_c)/c))] = 255
 
 
     return new_image
index 3c4ca56..87eaecf 100755 (executable)
--- a/imago.py
+++ b/imago.py
@@ -93,7 +93,7 @@ def main():
     if show_all:
         do_something(im_h2, "second high pass filters")
 
-    im_h2 = filters.components(im_h2)
+    im_h2 = filters.components2(im_h2)
     if show_all:
         do_something(im_h2, "components centers")
 
@@ -129,8 +129,8 @@ def main():
         draw = ImageDraw.Draw(im_line)
         line_points = set()
         for line in line_l:
-            draw.line(line_from_angl_dist(line, im_h2.size), fill=255, width=5)
-            draw_c.line(line_from_angl_dist(line, im_c.size), fill=(70, 70, 70), width=5)
+            draw.line(line_from_angl_dist(line, im_h2.size), fill=255, width=7)
+            draw_c.line(line_from_angl_dist(line, im_c.size), fill=(70, 70, 70), width=7)
             for p in combine(im_h2, im_line):
                 line_points.add(p)
         for point in line_points:
index 37e8e01..5ca0057 100755 (executable)
@@ -1,5 +1,6 @@
 #!/bin/bash
 
-for file in "$@" ; do
+for file in "$1/"*".jpg" ; do
+       echo $file":"
        ../imago.py -vds -w 640 $file > "${file%.jpg}.txt"
 done