From: Tomas Musil Date: Tue, 9 Oct 2012 13:58:14 +0000 (+0200) Subject: manual mode works X-Git-Url: http://git.tomasm.cz/imago.git/commitdiff_plain/e3b0b7b8f5accc4f6af935923e6db9b34a40b362?hp=b401645e5761e876866edcab680d54f1310bb0bd manual mode works --- diff --git a/imago.py b/imago.py index 830a53b..81aa1ac 100755 --- a/imago.py +++ b/imago.py @@ -64,6 +64,13 @@ def main(): else: lines = linef.find_lines(image, show_all, do_something, verbose) + image_g = image.copy() + draw = ImageDraw.Draw(image_g) + for line in [l for s in lines for l in s]: + draw.line(linef.line_from_angl_dist(line, image.size), fill=(120, 255, 120)) + if show_all: + do_something(image_g, "the grid") + intersections = intersections_from_angl_dist(lines, image.size) image_g = image.copy() draw = ImageDraw.Draw(image_g) diff --git a/linef.py b/linef.py index f46d92c..1dd35d4 100755 --- a/linef.py +++ b/linef.py @@ -100,13 +100,7 @@ def find_lines(image, show_all, do_something, verbose): if show_all: do_something(im_c, "hough x lines") - image_g = image.copy() - draw = ImageDraw.Draw(image_g) - for line in [l for s in lines for l in s]: - draw.line(line_from_angl_dist(line, image.size), fill=(120, 255, 120)) - if show_all: - do_something(image_g, "the grid") - + return lines def combine(image1, image2): diff --git a/manual.py b/manual.py index 658f601..f774c7b 100644 --- a/manual.py +++ b/manual.py @@ -2,6 +2,7 @@ import pygame import Image, ImageDraw +from math import atan, sin, cos, pi class UserQuitError(Exception): pass @@ -32,18 +33,23 @@ def find_lines(im_orig): corners = [] color=(64, 64, 255) line_width = 2 + lines_r = [] + while not done: for event in pygame.event.get(): - if event.type == pygame.QUIT: + if event.type == pygame.QUIT or event.type == pygame.KEYDOWN: pygame.quit() - raise UserQuitError + if len(corners) == 4: + return lines_r + else: + raise UserQuitError if event.type == pygame.MOUSEBUTTONDOWN: - if len(corners) >= 4: + if len(corners) >= 4: corners = [] im = im_orig.copy() draw = ImageDraw.Draw(im) - if len(corners) < 4: + if len(corners) < 4: corners.append(pygame.mouse.get_pos()) draw.point(corners[:-1], fill=color) if len(corners) == 4: @@ -61,12 +67,18 @@ def find_lines(im_orig): l_hor = lines(corners[1:4] + [corners[0]], 0) for l in l_hor: draw.line(l, fill=color, width=line_width) + l_vert += [(corners[0], corners[3]), + (corners[1], corners[2])] + l_hor += [(corners[0], corners[1]), + (corners[2], corners[3])] l_vert = sorted(l_vert) l_hor = sorted(l_hor) for i in [3, 8, 14]: for j in [3, 8, 14]: hoshi(intersection(line(l_vert[i][0], l_vert[i][1]), line(l_hor[j][0], l_hor[j][1]))) + lines_r = [[l2ad(l[0], l[1], im.size) for l in l_vert], + [l2ad(l[0], l[1], im.size) for l in l_hor]] screen.display_picture(im) clock.tick(15) @@ -119,3 +131,14 @@ def intersection(p, q): return None return (int(round(float(q[1] * p[2] - p[1] * q[2]) / det)), int(round(float(p[0] * q[2] - q[0] * p[2]) / det))) + +def l2ad(a, b, size): + if (a[0] - b[0]) == 0: + angle = pi / 2 + else: + q = float(a[1] - b[1]) / (a[0] - b[0]) + angle = atan(q) + + distance = (((a[0] - (size[0] / 2)) * sin(angle)) + + ((a[1] - (size[1] / 2)) * - cos(angle))) + return (angle, distance)