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 _, _, cs = image.shape
44 scipy.ndimage.gaussian_filter(image[:, :, i], CUTOFF1, mode = 'mirror') -
45 scipy.ndimage.gaussian_filter(image[:, :, i], CUTOFF0, mode = 'mirror')
51 def correlate(image0_bp, x0, y0, image1_bp, x1, y1):
53 y0:y0 + BLOCK_SIZE * BLOCKS,
54 x0:x0 + BLOCK_SIZE * BLOCKS,
58 y1:y1 + BLOCK_SIZE * (BLOCKS + 1),
59 x1:x1 + BLOCK_SIZE * (BLOCKS + 1),
63 # note: swapping block1, block0 flips the output (subtracts x and y
64 # from BLOCK_SIZE) and we need this to find matching part of image1
68 scipy.signal.correlate(
79 #temp = corr - numpy.mean(corr)
80 #temp /= 10. * numpy.sqrt(numpy.mean(numpy.square(temp)))
81 #gamma.write_image(f'corr_{i:d}_{j:d}.jpg', temp + .5)
83 y, x = numpy.unravel_index(numpy.argmax(corr), corr.shape)
88 x <= BLOCK_SIZE - CUTOFF1 and
90 y <= BLOCK_SIZE - CUTOFF1
96 image0_bp = bandpass(image0)
97 gamma.write_image('image0_bp.jpg', image0_bp + .5)
100 image1_bp = bandpass(image1)
101 gamma.write_image('image1_bp.jpg', image1_bp + .5)
104 buckets = [[[], []], [[], []]]
105 for i in range(yb - BLOCKS):
107 y0 = y1 + BLOCK_SIZE // 2
108 for j in range(xb - BLOCKS):
110 x0 = x1 + BLOCK_SIZE // 2
112 offset = correlate(image0_bp, x0, y0, image1_bp, x1, y1)
113 if offset is not None:
115 print('i', i, 'j', j, 'x', x, 'y', y)
116 buckets[i >= (yb - BLOCKS) // 2][j >= (xb - BLOCKS) // 2].append(
117 (x0, y0, x1, y1, x, y)
124 k = len(buckets[i][j]) // 2
125 xm = sorted([x for _, _, _, _, x, _ in buckets[i][j]])[k]
126 ym = sorted([y for _, _, _, _, _, y in buckets[i][j]])[k]
127 #print('i', i, 'j', j, 'xm', xm, 'ym', ym)
129 [[x, y] for _, _, _, _, x, y in buckets[i][j]],
132 v = numpy.array([xm, ym], numpy.double)
133 k = numpy.argmin(numpy.sum(numpy.square(u - v[numpy.newaxis, :]), 1))
134 x0, y0, x1, y1, x, y = buckets[i][j][k]
135 #print('i', i, 'j', j, 'x', x, 'y', y)
139 x0 + (BLOCKS * BLOCK_SIZE // 2),
140 y0 + (BLOCKS * BLOCK_SIZE // 2)
148 x1 + x + (BLOCKS * BLOCK_SIZE // 2),
149 y1 + y + (BLOCKS * BLOCK_SIZE // 2)
154 p = numpy.stack(p, 1)
155 q = numpy.stack(q, 1)
156 A = perspective.calc_transform(p, q)
159 out_image1 = perspective.remap_image(A, image1)
161 sys.stderr.write(f'write {out_jpg1:s}\n')
162 gamma.write_image(out_jpg1, out_image1)