5 analyses the given file
7 shows every step of the computation
17 import Image, ImageDraw
18 except ImportError, msg:
19 print >>sys.stderr, msg
24 from hough import Hough
26 class UsageError(Exception):
27 def __init__(self, msg):
31 """Main function of the program."""
39 raise UsageError('Missing filename')
45 except UsageError, err:
46 print >>sys.stderr, err.msg, "(\"imago.py --help\" for help)"
50 image = Image.open(argv[0])
52 print >>sys.stderr, msg
55 im_debug.show(image, "original image")
57 im_l = image.convert('L')
59 im_debug.show(im_l, "ITU-R 601-2 luma transform")
61 im_edges = filters.edge_detection(im_l)
63 im_debug.show(im_edges, "edge detection")
65 im_h = filters.high_pass(im_edges, 100)
67 im_debug.show(im_h, "high pass filters")
69 hough1 = Hough(im_h.size)
70 im_hough = hough1.transform(im_h)
72 im_debug.show(im_hough, "hough transform")
74 im_h2 = filters.high_pass(im_hough, 120)
76 im_debug.show(im_h2, "second high pass filters")
78 hough2 = Hough(im_h2.size)
79 im_hough2 = hough2.transform(im_h2)
81 im_debug.show(im_hough2, "second hough transform")
83 im_h3 = filters.high_pass(im_hough2, 120)
85 im_debug.show(im_h3, "third high pass filters")
87 lines = hough2.find_angle_distance(im_h3)
89 im_lines = Image.new('L', im_h2.size)
91 draw = ImageDraw.Draw(im_lines)
94 draw.line(line_from_angl_dist(line, im_h2.size), fill=255)
96 im_debug.show(im_lines, "lines")
98 im_c = combine(im_h2, im_lines)
100 im_debug.show(im_c, "first hough x lines")
104 im_debug.show(im_c, "optimalised hough")
106 lines = hough1.all_lines(im_c)
107 draw = ImageDraw.Draw(image)
109 draw.line(line_from_angl_dist(line, image.size), fill=(120, 255, 120))
111 im_debug.show(image, "the grid")
119 for y in xrange(image.size[1]):
120 for x in xrange(image.size[0]):
121 if im_l[x, y] and last:
129 def combine(image1, image2):
130 im_l1 = image1.load()
131 im_l2 = image2.load()
133 im_n = Image.new('L', image1.size)
136 for x in xrange(image1.size[0]):
137 for y in xrange(image1.size[1]):
138 if im_l1[x, y] and im_l2[x, y]:
142 def line_from_angl_dist((angle, distance), size):
144 y1 = int(round((x1 * math.sin(angle) - distance)/math.cos(angle))) + size[1] / 2
146 y2 = int(round((x2 * math.sin(angle) - distance)/math.cos(angle))) + size[1] / 2
147 return [(0, y1), (size[0] - 1, y2)]
149 if __name__ == '__main__':