1 """Lines finding module."""
3 from functools import partial
5 from math import sin, cos, pi
8 import Image, ImageDraw
9 except ImportError, msg:
10 print >> sys.stderr, msg
14 from hough import Hough
16 def prepare(image, show_all, do_something, verbose):
18 im_l = image.convert('L')
20 do_something(im_l, "ITU-R 601-2 luma transform")
23 print >> sys.stderr, "edge detection"
24 im_edges = filters.edge_detection(im_l)
26 do_something(im_edges, "edge detection")
28 im_h = filters.high_pass(im_edges, 100)
30 do_something(im_h, "high pass filters")
35 def find_lines(image, show_all, do_something, verbose):
36 """Find lines in the *image*."""
37 # TODO refactor into smaller functions
40 print >> sys.stderr, "preprocessing"
43 do_something(image, "original image")
45 im_h = prepare(image, show_all, do_something, verbose)
48 hough = Hough.default(im_h)
51 print >> sys.stderr, "hough transform"
53 im_hough = hough.transform(im_h)
55 do_something(im_hough, "hough transform")
57 # im_hough.image = filters.peaks(im_hough.image)
59 # do_something(im_hough.image, "peak extraction")
61 im_h2 = filters.high_pass(im_hough, 96)
63 do_something(im_h2, "second high pass filters")
65 im_h2 = filters.components2(im_h2)
67 do_something(im_h2, "components centers")
70 print >> sys.stderr, "second hough transform"
72 # im_hough might be used instead im_h2, but at the moment it brings a lot of
73 # noise to the second transform, which later confuses the center-finding
74 # mechanism (which is not very robust yet)
75 hough2 = Hough.default(im_h2)
76 im_hough2 = hough2.transform(im_h2)
78 do_something(im_hough2, "second hough transform")
80 im_h3 = filters.high_pass(im_hough2, 120)
82 do_something(im_h3, "third high pass filter")
84 im_h3 = filters.components(im_h3)
86 do_something(im_h3, "half centers")
89 print >> sys.stderr, "finding the grid"
91 lines_m = hough2.all_lines_h(im_h3)
93 im_c = im_h2.convert('RGB').convert('RGB', (1, 0.5, 0.5, 0))
94 draw_c = ImageDraw.Draw(im_c)
97 for line_l in lines_m:
98 im_line = Image.new('L', im_h2.size)
99 draw = ImageDraw.Draw(im_line)
102 draw.line(line_from_angl_dist(line, im_h2.size), fill=255, width=7)
103 draw_c.line(line_from_angl_dist(line, im_c.size),
104 fill=(70, 70, 70), width=7)
105 for p in combine(im_h2, im_line):
107 for point in line_points:
108 draw_c.point(point, fill=(120, 255, 120))
109 lines.append(hough.lines_from_list(line_points))
110 line_points = list(line_points)
112 bounds += [line_points[0], line_points[-1]]
115 do_something(im_c, "hough x lines")
117 image_g = image.copy()
118 draw = ImageDraw.Draw(image_g)
119 for line in [l for s in lines for l in s]:
120 draw.line(line_from_angl_dist(line, image.size), fill=(120, 255, 120))
122 do_something(image_g, "lines")
124 return lines, lines_m[0][0], lines_m[1][0], bounds, hough
126 def combine(image1, image2):
127 """Return a list of points that are present in both images."""
128 im_l1 = image1.load()
129 im_l2 = image2.load()
133 for x in xrange(image1.size[0]):
134 for y in xrange(image1.size[1]):
135 if im_l1[x, y] and im_l2[x, y]:
136 on_both.append((x, y))
139 def line_from_angl_dist((angle, distance), size):
140 """Take *angle* and *distance* (from the center of the image) of a line and
141 size of the image. Return the line represented by two points."""
142 if pi / 4 < angle < 3 * pi / 4:
144 x1 = int(round((y1 * cos(angle) + distance) / sin(angle))) + size[0] / 2
146 x2 = int(round((y2 * cos(angle) + distance) / sin(angle))) + size[0] / 2
147 return [(x1, 0), (x2, size[1])]
150 y1 = int(round((x1 * sin(angle) - distance) / cos(angle))) + size[1] / 2
152 y2 = int(round((x2 * sin(angle) - distance) / cos(angle))) + size[1] / 2
153 return [(0, y1), (size[0], y2)]