X-Git-Url: http://git.tomasm.cz/imago.git/blobdiff_plain/ab71bc75ef14e2e731e92274b67988f6c0174061..f0548db6acfd9616ddafa443e45103fc6655e0e4:/filters.py?ds=inline diff --git a/filters.py b/filters.py index a5d00e6..c53b6e8 100644 --- a/filters.py +++ b/filters.py @@ -1,23 +1,13 @@ -from PIL import Image +from PIL import Image, ImageFilter + +import pcf def edge_detection(image): - image_l = image.load() - new_image = Image.new('L', image.size) - new_image_l = new_image.load() - - for x in xrange(2, image.size[0] - 2): - for y in xrange(2, image.size[1] - 2): - pix = (sum([sum([ - image_l[a, b] - for b in range(y - 2, y + 3)]) - for a in range(x - 2, x + 3)]) - - (25 * image_l[x, y])) - if pix > 255: - pix = 255 - if pix < 0: - pix = 0 - new_image_l[x, y] = pix - return new_image + image = image.filter(ImageFilter.GaussianBlur()) + # GaussianBlur is undocumented class, it might not work in future versions + # of PIL + image = Image.fromstring('L', image.size, pcf.edge(image.size, image.tostring())) + return image def peaks(image): image_l = image.load() @@ -30,7 +20,7 @@ def peaks(image): - image_l[a, b] for b in range(y - 2, y + 3)]) for a in range(x - 2, x + 3)]) - + (16 * image_l[x, y])) + + (17 * image_l[x, y])) if pix > 255: pix = 255 if pix < 0: @@ -57,8 +47,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,37 +89,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 half_centers(image): +def components2(image): image_l = image.load() new_image = Image.new('L', image.size) new_image_l = new_image.load() - - x_s = 0 - y_s = 0 - n = 0 - for x in range(0, image.size[0] / 2): - for y in range(0, image.size[1]): - if image_l[x, y] > 127: - x_s += x - y_s += y - n += 1 - new_image_l[x_s / n, y_s / n] = 255 - - x_s = 0 - y_s = 0 - n = 0 - for x in range(image.size[0] / 2, image.size[0]): - for y in range(0, image.size[1]): - if image_l[x, y] > 127: - x_s += x - y_s += y - n += 1 - new_image_l[x_s / n, y_s / n] = 255 - return new_image + 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