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