X-Git-Url: http://git.tomasm.cz/imago.git/blobdiff_plain/6171054cca1b1120afad2d70a50b6efc0441a831..e6f073518dbb7ad540d5876f6326b326cdc6da13:/src/gridf3.py?ds=inline diff --git a/src/gridf3.py b/src/gridf3.py index 0697b92..8a1a216 100644 --- a/src/gridf3.py +++ b/src/gridf3.py @@ -12,6 +12,9 @@ from geometry import l2ad class GridFittingFailedError(Exception): pass +class BadGenError(Exception): + pass + def plot_line(line, c, size): points = linef.line_from_angl_dist(line, size) pyplot.plot(*zip(*points), color=c) @@ -32,12 +35,16 @@ class Diagonal_model: if l1[i] and l2[j]: yield (l1[i], l2[j]) + def remove(self, data): + self.data = list(set(self.data) - set(data)) + def initial(self): try: - return self.gen.next() + nxt = self.gen.next() except StopIteration: self.gen = self.initial_g() - return self.gen.next() + nxt = self.gen.next() + return nxt def get(self, sample): if len(sample) == 2: @@ -50,15 +57,25 @@ class Diagonal_model: score = 0 a, b, c = est dst = lambda (x, y): abs(a * x + b * y + c) / sqrt(a*a+b*b) + l1 = None + l2 = None for p in self.data: d = dst(p) if d <= dist: cons.append(p) - score += min(d, dist) + if p.l1 == l1 or p.l2 == l2: + return float("inf"), [] + else: + l1, l2 = p.l1, p.l2 + else: # TODO delete this or refactor + score += min(d, dist) + return score, cons def intersection((a1, b1, c1), (a2, b2, c2)): delim = float(a1 * b2 - b1 * a2) + if delim == 0: + return None x = (b1 * c2 - c1 * b2) / delim y = (c1 * a2 - a1 * c2) / delim return x, y @@ -125,7 +142,7 @@ def gen_corners(d1, d2): # TODO create an intersection? try: yield manual.lines(map(lambda p: p.to_tuple(), [c2, c1, c3, c4])) - except (TypeError, ZeroDivisionError): + except (TypeError): pass # the square was too small to fit 17 lines inside # TODO define SquareTooSmallError or something @@ -157,16 +174,24 @@ def find(lines, size, l1, l2, bounds, hough, show_all, do_something, logger): points = [l.points for l in new_lines1] - for trial in xrange(3): - line1, cons = ransac.estimate(points, 2, 800, Diagonal_model) - points2 = map(lambda l: [(p if not p in cons else None) for p in l], points) - line2, cons2 = ransac.estimate(points2, 2, 800, Diagonal_model) - center = intersection(line1, line2) - data = sum(points, []) - diag1 = Line(line1) - diag1.points = ransac.filter_near(data, diag1, 2) - diag2 = Line(line2) - diag2.points = ransac.filter_near(data, diag2, 2) + def dst_p(x, y): + x = x - size[0] / 2 + y = y - size[1] / 2 + return sqrt(x * x + y * y) + + for n_tries in xrange(3): + model = Diagonal_model(points) + diag_lines = ransac.ransac_multi(6, points, 2, 800, model=model) + diag_lines = [l[0] for l in diag_lines] + centers = [] + cen_lin = [] + for i in xrange(len(diag_lines)): + line1 = diag_lines[i] + for line2 in diag_lines[i+1:]: + c = intersection(line1, line2) + if c and dst_p(*c) < min(size) / 2: + cen_lin.append((line1, line2, c)) + centers.append(c) if show_all: import matplotlib.pyplot as pyplot @@ -177,10 +202,11 @@ def find(lines, size, l1, l2, bounds, hough, show_all, do_something, logger): pyplot.plot([0, max_x], [find_y(0), find_y(max_x)], color='b') fig = pyplot.figure(figsize=(8, 6)) - plot_line_g(diag1, size[0]) - plot_line_g(diag2, size[0]) + for l in diag_lines: + plot_line_g(l, size[0]) pyplot.scatter(*zip(*sum(points, []))) - pyplot.scatter([center[0]], [center[1]], color='r') + if len(centers) >= 1: + pyplot.scatter([c[0] for c in centers], [c[1] for c in centers], color='r') pyplot.xlim(0, size[0]) pyplot.ylim(0, size[1]) pyplot.gca().invert_yaxis() @@ -188,16 +214,29 @@ def find(lines, size, l1, l2, bounds, hough, show_all, do_something, logger): size_f = fig.canvas.get_width_height() buff = fig.canvas.tostring_rgb() image_p = Image.fromstring('RGB', size_f, buff, 'raw') - do_something(image_p, "finding diagonal") + do_something(image_p, "finding diagonals") - - grids = list(gen_corners(diag1, diag2)) - - try: - sc, grid = min(map(lambda g: (score(sum(g, []), data), g), grids)) + data = sum(points, []) + # TODO what if lines are missing? + sc = float("inf") + grid = None + for (line1, line2, c) in cen_lin: + diag1 = Line(line1) + diag1.points = ransac.filter_near(data, diag1, 2) + diag2 = Line(line2) + diag2.points = ransac.filter_near(data, diag2, 2) + + + grids = list(gen_corners(diag1, diag2)) + + try: + new_sc, new_grid = min(map(lambda g: (score(sum(g, []), data), g), grids)) + if new_sc < sc: + sc, grid = new_sc, new_grid + except ValueError: + pass + if grid: break - except ValueError: - pass else: raise GridFittingFailedError