X-Git-Url: http://git.tomasm.cz/imago.git/blobdiff_plain/b91b0e43b25e64b3516b9156ca42e7309665628b..1e473b2725c875b2d4e6e69530a95eec2f3a901e:/gridf.py?ds=sidebyside diff --git a/gridf.py b/gridf.py index 6f044f1..0955d7e 100644 --- a/gridf.py +++ b/gridf.py @@ -53,16 +53,17 @@ def find(lines, size, l1, l2, bounds, hough, do_something, im_h): dr_l = ImageDraw.Draw(im_l) for line in sum(lines, []): dr_l.line(line_from_angl_dist(line, size), width=1, fill=255) - #im_l = im_l.filter(MyGaussianBlur(radius=30)) + + #im_l = im_h #hocus pocus + im_l = im_l.filter(MyGaussianBlur(radius=2)) #GaussianBlur is undocumented class, may not work in future versions of PIL - #im_l = im_l.tostring() - im_l = im_h.tostring() # hocus pocus + im_l_s = im_l.tostring() #from gridf_analyzer import error_surface #error_surface(im_l, a, b, c, d, hough, size, v1 ,v2) grid = get_grid(a, b, c, d, hough, size) - dist = distance(im_l, grid, size) + dist = distance(im_l_s, grid, size) #let's try the bruteforce aproach: s = 0.001 @@ -74,9 +75,10 @@ def find(lines, size, l1, l2, bounds, hough, do_something, im_h): pool = multiprocessing.Pool(None) - tasks = [(X[x], Y[x], im_l, a, b, c, d, s, v1, v2, k, hough, size) for x in xrange(0, 2 * k)] + tasks = [(X[x], Y[x], im_l_s, a, b, c, d, s, v1, v2, k, hough, size) for x in xrange(0, 2 * k)] - #start = time.time() + import time + start = time.time() opt_ab = pool.map(job_br1, tasks, 1) opt_cd = pool.map(job_br2, tasks, 1) an, bn, cn, dn = 4 * [0] @@ -92,66 +94,29 @@ def find(lines, size, l1, l2, bounds, hough, do_something, im_h): if tpl[0] > d1: d1 = tpl[0] c, d = tpl[1], tpl[2] - #print time.time() - start + print time.time() - start grid = get_grid(a, b, c, d, hough, size) grid_lines = [[l2ad(l, size) for l in grid[0]], [l2ad(l, size) for l in grid[1]]] - return grid, grid_lines + + ### - #old optimization experiments: - print dist - - path = [(0,0)] #MNTR - s = 0.01 - for _ in range(10): - ts1 = [(s, 0), (0, s), (-s, 0), (0, -s)] - grids = [(get_grid(a + t[0] * v1, b + t[1] * v1, - c, d, hough, size), t) for t in ts1] - distances = [distance(im_l, grid, size) for (grid, t) in grids] - gradient = [(di - dist) for di in distances] - gradient = [gradient[0] - gradient[2], gradient[1] - gradient[3]] - norm = (gradient[0] ** 2 + gradient[1] ** 2) ** 0.5 - gradient = [g / (100 * norm) for g in gradient] - path.append(gradient) - a, b = a + gradient[0] * v1, b + gradient[1] * v1 - dist = distance(im_l, grid, size) - print dist - - ###MNTR - import matplotlib.pyplot as plt - from matplotlib import cm - import pickle - - X, Y, Z = pickle.load(open('surface250')) - - plt.imshow(Z, cmap=cm.jet, interpolation='none', - origin='upper', extent=(-0.250, 0.250, -0.250, 0.250), aspect='equal') - plt.colorbar() - plt.plot([y for (x, y) in path], [x for (x, y) in path], 'go-') - - plt.show() - ###MNTR - - print "---" - - s = 0.02 - while True: - ts1 = [(s, 0), (-s, 0), (s, s), (-s, -s), (-s, s), (s, -s), (0, s), (0, -s)] - grids = [(get_grid(a, b, - c + t[0] * v2, d + t[1] * v2, hough, size), t) for t in ts1] - distances = [(distance(im_l, grid, size), - grid, t) for grid, t in grids] - distances.sort(reverse=True) - if distances[0][0] > dist: - dist = distances[0][0] - grid = distances[0][1] - t = distances[0][2] - c, d = c + t[0] * v2, d + t[1] * v2 - print dist - s *= 0.75 - else: - break + im_t = Image.new('RGB', im_l.size, None) + im_t_l = im_t.load() + im_l_l = im_l.load() + for x in xrange(im_t.size[0]): + for y in xrange(im_t.size[1]): + im_t_l[x, y] = (im_l_l[x, y], 0, 0) + + im_t_d = ImageDraw.Draw(im_t) + + for l in grid[0] + grid[1]: + im_t_d.line(l, width=1, fill=(0, 255, 0)) + + do_something(im_t, "lines and grid") + + + ### - grid_lines = [[l2ad(l, size) for l in grid[0]], [l2ad(l, size) for l in grid[1]]] return grid, grid_lines def get_grid(a, b, c, d, hough, size): @@ -185,22 +150,3 @@ def distance(im_l, grid, size): #im_d, distance = combine(im_l, im_g) distance = pcf.combine(im_l, im_g.tostring()) return distance - -def combine(bg, fg): - bg_l = bg.load() - fg_l = fg.load() - #res = Image.new('L', fg.size) - #res_l = res.load() - - score = 0 - area = 0 - - for x in xrange(fg.size[0]): - for y in xrange(fg.size[1]): - if fg_l[x, y]: - #res_l[x, y] = bg_l[x, y] * fg_l[x, y] - score += bg_l[x, y] - area += 1 - - #return res, float(score)/area - return None, float(score)/area