3 """Go image recognition"""
9 from operator import itemgetter
12 import Image, ImageDraw
13 except ImportError, msg:
14 print >> sys.stderr, msg
19 from hough import Hough
25 """Main function of the program."""
27 parser = argparse.ArgumentParser(description=__doc__)
28 parser.add_argument('file', metavar='file', nargs=1,
29 help="image to analyse")
30 parser.add_argument('-w', type=int, default=640,
31 help="scale image to the specified width before analysis")
32 parser.add_argument('-d', '--debug', dest='show_all', action='store_true',
33 help="show every step of the computation")
34 parser.add_argument('-s', '--save', dest='do_something', action='store_const',
35 const=image_save, default=im_debug.show,
36 help="save images instead of displaying them")
37 parser.add_argument('-v', '--verbose', dest='verbose', action='store_true',
38 help="report progress")
39 args = parser.parse_args()
41 show_all = args.show_all
42 do_something = args.do_something
43 verbose = args.verbose
46 image = Image.open(args.file[0])
48 print >> sys.stderr, msg
50 if image.size[0] > args.w:
51 image = image.resize((args.w, int((float(args.w)/image.size[0]) *
52 image.size[1])), Image.ANTIALIAS)
54 Saving_dir = "saved/" + args.file[0][:-4] + "_" + str(image.size[0]) + "/"
57 print >> sys.stderr, "preprocessing"
60 do_something(image, "original image")
62 im_l = image.convert('L')
64 do_something(im_l, "ITU-R 601-2 luma transform")
67 print >> sys.stderr, "edge detection"
69 im_edges = filters.edge_detection(im_l)
71 do_something(im_edges, "edge detection")
73 im_h = filters.high_pass(im_edges, 100)
75 do_something(im_h, "high pass filters")
78 print >> sys.stderr, "hough transform"
80 hough1 = Hough(im_h.size)
81 im_hough = hough1.transform(im_h)
83 do_something(im_hough, "hough transform")
85 im_hough = filters.peaks(im_hough)
87 do_something(im_hough, "peak extraction")
89 im_h2 = filters.high_pass(im_hough, 120)
91 do_something(im_h2, "second high pass filters")
93 im_h2 = filters.components(im_h2)
95 do_something(im_h2, "components centers")
98 print >> sys.stderr, "second hough transform"
100 hough2 = Hough(im_h2.size)
101 im_hough2 = hough2.transform(im_h2)
103 do_something(im_hough2, "second hough transform")
105 im_h3 = filters.high_pass(im_hough2, 120)
107 do_something(im_h3, "third high pass filter")
109 im_h3 = filters.half_centers(im_h3)
111 do_something(im_h3, "half centers")
114 print >> sys.stderr, "finding the grid"
116 lines_m = hough2.all_lines(im_h3)
120 im_line = Image.new('L', im_h2.size)
121 draw = ImageDraw.Draw(im_line)
122 draw.line(line_from_angl_dist(line, im_h2.size), fill=255, width=5)
124 do_something(im_line, "line")
125 im_c = combine(im_h2, im_line)
127 do_something(im_c, "hough x lines")
128 lines.append(hough1.all_lines(im_c))
133 intersections = intersections_from_angl_dist(lines, image.size)
134 image_g = image.copy()
135 draw = ImageDraw.Draw(image_g)
136 for line in intersections:
138 draw.point((x , y), fill=(120, 255, 120))
140 for line in intersections:
141 print ' '.join([stone_color(image, intersection) for intersection in
145 do_something(image_g, "the grid")
149 def stone_color(image, (x, y)):
151 for i in range(-2, 3):
152 for j in range(-2, 3):
153 suma += sum(image.getpixel((x + i, y + j)))
162 def image_save(image, title=''):
165 filename = Saving_dir + "{0:0>2}".format(Saving_num) + '.jpg'
166 if not os.path.isdir(Saving_dir):
167 os.makedirs(Saving_dir)
168 image.save(filename, 'JPEG')
171 def combine(image1, image2):
172 im_l1 = image1.load()
173 im_l2 = image2.load()
175 im_n = Image.new('L', image1.size)
178 for x in xrange(image1.size[0]):
179 for y in xrange(image1.size[1]):
180 if im_l1[x, y] and im_l2[x, y]:
184 def line_from_angl_dist((angle, distance), size):
186 y1 = int(round((x1 * math.sin(angle) - distance) / math.cos(angle))) + size[1] / 2
188 y2 = int(round((x2 * math.sin(angle) - distance) / math.cos(angle))) + size[1] / 2
189 return [(0, y1), (size[0] - 1, y2)]
191 def intersections_from_angl_dist(lines, size):
193 for (angl1, dist1) in sorted(lines[1], key=itemgetter(1)):
195 for (angl2, dist2) in lines[0]:
196 if abs(angl1 - angl2) > 0.4:
197 x = - ((dist2 / math.cos(angl2))-(dist1 / math.cos(angl1))) / (math.tan(angl1) - math.tan(angl2))
198 y = (math.tan(angl1) * x) - (dist1 / math.cos(angl1))
199 line.append((int(x + size[0] / 2), int(y + size[1] / 2)))
200 intersections.append(line)
203 if __name__ == '__main__':