d234fec6f3a4afc6ad543acac016b1aa390de6eb
[imago.git] / imago.py
1 #!/usr/bin/env python
2
3 """Usage:  imago.py file
4             analyses the given file
5         imago.py file --debug
6             shows every step of the computation
7         imago.py --help
8             shows this help"""
9
10 import sys
11 import math
12 try:
13     import Image, ImageDraw
14 except ImportError, msg:
15     print >>sys.stderr, msg
16     sys.exit(1)
17 import im_debug
18 import filter
19 from hough import Hough
20
21 class UsageError(Exception):
22     def __init__(self, msg):
23         self.msg = msg
24
25 def main(*argv):
26     """Main function of the program."""
27     
28     show_all = False
29
30     try:
31         if argv is ():
32             argv = sys.argv[1:]
33             if argv == []:
34                 raise UsageError('Missing filename')
35         if "--help" in argv:
36             print __doc__
37             return 0
38         if "--debug" in argv:
39             show_all = True
40     except UsageError, err:
41         print >>sys.stderr, err.msg, "(\"imago.py --help\" for help)"
42         return 2
43
44     try:
45         image = Image.open(argv[0])
46     except IOError, msg:
47         print >>sys.stderr, msg
48         return 1
49     if show_all:
50        im_debug.show(image, "original image")
51
52     im_l = image.convert('L')
53     if show_all:
54         im_debug.show(im_l, "ITU-R 601-2 luma transform")
55
56     im_edges = filter.edge_detection(im_l)
57     if show_all:    
58         im_debug.show(im_edges, "edge detection")
59
60     im_h = filter.high_pass(im_edges, 100)
61     if show_all:
62         im_debug.show(im_h, "high pass filter")
63     
64     hough1 = Hough(im_h.size)
65     im_hough = hough1.transform(im_h)
66     if show_all:
67         im_debug.show(im_hough, "hough transform")
68
69     im_h2 = filter.high_pass(im_hough, 120)
70     if show_all:
71         im_debug.show(im_h2, "second high pass filter")
72
73     hough2 = Hough(im_h2.size)
74     im_hough2 = hough2.transform(im_h2)
75     if show_all:
76         im_debug.show(im_hough2, "second hough transform")
77
78     im_h3 = filter.high_pass(im_hough2, 120)
79     if show_all:
80         im_debug.show(im_h3, "third high pass filter")
81      
82     lines = hough2.find_angle_distance(im_h3)
83
84     im_lines = Image.new('L', im_h2.size)
85
86     draw = ImageDraw.Draw(im_lines)
87
88     for line in lines:
89         draw.line(line_from_angl_dist(line, im_h2.size), fill=255)
90     if show_all:
91         im_debug.show(im_lines, "lines")
92
93     im_c = combine(im_h2, im_lines)
94     if show_all:
95         im_debug.show(im_c, "first hough x lines")
96
97     collapse(im_c)
98     if show_all:
99         im_debug.show(im_c, "optimalised hough")
100
101     lines = hough1.all_lines(im_c)
102     draw = ImageDraw.Draw(image)
103     for line in lines:
104         draw.line(line_from_angl_dist(line, image.size), fill=(120, 255, 120))
105
106     im_debug.show(image, "the grid")
107
108     return 0
109
110 def collapse(image):
111     #HACK
112     im_l = image.load()
113     last = False
114     for y in xrange(image.size[1]):
115         for x in xrange(image.size[0]):
116             if im_l[x,y] and last:
117                 im_l[x, y] = 0
118                 last = False
119             elif im_l[x, y]:
120                 last = True
121             elif last:
122                 last = False
123
124 def combine(image1, image2):
125     im_l1 = image1.load()
126     im_l2 = image2.load()
127
128     im_n = Image.new('L', image1.size)
129     im_nl = im_n.load()
130
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]:
134                 im_nl[x, y] = 255
135     return im_n
136
137 def line_from_angl_dist((angle, distance), size):
138     x1 = - size[0] / 2
139     y1 = int(round((x1 * math.sin(angle) - distance)/math.cos(angle))) + size[1] / 2
140     x2 = size[0] / 2 
141     y2 = int(round((x2 * math.sin(angle) - distance)/math.cos(angle))) + size[1] / 2
142     return [(0, y1), (size[0] - 1, y2)]
143
144 if __name__ == '__main__':
145     sys.exit(main())