示例#1
0
文件: linef.py 项目: mkghub/imago
def run_ransac(image):
    # TODO comment
    # TODO vizualize this
    image_l = image.load()
    width, height = image.size

    data = []

    for y in xrange(0, height):
        for x in xrange(0, width):
            if image_l[x, y] > 128:
                data.append((x, y))
                if y < 30:
                    data.append((width - x, y + height))

    dist = 3 
    [(line, points), (line2, points2)] = ransac.ransac_multi(2, data, dist, 250)
    line_to_points = lambda (a, b, c), x: (x, (a*x + c) / (- b))
    # TODO width should not be here vvv
    # TODO refactor gridf to use standard equations instead of points
    line = [line_to_points(line, 0), line_to_points(line, width - 1)]
    line2 = [line_to_points(line2, 0), line_to_points(line2, width - 1)]
    return [sorted(points), sorted(points2)], line, line2
示例#2
0
def run_ransac(image):
    # TODO comment
    # TODO vizualize this
    image_l = image.load()
    width, height = image.size

    data = []

    for y in xrange(0, height):
        for x in xrange(0, width):
            if image_l[x, y] > 128:
                data.append((x, y))
                if y < 30:
                    data.append((width - x, y + height))

    dist = 3 
    [(line, points), (line2, points2)] = ransac.ransac_multi(2, data, dist, 250)
    line_to_points = lambda (a, b, c), x: (x, (a*x + c) / (- b))
    # TODO width should not be here vvv
    # TODO refactor gridf to use standard equations instead of points
    line = [line_to_points(line, 0), line_to_points(line, width - 1)]
    line2 = [line_to_points(line2, 0), line_to_points(line2, width - 1)]
    return [sorted(points), sorted(points2)], line, line2
示例#3
0
def find(lines, size, l1, l2, bounds, hough, show_all, do_something, logger):
    """Find the best grid given the *lines* and *size* of the image.
    
    Last three parameters serves for debugging, *l1*, *l2*, *bounds* and *hough*
    are here for compatibility with older version of gridf, so they can be
    easily exchanged, tested and compared.
    """

    new_lines1 = map(lambda l: Line.from_ad(l, size), lines[0])
    new_lines2 = map(lambda l: Line.from_ad(l, size), lines[1])
    for l1 in new_lines1:
        for l2 in new_lines2:
            p = Point(intersection(l1, l2))
            p.l1 = l1
            p.l2 = l2
            l1.points.append(p)
            l2.points.append(p)

    points = [l.points for l in new_lines1]

    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):
        logger("finding the diagonals")
        model = Diagonal_model(points)
        diag_lines = ransac.ransac_multi(6, points, 2,
                                         params.ransac_diagonal_iter, 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
            from PIL import Image

            def plot_line_g((a, b, c), max_x):
                find_y = lambda x: - (c + a * x) / b
                pyplot.plot([0, max_x], [find_y(0), find_y(max_x)], color='b')

            fig = pyplot.figure(figsize=(8, 6))
            for l in diag_lines:
                plot_line_g(l, size[0])
            pyplot.scatter(*zip(*sum(points, [])))
            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()
            fig.canvas.draw()
            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 diagonals")

        logger("finding the grid")
        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, min(size) / 3))
            
            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