3 """Go image recognition lines-finding module"""
6 from math import sin, cos, pi
9 import Image, ImageDraw
10 except ImportError, msg:
11 print >> sys.stderr, msg
15 from hough import Hough
17 def find_lines(image, show_all, do_something, verbose):
20 print >> sys.stderr, "preprocessing"
23 do_something(image, "original image")
25 im_l = image.convert('L')
27 do_something(im_l, "ITU-R 601-2 luma transform")
30 print >> sys.stderr, "edge detection"
32 im_edges = filters.edge_detection(im_l)
34 do_something(im_edges, "edge detection")
36 im_h = filters.high_pass(im_edges, 100)
38 do_something(im_h, "high pass filters")
41 print >> sys.stderr, "hough transform"
43 hough1 = Hough(im_h.size)
44 im_hough = hough1.transform(im_h)
46 do_something(im_hough, "hough transform")
48 im_hough = filters.peaks(im_hough)
50 do_something(im_hough, "peak extraction")
52 im_h2 = filters.high_pass(im_hough, 120)
54 do_something(im_h2, "second high pass filters")
56 im_h2 = filters.components2(im_h2)
58 do_something(im_h2, "components centers")
61 print >> sys.stderr, "second hough transform"
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)
69 do_something(im_hough2, "second hough transform")
71 im_h3 = filters.high_pass(im_hough2, 120)
73 do_something(im_h3, "third high pass filter")
75 im_h3 = filters.components(im_h3)
77 do_something(im_h3, "half centers")
80 print >> sys.stderr, "finding the grid"
82 lines_m = hough2.all_lines_h(im_h3)
84 im_c = im_h2.convert('RGB').convert('RGB', (1, 0.5, 0.5, 0))
85 draw_c = ImageDraw.Draw(im_c)
87 for line_l in lines_m:
88 im_line = Image.new('L', im_h2.size)
89 draw = ImageDraw.Draw(im_line)
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):
96 for point in line_points:
97 draw_c.point(point, fill=(120, 255, 120))
98 lines.append(hough1.lines_from_list(line_points))
101 do_something(im_c, "hough x lines")
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))
108 do_something(image_g, "the grid")
112 def combine(image1, image2):
113 im_l1 = image1.load()
114 im_l2 = image2.load()
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))
124 def line_from_angl_dist((angle, distance), size):
125 if pi / 4 < angle < 3 * pi / 4:
127 x1 = int(round((y1 * cos(angle) + distance) / sin(angle))) + size[0] / 2
129 x2 = int(round((y2 * cos(angle) + distance) / sin(angle))) + size[0] / 2
130 return [(x1, 0), (x2, size[1])]
133 y1 = int(round((x1 * sin(angle) - distance) / cos(angle))) + size[1] / 2
135 y2 = int(round((x2 * sin(angle) - distance) / cos(angle))) + size[1] / 2
136 return [(0, y1), (size[0], y2)]