Пример #1
0
def run(img):
    img = extract_paper(img)

    graph = sketch_graph(img)

    components = nx.connected_components(graph)

    circles = [
        topology.fit_circle_to_points(list(component))
        for component in components
    ]

    log.hsvOrGreyImage(img, circles=circles)

    svg = printer.circles_to_svg(circles)
    printer.write_file("log/out.svg", svg)
    pdf = printer.svg_to_pdf("log/out.svg", "log/out.pdf")
    printer.write_file("log/out.pdf", pdf)
    # printer.print_pdf("log/out.pdf")

    return graph
Пример #2
0
def get_tangent_direction(path, index, direction, neighborhood):
    """The tangent as normalized vector at the point at path[index].

    This is determined by considering the points leading either into the point
    (if direction is -1) or out of the point (if direction is 1).

    We look at the points that are within the neighborhood of the point in
    question. We fit a circle to these points and then get the tangent of the
    circle at the point.
    """
    origin = path[index]
    points = get_neighborhood(path, index, direction, neighborhood)

    if is_colinear(points):
        # If they're colinear, then getting the tangent is easy. We just pick
        # two points in the path to get the direction.
        return normalize(sub(origin, points[1]))

    (center, radius) = topology.fit_circle_to_points(points)
    perpendicular = normalize(sub(center, origin))
    (px, py) = perpendicular
    # There are two options for tangent direction: a or b.
    a = (-py, px)
    b = (py, -px)
    pa = add(origin, a)
    pb = add(origin, b)
    # We choose the one that is furthest from points.
    a_total = 0
    b_total = 0
    for point in points:
        a_total += quadrance(pa, point)
        b_total += quadrance(pb, point)
    if a_total > b_total:
        return a
    else:
        return b
Пример #3
0
def get_tangent_direction(path, index, direction, neighborhood):
    """The tangent as normalized vector at the point at path[index].

    This is determined by considering the points leading either into the point
    (if direction is -1) or out of the point (if direction is 1).

    We look at the points that are within the neighborhood of the point in
    question. We fit a circle to these points and then get the tangent of the
    circle at the point.
    """
    origin = path[index]
    points = get_neighborhood(path, index, direction, neighborhood)

    if is_colinear(points):
        # If they're colinear, then getting the tangent is easy. We just pick
        # two points in the path to get the direction.
        return normalize(sub(origin, points[1]))

    (center, radius) = topology.fit_circle_to_points(points)
    perpendicular = normalize(sub(center, origin))
    (px, py) = perpendicular
    # There are two options for tangent direction: a or b.
    a = (-py, px)
    b = (py, -px)
    pa = add(origin, a)
    pb = add(origin, b)
    # We choose the one that is furthest from points.
    a_total = 0
    b_total = 0
    for point in points:
        a_total += quadrance(pa, point)
        b_total += quadrance(pb, point)
    if a_total > b_total:
        return a
    else:
        return b