From: Tomas Musil Date: Fri, 27 Jun 2014 13:25:08 +0000 (+0200) Subject: refactored image component filters X-Git-Url: http://git.tomasm.cz/imago.git/commitdiff_plain/b8d894cc2b9cf2b381560e47e6a35fbda2edba59?ds=inline;hp=45b6aba3bbf3aea1bacad077387ae77a1deb1a3c refactored image component filters --- diff --git a/imago_pack/filters.py b/imago_pack/filters.py index 50a9a01..a879ee9 100644 --- a/imago_pack/filters.py +++ b/imago_pack/filters.py @@ -32,7 +32,6 @@ def color_enhance(image): g = (g - min_g) * 255 / (max_g - min_g) b = (b - min_b) * 255 / (max_b - min_b) new_image_l[x, y] = (r, g, b) - # print min_r, max_r, r, g, b return new_image @@ -80,9 +79,9 @@ def high_pass(image, height): return new_image -# TODO factor these into one method -# TODO comment it -def components(image): +def components(image, diameter): + # TODO comment + # TODO refactor image_l = image.load() new_image = Image.new('L', image.size) new_image_l = new_image.load() @@ -90,87 +89,67 @@ def components(image): components = [None] comp_counter = 1 - for y in xrange(1, image.size[1] - 1): - for x in xrange(1, image.size[0] - 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 - -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: + if diameter == 1: + for y in xrange(1, image.size[1] - 1): + for x in xrange(1, image.size[0] - 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 + elif diameter == 2: + 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 + else: + pass #TODO error + new_image = Image.new('L', image.size) new_image_l = new_image.load() @@ -187,3 +166,4 @@ def components2(image): new_image_l[int(round(float(x_c)/c)), int(round(float(y_c)/c))] = 255 return new_image + diff --git a/imago_pack/linef.py b/imago_pack/linef.py index 47952c2..ccf209e 100644 --- a/imago_pack/linef.py +++ b/imago_pack/linef.py @@ -38,7 +38,7 @@ def transform(image, hough, show_image): im_h2 = filters.high_pass(im_hough, 96) show_image(im_h2, "second high pass filters") - im_h2 = filters.components2(im_h2) + im_h2 = filters.components(im_h2, 2) show_image(im_h2, "components centers") return im_h2 @@ -70,7 +70,7 @@ def find_lines(image, show_image, logger): im_h3 = filters.high_pass(im_hough2, 120) show_image(im_h3, "third high pass filter") - im_h3 = filters.components(im_h3) + im_h3 = filters.components(im_h3, 1) show_image(im_h3, "half centers") logger("finding the lines")