3 """Usage: imago.py file
4 analyses the given file
6 shows every step of the computation
13 import Image, ImageDraw
14 except ImportError, msg:
15 print >>sys.stderr, msg
19 from hough import Hough
21 class UsageError(Exception):
22 def __init__(self, msg):
26 """Main function of the program."""
34 raise UsageError('Missing filename')
40 except UsageError, err:
41 print >>sys.stderr, err.msg, "(\"imago.py --help\" for help)"
45 image = Image.open(argv[0])
47 print >>sys.stderr, msg
50 im_debug.show(image, "original image")
52 im_l = image.convert('L')
54 im_debug.show(im_l, "ITU-R 601-2 luma transform")
56 im_edges = filter.edge_detection(im_l)
58 im_debug.show(im_edges, "edge detection")
60 im_h = filter.high_pass(im_edges, 100)
62 im_debug.show(im_h, "high pass filter")
64 hough1 = Hough(im_h.size)
65 im_hough = hough1.transform(im_h)
67 im_debug.show(im_hough, "hough transform")
69 im_h2 = filter.high_pass(im_hough, 120)
71 im_debug.show(im_h2, "second high pass filter")
73 hough2 = Hough(im_h2.size)
74 im_hough2 = hough2.transform(im_h2)
76 im_debug.show(im_hough2, "second hough transform")
78 im_h3 = filter.high_pass(im_hough2, 120)
80 im_debug.show(im_h3, "third high pass filter")
82 lines = hough2.find_angle_distance(im_h3)
84 im_lines = Image.new('L', im_h2.size)
86 draw = ImageDraw.Draw(im_lines)
89 draw.line(line_from_angl_dist(line, im_h2.size), fill=255)
91 im_debug.show(im_lines, "lines")
93 im_c = combine(im_h2, im_lines)
95 im_debug.show(im_c, "first hough x lines")
99 im_debug.show(im_c, "optimalised hough")
101 lines = hough1.all_lines(im_c)
102 draw = ImageDraw.Draw(image)
104 draw.line(line_from_angl_dist(line, image.size), fill=(120, 255, 120))
106 im_debug.show(image, "the grid")
114 for y in xrange(image.size[1]):
115 for x in xrange(image.size[0]):
116 if im_l[x,y] and last:
124 def combine(image1, image2):
125 im_l1 = image1.load()
126 im_l2 = image2.load()
128 im_n = Image.new('L', image1.size)
131 for x in xrange(image1.size[0]):
132 for y in xrange(image1.size[1]):
133 if im_l1[x, y] and im_l2[x, y]:
137 def line_from_angl_dist((angle, distance), size):
139 y1 = int(round((x1 * math.sin(angle) - distance)/math.cos(angle))) + size[1] / 2
141 y2 = int(round((x2 * math.sin(angle) - distance)/math.cos(angle))) + size[1] / 2
142 return [(0, y1), (size[0] - 1, y2)]
144 if __name__ == '__main__':