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 print >> sys.stderr, "hough transform"
50 im_hough = Hough.Transform(im_h)
52 do_something(im_hough.image, "hough transform")
54 # im_hough.image = filters.peaks(im_hough.image)
56 # do_something(im_hough.image, "peak extraction")
58 im_h2 = im_hough.apply_filter(partial(filters.high_pass, height=96))
60 do_something(im_h2.image, "second high pass filters")
62 im_h2 = im_h2.apply_filter(filters.components2)
64 do_something(im_h2.image, "components centers")
67 print >> sys.stderr, "second hough transform"
69 # im_hough might be used instead im_h2, but at the moment it brings a lot of
70 # noise to the second transform, which later confuses the center-finding
71 # mechanism (which is not very robust yet)
72 im_hough2 = Hough.Transform(im_h2.image)
74 do_something(im_hough2.image, "second hough transform")
76 im_h3 = im_hough2.apply_filter(partial(filters.high_pass, height=120))
78 do_something(im_h3.image, "third high pass filter")
80 im_h3 = im_h3.apply_filter(filters.components)
82 do_something(im_h3.image, "half centers")
85 print >> sys.stderr, "finding the grid"
87 lines_m = im_h3.all_lines_h()
89 im_c = im_h2.image.convert('RGB').convert('RGB', (1, 0.5, 0.5, 0))
90 draw_c = ImageDraw.Draw(im_c)
93 for line_l in lines_m:
94 im_line = Image.new('L', im_h2.size)
95 draw = ImageDraw.Draw(im_line)
98 draw.line(line_from_angl_dist(line, im_h2.size), fill=255, width=7)
99 draw_c.line(line_from_angl_dist(line, im_c.size),
100 fill=(70, 70, 70), width=7)
101 for p in combine(im_h2.image, im_line):
103 for point in line_points:
104 draw_c.point(point, fill=(120, 255, 120))
105 lines.append(im_hough.lines_from_list(line_points))
106 line_points = list(line_points)
108 bounds += [line_points[0], line_points[-1]]
111 do_something(im_c, "hough x lines")
113 image_g = image.copy()
114 draw = ImageDraw.Draw(image_g)
115 for line in [l for s in lines for l in s]:
116 draw.line(line_from_angl_dist(line, image.size), fill=(120, 255, 120))
118 do_something(image_g, "lines")
120 return lines, lines_m[0][0], lines_m[1][0], bounds, im_hough
122 def combine(image1, image2):
123 """Return a list of points that are present in both images."""
124 im_l1 = image1.load()
125 im_l2 = image2.load()
129 for x in xrange(image1.size[0]):
130 for y in xrange(image1.size[1]):
131 if im_l1[x, y] and im_l2[x, y]:
132 on_both.append((x, y))
135 def line_from_angl_dist((angle, distance), size):
136 """Take *angle* and *distance* (from the center of the image) of a line and
137 size of the image. Return the line represented by two points."""
138 if pi / 4 < angle < 3 * pi / 4:
140 x1 = int(round((y1 * cos(angle) + distance) / sin(angle))) + size[0] / 2
142 x2 = int(round((y2 * cos(angle) + distance) / sin(angle))) + size[0] / 2
143 return [(x1, 0), (x2, size[1])]
146 y1 = int(round((x1 * sin(angle) - distance) / cos(angle))) + size[1] / 2
148 y2 = int(round((x2 * sin(angle) - distance) / cos(angle))) + size[1] / 2
149 return [(0, y1), (size[0], y2)]