Mercurial > hg > pymctf
comparison pymctf.py @ 3:0e5a584fd6b2
add apply_mc()
author | Peter Meerwald <pmeerw@cosy.sbg.ac.at> |
---|---|
date | Tue, 18 Dec 2007 10:04:01 +0100 |
parents | f22cbbbb6814 |
children | 4fc1d403ad14 |
comparison
equal
deleted
inserted
replaced
2:f22cbbbb6814 | 3:0e5a584fd6b2 |
---|---|
16 | 16 |
17 # temporal low-pass frame position | 17 # temporal low-pass frame position |
18 LEFT = -1 | 18 LEFT = -1 |
19 MIDDLE = 0 | 19 MIDDLE = 0 |
20 RIGHT = 1 | 20 RIGHT = 1 |
21 | |
22 def apply_mc(a, mvf=None, bs=8): | |
23 mc_a = numpy.empty(a.shape, numpy.float) | |
24 for r in xrange(0, a.shape[0], bs): | |
25 for c in xrange(0, a.shape[1], bs): | |
26 mv = mvf[r,c] | |
27 rm, cm = r+mv[0],c+mv[1] | |
28 mc_a[r:r+bs,c:c+bs] = a[rm:rm+bs,cm:cm+bs] | |
29 return mc_a | |
30 | |
31 def apply_mc_all(w, mvfs, bs=8, tlp=MIDDLE): | |
32 ws_in = [w] | |
33 | |
34 take = 1 | |
35 while take <= len(mvfs): | |
36 ws_out = [] | |
37 | |
38 if tlp == RIGHT: left = 0; mid = take; right = 0 | |
39 elif tlp == LEFT: left = 0; mid = 0; right = take | |
40 else: left = 0; mid = max(take/2, 1); right = take | |
41 | |
42 for i in xrange(left, mid): | |
43 ws_out.append(apply_mc(ws_in[i], mvfs[i], bs)) | |
44 ws_out.append(ws_in[i]) | |
45 for i in xrange(mid, right): | |
46 ws_out.append(ws_in[i]) | |
47 ws_out.append(apply_mc(ws_in[i], mvfs[i])) | |
48 | |
49 ws_in = ws_out | |
50 del mvfs[:take] | |
51 | |
52 take *= 2 | |
53 | |
54 return ws_in | |
21 | 55 |
22 def me(a, refblock, rc, cc, sr): | 56 def me(a, refblock, rc, cc, sr): |
23 min_sad = sys.maxint | 57 min_sad = sys.maxint |
24 min_r, min_c = 0, 0 | 58 min_r, min_c = 0, 0 |
25 bs = refblock.shape[0] | 59 bs = refblock.shape[0] |
229 numpy.place(blockimvf[:,:,1], unconnected, -mvf[r,c,1]) | 263 numpy.place(blockimvf[:,:,1], unconnected, -mvf[r,c,1]) |
230 numpy.place(blockimvf[:,:,2], unconnected, CONNECTED) | 264 numpy.place(blockimvf[:,:,2], unconnected, CONNECTED) |
231 | 265 |
232 return mvf, imvf | 266 return mvf, imvf |
233 | 267 |
234 def decompose_sequence(seq, Hs=[], MVFs=[], bs=8, sr=8, hlevel=2, tlp=MIDDLE, visualize_mvf=False, dlevel=-1): | 268 def decompose_sequence(seq, Hs=[], MVFs=[], bs=8, sr=8, hlevel=2, tlp=MIDDLE): |
235 ''' | 269 ''' |
236 Recursively decompose frame sequence using motion-compensated temporal filtering | 270 Recursively decompose frame sequence using motion-compensated temporal filtering |
237 employing the parameters blocksize, searchrange and hierarchy level for motion estimation. | 271 employing the parameters blocksize, searchrange and hierarchy level for motion estimation. |
238 | 272 |
239 Output is [L], [H0, H1, H1, H2, H2, H2, H2], [MVF0, MVF1, MVF1, MVF2, MVF2, MVF2, MVF2] for | 273 Output is [L], [H0, H1, H1, H2, H2, H2, H2], [MVF0, MVF1, MVF1, MVF2, MVF2, MVF2, MVF2] for |
241 | 275 |
242 The tlp argument allows to move the temporal low-pass frame to the left, | 276 The tlp argument allows to move the temporal low-pass frame to the left, |
243 middle or right. | 277 middle or right. |
244 ''' | 278 ''' |
245 Ls = [] | 279 Ls = [] |
246 if dlevel < 0: dlevel = int(math.log(len(seq), 2)) | |
247 | 280 |
248 if len(seq) == 1: | 281 if len(seq) == 1: |
249 return seq, Hs, MVFs | 282 return seq, Hs, MVFs |
250 | 283 |
251 if tlp == RIGHT: left = 0; mid = len(seq); right = 0 | 284 if tlp == RIGHT: left = 0; mid = len(seq); right = 0 |
253 else: left = 0; mid = max(len(seq)/2, 2); right = len(seq) | 286 else: left = 0; mid = max(len(seq)/2, 2); right = len(seq) |
254 | 287 |
255 for i in xrange(left, mid, 2): | 288 for i in xrange(left, mid, 2): |
256 sad, mvf = motion_estimation(seq[i+1], seq[i], bs, sr, hlevel) | 289 sad, mvf = motion_estimation(seq[i+1], seq[i], bs, sr, hlevel) |
257 mvf, imvf = inverse_mvf(mvf, bs) | 290 mvf, imvf = inverse_mvf(mvf, bs) |
258 if visualize_mvf: | |
259 show_mvf(mvf, imvf, i, dlevel-1, bs, sr) | |
260 MVFs.insert(i//2, mvf) | 291 MVFs.insert(i//2, mvf) |
261 L, H = ft_mvf(seq[i], seq[i+1], mvf, imvf, bs) | 292 L, H = ft_mvf(seq[i], seq[i+1], mvf, imvf, bs) |
262 Ls.append(L) | 293 Ls.append(L) |
263 Hs.insert(i//2, H) | 294 Hs.insert(i//2, H) |
264 | 295 |
265 for i in xrange(mid, right, 2): | 296 for i in xrange(mid, right, 2): |
266 sad, mvf = motion_estimation(seq[i], seq[i+1], bs, sr, hlevel) | 297 sad, mvf = motion_estimation(seq[i], seq[i+1], bs, sr, hlevel) |
267 mvf, imvf = inverse_mvf(mvf, bs) | 298 mvf, imvf = inverse_mvf(mvf, bs) |
268 if visualize_mvf: | |
269 show_mvf(mvf, imvf, i, dlevel-1, bs, sr) | |
270 MVFs.insert(i//2, mvf) | 299 MVFs.insert(i//2, mvf) |
271 L, H = ft_mvf(seq[i+1], seq[i], mvf, imvf, bs) | 300 L, H = ft_mvf(seq[i+1], seq[i], mvf, imvf, bs) |
272 Ls.append(L) | 301 Ls.append(L) |
273 Hs.insert(i//2, H) | 302 Hs.insert(i//2, H) |
274 | 303 |
275 return decompose_sequence(Ls, Hs, MVFs, bs, sr, hlevel, tlp, visualize_mvf, dlevel-1) | 304 return decompose_sequence(Ls, Hs, MVFs, bs, sr, hlevel, tlp) |
305 | |
306 def decompose_sequence_using_mvf(seq, Hs=[], MVFs=[], bs=8, tlp=MIDDLE): | |
307 ''' | |
308 Recursively decompose frame sequence using motion-compensated temporal filtering | |
309 employing the given motion vector field. | |
310 | |
311 Output is [L], [H0, H1, H1, H2, H2, H2, H2] for | |
312 a sequence of length 8. | |
313 | |
314 The tlp argument allows to move the temporal low-pass frame to the left, | |
315 middle or right. | |
316 ''' | |
317 Ls = [] | |
318 | |
319 if len(seq) == 1: | |
320 return seq, Hs | |
321 | |
322 if tlp == RIGHT: left = 0; mid = len(seq); right = 0 | |
323 elif tlp == LEFT: left = 0; mid = 0; right = len(seq) | |
324 else: left = 0; mid = max(len(seq)/2, 2); right = len(seq) | |
325 | |
326 for i in xrange(left, mid, 2): | |
327 mvf = MVFs[(-len(seq)+i)/2] | |
328 mvf, imvf = inverse_mvf(mvf, bs) | |
329 L, H = ft_mvf(seq[i], seq[i+1], mvf, imvf, bs) | |
330 Ls.append(L) | |
331 Hs.insert(i//2, H) | |
332 | |
333 for i in xrange(mid, right, 2): | |
334 mvf = MVFs[(-len(seq)+i)/2 ] | |
335 mvf, imvf = inverse_mvf(mvf, bs) | |
336 L, H = ft_mvf(seq[i+1], seq[i], mvf, imvf, bs) | |
337 Ls.append(L) | |
338 Hs.insert(i//2, H) | |
339 | |
340 del MVFs[-len(seq)/2:] | |
341 | |
342 return decompose_sequence_using_mvf(Ls, Hs, MVFs, bs, tlp) | |
343 | |
276 | 344 |
277 def reconstruct_sequence(seq, Hs, MVFs, bs=8, tlp=MIDDLE): | 345 def reconstruct_sequence(seq, Hs, MVFs, bs=8, tlp=MIDDLE): |
278 ''' | 346 ''' |
279 Recursively reconstruct a frame sequence from temporal low- and high-pass subbands | 347 Recursively reconstruct a frame sequence from temporal low- and high-pass subbands |
280 and motion fields. | 348 and motion fields. |