fix split lines in Hough space
[imago.git] / src / linef.py
1 """Lines finding module."""
2
3 from functools import partial
4 import sys
5 from math import sin, cos, pi
6
7 try:
8     from PIL import Image, ImageDraw
9 except ImportError, msg:
10     print >> sys.stderr, msg
11     sys.exit(1)
12
13 import filters
14 from hough import Hough
15 import ransac
16
17 def prepare(image, show_image, logger):
18     # TODO comment
19     im_l = image.convert('L')
20     show_image(im_l, "ITU-R 601-2 luma transform")
21
22     logger("edge detection")
23     im_edges = filters.edge_detection(im_l)
24     show_image(im_edges, "edge detection")
25
26     im_h = filters.high_pass(im_edges, 100)
27     show_image(im_h, "high pass filters")
28
29     return im_h
30  
31 def transform(image, hough, show_image):
32     # TODO comment
33     im_hough = hough.transform(image)
34     show_image(im_hough, "hough transform")
35
36     # im_hough = filters.peaks(im_hough)
37     # show_image(im_hough, "peak extraction")
38                
39     im_h2 = filters.high_pass(im_hough, 128)
40     show_image(im_h2, "second high pass filters")
41
42     im_h2 = filters.components(im_h2, 2)
43     show_image(im_h2, "components centers")
44
45     return im_h2
46
47 def run_ransac(image):
48     # TODO comment
49     # TODO vizualize this
50     image_l = image.load()
51     width, height = image.size
52
53     data = []
54
55     for y in xrange(0, height):
56         for x in xrange(0, width):
57             if image_l[x, y] > 128:
58                 data.append((x, y))
59                 if y < 30:
60                     data.append((width - x, y + height))
61
62     dist = 3 
63     [(line, points), (line2, points2)] = ransac.ransac_multi(2, data, dist, 250)
64     line_to_points = lambda (a, b, c), x: (x, (a*x + c) / (- b))
65     # TODO width should not be here vvv
66     # TODO refactor gridf to use standard equations instead of points
67     line = [line_to_points(line, 0), line_to_points(line, width - 1)]
68     line2 = [line_to_points(line2, 0), line_to_points(line2, width - 1)]
69     return [sorted(points), sorted(points2)], line, line2
70
71 def find_lines(image, show_image, logger):
72     """Find lines in the *image*."""
73     
74     logger("preprocessing")
75     show_image(image, "original image")
76
77     im_h = prepare(image, show_image, logger)
78
79     hough = Hough.default(im_h)
80
81     logger("hough transform")
82     
83     im_h2 = transform(im_h, hough, show_image)
84
85     logger("finding the lines")
86
87     r_lines, l1, l2 = run_ransac(im_h2) 
88
89     lines = map(hough.lines_from_list, r_lines)
90
91     # TODO refactor gridf to get rid of this:
92     bounds = sum(map(lambda l: [l[0], l[-1]], r_lines), []) 
93     # sum(list, []) = flatten list
94
95     # TODO do this only if show_all is true:
96     image_g = image.copy()
97     draw = ImageDraw.Draw(image_g)
98     for line in [l for s in lines for l in s]:
99         draw.line(line_from_angl_dist(line, image.size), fill=(120, 255, 120))
100     show_image(image_g, "lines")
101
102
103     return lines, l1, l2, bounds, hough # TODO
104
105 def line_from_angl_dist((angle, distance), size):
106     """Take *angle* and *distance* (from the center of the image) of a line and
107     size of the image. Return the line represented by two points."""
108     if pi / 4 < angle < 3 * pi / 4:
109         y1 = - size[1] / 2
110         x1 = int(round((y1 * cos(angle) + distance) / sin(angle))) + size[0] / 2
111         y2 = size[1] / 2 
112         x2 = int(round((y2 * cos(angle) + distance) / sin(angle))) + size[0] / 2
113         return [(x1, 0), (x2, size[1])]
114     else:
115         x1 = - size[0] / 2
116         y1 = int(round((x1 * sin(angle) - distance) / cos(angle))) + size[1] / 2
117         x2 = size[0] / 2 
118         y2 = int(round((x2 * sin(angle) - distance) / cos(angle))) + size[1] / 2
119         return [(0, y1), (size[0], y2)]