2 """Usage: imago.py file"""
6 import Image, ImageDraw
9 from hough import Hough
11 class UsageError(Exception):
12 def __init__(self, msg):
16 """Main function of the program."""
24 raise UsageError('Missing filename')
30 except UsageError, err:
31 print >>sys.stderr, err.msg, "(\"imago.py --help\" for help)"
35 image = Image.open(argv[0])
37 print >>sys.stderr, msg
40 im_debug.show(image, "original image")
42 im_l = image.convert('L')
44 im_debug.show(im_l, "ITU-R 601-2 luma transform")
46 im_edges = filter.edge_detection(im_l)
48 im_debug.show(im_edges, "edge detection")
50 im_h = filter.high_pass(im_edges, 100)
52 im_debug.show(im_h, "high pass filter")
54 hough1 = Hough(im_h.size)
55 im_hough = hough1.transform(im_h)
57 im_debug.show(im_hough, "hough transform")
59 im_h2 = filter.high_pass(im_hough, 120)
61 im_debug.show(im_h2, "second high pass filter")
63 hough2 = Hough(im_h2.size)
64 im_hough2 = hough2.transform(im_h2)
66 im_debug.show(im_hough2, "second hough transform")
68 im_h3 = filter.high_pass(im_hough2, 120)
70 im_debug.show(im_h3, "third high pass filter")
72 lines = hough2.find_angle_distance(im_h3)
74 im_lines = Image.new('L', im_h2.size)
76 draw = ImageDraw.Draw(im_lines)
79 draw.line(line_from_angl_dist(line, im_h2.size), fill=255)
81 im_debug.show(im_lines, "lines")
83 im_c = combine(im_h2, im_lines)
85 im_debug.show(im_c, "first hough x lines")
89 im_debug.show(im_c, "optimalised hough")
91 lines = hough1.all_lines(im_c)
92 draw = ImageDraw.Draw(image)
94 draw.line(line_from_angl_dist(line, image.size), fill=(120, 255, 120))
96 im_debug.show(image, "the grid")
104 for y in xrange(image.size[1]):
105 for x in xrange(image.size[0]):
106 if im_l[x,y] and last:
114 def combine(image1, image2):
115 im_l1 = image1.load()
116 im_l2 = image2.load()
118 im_n = Image.new('L', image1.size)
121 for x in xrange(image1.size[0]):
122 for y in xrange(image1.size[1]):
123 if im_l1[x, y] and im_l2[x, y]:
127 def line_from_angl_dist((angle, distance), size):
129 y1 = int(round((x1 * math.sin(angle) - distance)/math.cos(angle))) + size[1] / 2
131 y2 = int(round((x2 * math.sin(angle) - distance)/math.cos(angle))) + size[1] / 2
132 return [(0, y1), (size[0] - 1, y2)]
134 if __name__ == '__main__':