3 """Go image recognition"""
11 import Image, ImageDraw
12 except ImportError, msg:
13 print >>sys.stderr, msg
18 from hough import Hough
24 """Main function of the program."""
26 parser = argparse.ArgumentParser(description=__doc__)
27 parser.add_argument('file', metavar='file', nargs=1,
28 help="image to anlyse")
29 parser.add_argument('-w', type=int, default=640,
30 help="scales image to the specified width before analysis")
31 parser.add_argument('-d', '--debug', dest='show_all', action='store_true',
32 help="show every step of the computation")
33 parser.add_argument('-s', '--save', dest='do_something', action='store_const',
34 const=image_save, default=im_debug.show,
35 help="save images instead of displaying them")
36 args = parser.parse_args()
38 show_all = args.show_all
39 do_something = args.do_something
42 image = Image.open(args.file[0])
44 print >>sys.stderr, msg
46 if image.size[0] > args.w:
47 image = image.resize((args.w, int((float(args.w)/image.size[0]) *
48 image.size[1])), Image.ANTIALIAS)
50 Saving_dir = "saved/" + args.file[0][:-4] + "_" + str(image.size[0]) + "/"
53 do_something(image, "original image")
55 im_l = image.convert('L')
57 do_something(im_l, "ITU-R 601-2 luma transform")
59 im_edges = filters.edge_detection(im_l)
61 do_something(im_edges, "edge detection")
63 im_h = filters.high_pass(im_edges, 100)
65 do_something(im_h, "high pass filters")
67 hough1 = Hough(im_h.size)
68 im_hough = hough1.transform(im_h)
70 do_something(im_hough, "hough transform")
72 im_hough = filters.peaks(im_hough)
74 do_something(im_hough, "peak extraction")
76 im_h2 = filters.high_pass(im_hough, 120)
78 do_something(im_h2, "second high pass filters")
80 im_c = filters.components(im_h2)
82 do_something(im_c, "components centers")
85 hough2 = Hough(im_h2.size)
86 im_hough2 = hough2.transform(im_h2)
88 do_something(im_hough2, "second hough transform")
90 im_h3 = filters.high_pass(im_hough2, 120)
92 do_something(im_h3, "third high pass filters")
94 lines = hough2.find_angle_distance(im_h3)
96 im_lines = Image.new('L', im_h2.size)
98 draw = ImageDraw.Draw(im_lines)
101 draw.line(line_from_angl_dist(line, im_h2.size), fill=255)
103 do_something(im_lines, "lines")
105 im_c = combine(im_h2, im_lines)
107 do_something(im_c, "first hough x lines")
111 do_something(im_c, "optimalised hough")
114 lines = hough1.all_lines(im_c)
115 draw = ImageDraw.Draw(image)
117 draw.line(line_from_angl_dist(line, image.size), fill=(120, 255, 120))
119 do_something(image, "the grid")
123 def image_save(image, title=''):
126 filename = Saving_dir + "{0:0>2}".format(Saving_num) + '.jpg'
127 if not os.path.isdir(Saving_dir):
128 os.makedirs(Saving_dir)
129 image.save(filename, 'JPEG')
136 for y in xrange(image.size[1]):
137 for x in xrange(image.size[0]):
138 if im_l[x, y] and last:
146 def combine(image1, image2):
147 im_l1 = image1.load()
148 im_l2 = image2.load()
150 im_n = Image.new('L', image1.size)
153 for x in xrange(image1.size[0]):
154 for y in xrange(image1.size[1]):
155 if im_l1[x, y] and im_l2[x, y]:
159 def line_from_angl_dist((angle, distance), size):
161 y1 = int(round((x1 * math.sin(angle) - distance)/math.cos(angle))) + size[1] / 2
163 y2 = int(round((x2 * math.sin(angle) - distance)/math.cos(angle))) + size[1] / 2
164 return [(0, y1), (size[0] - 1, y2)]
166 if __name__ == '__main__':