11 # a BLOCKS x BLOCKS section is correlated with a (BLOCKS + 1) x (BLOCKS + 1)
12 # the maximum allowable shift is BLOCK_SIZE with BLOCKS of surrounding context
19 in_jpg0 = 'tank_battle/down_2364.jpg'
20 in_jpg1 = 'tank_battle/down_2365.jpg'
21 out_jpg0 = 'out0_2364.jpg'
22 out_jpg1 = 'out0_2365.jpg'
24 print(f'read {in_jpg0:s}')
25 image0 = gamma.read_image(in_jpg0)
28 sys.stderr.write(f'write {out_jpg0:s}\n')
29 gamma.write_image(out_jpg0, image0)
31 print(f'read {in_jpg1:s}')
32 image1 = gamma.read_image(in_jpg1)
33 assert image1.shape == shape
38 print('xb', xb, 'yb', yb)
41 image0_lp0 = numpy.stack(
43 scipy.ndimage.gaussian_filter(image0[:, :, i], CUTOFF0, mode = 'mirror')
48 image0_lp1 = numpy.stack(
50 scipy.ndimage.gaussian_filter(image0[:, :, i], CUTOFF1, mode = 'mirror')
55 image0_bp = image0_lp1 - image0_lp0
56 gamma.write_image('image0_bp.jpg', image0_bp + .5)
59 image1_lp0 = numpy.stack(
61 scipy.ndimage.gaussian_filter(image1[:, :, i], CUTOFF0, mode = 'mirror')
66 image1_lp1 = numpy.stack(
68 scipy.ndimage.gaussian_filter(image1[:, :, i], CUTOFF1, mode = 'mirror')
73 image1_bp = image1_lp1 - image1_lp0
74 gamma.write_image('image1_bp.jpg', image1_bp + .5)
77 buckets = [[[], []], [[], []]]
78 for i in range(yb - BLOCKS):
80 y0 = y1 + BLOCK_SIZE // 2
81 for j in range(xb - BLOCKS):
83 x0 = x1 + BLOCK_SIZE // 2
86 y0:y0 + BLOCK_SIZE * BLOCKS,
87 x0:x0 + BLOCK_SIZE * BLOCKS,
91 y1:y1 + BLOCK_SIZE * (BLOCKS + 1),
92 x1:x1 + BLOCK_SIZE * (BLOCKS + 1),
96 # note: swapping block1, block0 flips the output (subtracts x and y
97 # from BLOCK_SIZE) and we need this to find matching part of image1
101 scipy.signal.correlate(
112 #temp = corr - numpy.mean(corr)
113 #temp /= 10. * numpy.sqrt(numpy.mean(numpy.square(temp)))
114 #gamma.write_image(f'corr_{i:d}_{j:d}.jpg', temp + .5)
116 y, x = numpy.unravel_index(numpy.argmax(corr), corr.shape)
119 y <= BLOCK_SIZE - CUTOFF1 and
121 x <= BLOCK_SIZE - CUTOFF1
123 buckets[i >= (yb - BLOCKS) // 2][j >= (xb - BLOCKS) // 2].append(
124 (x0, y0, x1, y1, x, y)
131 k = len(buckets[i][j]) // 2
132 xm = sorted([x for _, _, _, _, x, _ in buckets[i][j]])[k]
133 ym = sorted([y for _, _, _, _, _, y in buckets[i][j]])[k]
134 #print('i', i, 'j', j, 'xm', xm, 'ym', ym)
136 [[x, y] for _, _, _, _, x, y in buckets[i][j]],
139 v = numpy.array([xm, ym], numpy.double)
140 k = numpy.argmin(numpy.sum(numpy.square(u - v[numpy.newaxis, :]), 1))
141 x0, y0, x1, y1, x, y = buckets[i][j][k]
142 #print('i', i, 'j', j, 'x', x, 'y', y)
146 x0 + (BLOCKS * BLOCK_SIZE // 2),
147 y0 + (BLOCKS * BLOCK_SIZE // 2)
155 x1 + x + (BLOCKS * BLOCK_SIZE // 2),
156 y1 + y + (BLOCKS * BLOCK_SIZE // 2)
161 p = numpy.stack(p, 1)
162 q = numpy.stack(q, 1)
163 A = perspective.calc_transform(p, q)
166 coords = numpy.zeros((2, ys, xs), numpy.double)
167 coords[0, :, :] = numpy.arange(ys, dtype = numpy.double)[:, numpy.newaxis]
168 coords[1, :, :] = numpy.arange(xs, dtype = numpy.double)[numpy.newaxis, :]
169 mapped_coords = perspective.apply_transform_multi(
171 coords[::-1, :, :].reshape((2, ys * xs))
172 ).reshape(2, ys, xs)[::-1, :, :]
173 out_image1 = numpy.stack(
175 scipy.ndimage.map_coordinates(image1[:, :, j], mapped_coords)
181 sys.stderr.write(f'write {out_jpg1:s}\n')
182 gamma.write_image(out_jpg1, out_image1)