better manual mode, grid-fitting preparation
[imago.git] / linef.py
1 #!/usr/bin/env python
2
3 """Go image recognition lines-finding module"""
4
5 import sys
6 from math import sin, cos, pi
7
8 try:
9     import Image, ImageDraw
10 except ImportError, msg:
11     print >> sys.stderr, msg
12     sys.exit(1)
13
14 import filters
15 from hough import Hough
16
17 def find_lines(image, show_all, do_something, verbose):
18
19     if verbose:
20         print >> sys.stderr, "preprocessing"
21
22     if show_all:
23         do_something(image, "original image")
24
25     im_l = image.convert('L')
26     if show_all:
27         do_something(im_l, "ITU-R 601-2 luma transform")
28
29     if verbose:
30         print >> sys.stderr, "edge detection"
31
32     im_edges = filters.edge_detection(im_l)
33     if show_all:    
34         do_something(im_edges, "edge detection")
35
36     im_h = filters.high_pass(im_edges, 100)
37     if show_all:
38         do_something(im_h, "high pass filters")
39     
40     if verbose:
41         print >> sys.stderr, "hough transform"
42
43     hough1 = Hough(im_h.size)
44     im_hough = hough1.transform(im_h)
45     if show_all:
46         do_something(im_hough, "hough transform")
47
48     im_hough = filters.peaks(im_hough)
49     if show_all:
50         do_something(im_hough, "peak extraction")
51                
52     im_h2 = filters.high_pass(im_hough, 120)
53     if show_all:
54         do_something(im_h2, "second high pass filters")
55
56     im_h2 = filters.components2(im_h2)
57     if show_all:
58         do_something(im_h2, "components centers")
59
60     if verbose:
61         print >> sys.stderr, "second hough transform"
62
63     hough2 = Hough(im_h2.size) 
64     # im_hough might be used instead im_h2, but at the moment it brings a lot of
65     # noise to the second transform, which later confuses the center-finding
66     # mechanism (which is not very robust yet)
67     im_hough2 = hough2.transform(im_h2)
68     if show_all:
69         do_something(im_hough2, "second hough transform")
70
71     im_h3 = filters.high_pass(im_hough2, 120)
72     if show_all:
73         do_something(im_h3, "third high pass filter")
74      
75     im_h3 = filters.components(im_h3)
76     if show_all:
77         do_something(im_h3, "half centers")
78
79     if verbose:
80         print >> sys.stderr, "finding the grid"
81
82     lines_m = hough2.all_lines_h(im_h3)
83     lines = []
84     im_c = im_h2.convert('RGB').convert('RGB', (1, 0.5, 0.5, 0))
85     draw_c = ImageDraw.Draw(im_c)
86
87     for line_l in lines_m:
88         im_line = Image.new('L', im_h2.size)
89         draw = ImageDraw.Draw(im_line)
90         line_points = set()
91         for line in line_l:
92             draw.line(line_from_angl_dist(line, im_h2.size), fill=255, width=7)
93             draw_c.line(line_from_angl_dist(line, im_c.size), fill=(70, 70, 70), width=7)
94             for p in combine(im_h2, im_line):
95                 line_points.add(p)
96         for point in line_points:
97             draw_c.point(point, fill=(120, 255, 120))
98         lines.append(hough1.lines_from_list(line_points))
99
100     if show_all:
101         do_something(im_c, "hough x lines")
102
103     image_g = image.copy()
104     draw = ImageDraw.Draw(image_g)
105     for line in [l for s in lines for l in s]:
106         draw.line(line_from_angl_dist(line, image.size), fill=(120, 255, 120))
107     if show_all:
108         do_something(image_g, "lines")
109    
110     return lines
111
112 def combine(image1, image2):
113     im_l1 = image1.load()
114     im_l2 = image2.load()
115
116     on_both = []
117
118     for x in xrange(image1.size[0]):
119         for y in xrange(image1.size[1]):
120             if im_l1[x, y] and im_l2[x, y]:
121                 on_both.append((x, y))
122     return on_both
123
124 def line_from_angl_dist((angle, distance), size):
125     if pi / 4 < angle < 3 * pi / 4:
126         y1 = - size[1] / 2
127         x1 = int(round((y1 * cos(angle) + distance) / sin(angle))) + size[0] / 2
128         y2 = size[1] / 2 
129         x2 = int(round((y2 * cos(angle) + distance) / sin(angle))) + size[0] / 2
130         return [(x1, 0), (x2, size[1])]
131     else:
132         x1 = - size[0] / 2
133         y1 = int(round((x1 * sin(angle) - distance) / cos(angle))) + size[1] / 2
134         x2 = size[0] / 2 
135         y2 = int(round((x2 * sin(angle) - distance) / cos(angle))) + size[1] / 2
136         return [(0, y1), (size[0], y2)]