conected components
authorTomas Musil <tomik.musil@gmail.com>
Tue, 17 Apr 2012 18:15:28 +0000 (20:15 +0200)
committerTomas Musil <tomik.musil@gmail.com>
Tue, 17 Apr 2012 18:15:28 +0000 (20:15 +0200)
filters.py
imago.py

index 222d8fe..377fa2e 100644 (file)
@@ -3,11 +3,13 @@ from PIL import Image
 from commons import clear
 
 def edge_detection(image):
+    clear()
+    print "edge detection"
+
     image_l = image.load()
     new_image = Image.new('L', image.size)
     new_image_l = new_image.load()
-    clear()
-    print "edge detection"
+    
     for x in range(2, image.size[0] - 2):
         for y in range(2, image.size[1] - 2):
             pix = (sum([sum([
@@ -21,13 +23,15 @@ def edge_detection(image):
                 pix = 0 
             new_image_l[x, y] = pix
     return new_image
-       
+
 def peaks(image):
+    clear()
+    print "peak extraction"
+
     image_l = image.load()
     new_image = Image.new('L', image.size)
     new_image_l = new_image.load()
-    clear()
-    print "peak extraction"
+    
     for x in range(2, image.size[0] - 2):
         for y in range(2, image.size[1] - 2):
             pix = (sum([sum([
@@ -43,11 +47,13 @@ def peaks(image):
     return new_image
 
 def high_pass(image, height):
+    clear()
+    print "high pass filter"
+
     image_l = image.load()
     new_image = Image.new('L', image.size)
     new_image_l = new_image.load()
-    clear()
-    print "high pass filter"
+    
     for x in xrange(image.size[0]):
         for y in xrange(image.size[1]):
             if image_l[x, y] < height:
@@ -56,3 +62,58 @@ def high_pass(image, height):
                 new_image_l[x, y] = image_l[x, y]
 
     return new_image
+
+def components(image):
+    clear()
+    print "components center filter"
+
+    image_l = image.load()
+    new_image = Image.new('L', image.size)
+    new_image_l = new_image.load()
+
+    components = []
+    comp_counter = 0
+
+    for x in xrange(1, image.size[0] - 1):
+        for y in xrange(1, image.size[1] - 1):
+            if image_l[x, y]:
+                s = {0}
+                s.add(new_image_l[x - 1, y - 1])
+                s.add(new_image_l[x, y - 1])
+                s.add(new_image_l[x + 1, y - 1])
+                s.add(new_image_l[x - 1, y])
+                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, c2 = s.pop(), s.pop()
+                    components[c2].add((x, y))
+                    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
\ No newline at end of file
index 45d165c..511027b 100755 (executable)
--- a/imago.py
+++ b/imago.py
@@ -68,15 +68,20 @@ def main():
     im_hough = hough1.transform(im_h)
     if show_all:
         do_something(im_hough, "hough transform")
-               
-       im_hough = filters.peaks(im_hough)
+
+    im_hough = filters.peaks(im_hough)
     if show_all:
         do_something(im_hough, "peak extraction")
-
+               
     im_h2 = filters.high_pass(im_hough, 120)
     if show_all:
         do_something(im_h2, "second high pass filters")
 
+    im_c = filters.components(im_h2)
+    if show_all:
+        do_something(im_c, "components centers")
+
+    """
     hough2 = Hough(im_h2.size)
     im_hough2 = hough2.transform(im_h2)
     if show_all:
@@ -104,6 +109,7 @@ def main():
     collapse(im_c)
     if show_all:
         do_something(im_c, "optimalised hough")
+    """
 
     lines = hough1.all_lines(im_c)
     draw = ImageDraw.Draw(image)