comparison pymctf.py @ 5:b235e08ebd04

cleaner recursion, eliminate parameters
author Peter Meerwald <pmeerw@cosy.sbg.ac.at>
date Tue, 18 Dec 2007 15:16:56 +0100
parents 4fc1d403ad14
children
comparison
equal deleted inserted replaced
4:4fc1d403ad14 5:b235e08ebd04
263 numpy.place(blockimvf[:,:,1], unconnected, -mvf[r,c,1]) 263 numpy.place(blockimvf[:,:,1], unconnected, -mvf[r,c,1])
264 numpy.place(blockimvf[:,:,2], unconnected, CONNECTED) 264 numpy.place(blockimvf[:,:,2], unconnected, CONNECTED)
265 265
266 return mvf, imvf 266 return mvf, imvf
267 267
268 def decompose_sequence(seq, Hs=[], MVFs=[], bs=8, sr=8, hlevel=2, tlp=MIDDLE): 268 def decompose(seq, bs=8, sr=8, hlevel=2, tlp=MIDDLE):
269 '''
270 Recursively decompose frame sequence using motion-compensated temporal filtering
271 employing the parameters blocksize, searchrange and hierarchy level for motion estimation.
272
273 Output is [L], [H0, H1, H1, H2, H2, H2, H2], [MVF0, MVF1, MVF1, MVF2, MVF2, MVF2, MVF2] for
274 a sequence of length 8.
275
276 The tlp argument allows to move the temporal low-pass frame to the left,
277 middle or right.
278 '''
279 Ls = []
280
281 if len(seq) == 1: 269 if len(seq) == 1:
282 return seq, Hs, MVFs 270 return seq, [], []
283 271
284 if tlp == RIGHT: left = 0; mid = len(seq); right = 0 272 if tlp == RIGHT: left = 0; mid = len(seq); right = 0
285 elif tlp == LEFT: left = 0; mid = 0; right = len(seq) 273 elif tlp == LEFT: left = 0; mid = 0; right = len(seq)
286 else: left = 0; mid = max(len(seq)/2, 2); right = len(seq) 274 else: left = 0; mid = max(len(seq)/2, 2); right = len(seq)
287 275
276 MVFs = []; Ls = []; Hs = []
288 for i in xrange(left, mid, 2): 277 for i in xrange(left, mid, 2):
289 sad, mvf = motion_estimation(seq[i+1], seq[i], bs, sr, hlevel) 278 sad, mvf = motion_estimation(seq[i+1], seq[i], bs, sr, hlevel)
290 mvf, imvf = inverse_mvf(mvf, bs) 279 mvf, imvf = inverse_mvf(mvf, bs)
291 MVFs.insert(i//2, mvf) 280 MVFs += [mvf]
292 L, H = ft_mvf(seq[i], seq[i+1], mvf, imvf, bs) 281 L, H = ft_mvf(seq[i], seq[i+1], mvf, imvf, bs)
293 Ls.append(L) 282 Ls += [L]
294 Hs.insert(i//2, H) 283 Hs += [H]
295 284
296 for i in xrange(mid, right, 2): 285 for i in xrange(mid, right, 2):
297 sad, mvf = motion_estimation(seq[i], seq[i+1], bs, sr, hlevel) 286 sad, mvf = motion_estimation(seq[i], seq[i+1], bs, sr, hlevel)
298 mvf, imvf = inverse_mvf(mvf, bs) 287 mvf, imvf = inverse_mvf(mvf, bs)
299 MVFs.insert(i//2, mvf) 288 MVFs += [mvf]
300 L, H = ft_mvf(seq[i+1], seq[i], mvf, imvf, bs) 289 L, H = ft_mvf(seq[i+1], seq[i], mvf, imvf, bs)
301 Ls.append(L) 290 Ls += [L]
302 Hs.insert(i//2, H) 291 Hs += [H]
303 292
304 return decompose_sequence(Ls, Hs, MVFs, bs, sr, hlevel, tlp) 293 return Ls, Hs, MVFs
305 294
306 def decompose_sequence_using_mvf(seq, Hs=[], MVFs=[], bs=8, tlp=MIDDLE): 295 def decompose_using_mvf(seq, 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: 296 if len(seq) == 1:
320 return seq, Hs 297 return seq, []
321 298
322 if tlp == RIGHT: left = 0; mid = len(seq); right = 0 299 if tlp == RIGHT: left = 0; mid = len(seq); right = 0
323 elif tlp == LEFT: left = 0; mid = 0; right = len(seq) 300 elif tlp == LEFT: left = 0; mid = 0; right = len(seq)
324 else: left = 0; mid = max(len(seq)/2, 2); right = len(seq) 301 else: left = 0; mid = max(len(seq)/2, 2); right = len(seq)
325 302
303 Ls = []; Hs = []
326 for i in xrange(left, mid, 2): 304 for i in xrange(left, mid, 2):
327 mvf = MVFs[(-len(seq)+i)/2] 305 mvf = MVFs[i//2]
328 mvf, imvf = inverse_mvf(mvf, bs) 306 mvf, imvf = inverse_mvf(mvf, bs)
329 L, H = ft_mvf(seq[i], seq[i+1], mvf, imvf, bs) 307 L, H = ft_mvf(seq[i], seq[i+1], mvf, imvf, bs)
330 Ls.append(L) 308 Ls += [L]
331 Hs.insert(i//2, H) 309 Hs += [H]
332 310
333 for i in xrange(mid, right, 2): 311 for i in xrange(mid, right, 2):
334 mvf = MVFs[(-len(seq)+i)/2 ] 312 mvf = MVFs[i//2]
335 mvf, imvf = inverse_mvf(mvf, bs) 313 mvf, imvf = inverse_mvf(mvf, bs)
336 L, H = ft_mvf(seq[i+1], seq[i], mvf, imvf, bs) 314 L, H = ft_mvf(seq[i+1], seq[i], mvf, imvf, bs)
337 Ls.append(L) 315 Ls += [L]
338 Hs.insert(i//2, H) 316 Hs += [H]
339 317
318 return Ls, Hs
319
320 def decompose_sequence(seq, bs=8, sr=8, hlevel=2, tlp=MIDDLE):
321 '''
322 Recursively decompose frame sequence using motion-compensated temporal filtering
323 employing the parameters blocksize, searchrange and hierarchy level for motion estimation.
324
325 Output is [L], [H0, H1, H1, H2, H2, H2, H2], [MVF0, MVF1, MVF1, MVF2, MVF2, MVF2, MVF2] for
326 a sequence of length 8.
327
328 The tlp argument allows to move the temporal low-pass frame to the left,
329 middle or right.
330 '''
331
332 Ls, Hs, MVFs = decompose(seq, bs, sr, hlevel, tlp)
333
334 while len(Ls) > 1:
335 Ls, hs, mvfs = decompose(Ls, bs, sr, hlevel, tlp)
336 Hs = hs + Hs; MVFs = mvfs + MVFs
337
338 return Ls, Hs, MVFs
339
340
341 def decompose_sequence_using_mvf(seq, MVFs, bs=8, tlp=MIDDLE):
342 '''
343 Recursively decompose frame sequence using motion-compensated temporal filtering
344 employing the given motion vector field.
345
346 Output is [L], [H0, H1, H1, H2, H2, H2, H2] for
347 a sequence of length 8.
348
349 The tlp argument allows to move the temporal low-pass frame to the left,
350 middle or right.
351 '''
352
353 Ls, Hs = decompose_using_mvf(seq, MVFs[-len(seq)/2:], bs, tlp)
340 del MVFs[-len(seq)/2:] 354 del MVFs[-len(seq)/2:]
341 355
342 return decompose_sequence_using_mvf(Ls, Hs, MVFs, bs, tlp) 356 while len(Ls) > 1:
343 357 Ls, hs = decompose_using_mvf(Ls, MVFs[-len(Ls)/2:], bs, tlp)
344 358 del MVFs[-len(Ls):]
345 def reconstruct_sequence(seq, Hs, MVFs, bs=8, tlp=MIDDLE): 359 Hs = hs + Hs
346 ''' 360
347 Recursively reconstruct a frame sequence from temporal low- and high-pass subbands 361 return Ls, Hs
348 and motion fields. 362
349 ''' 363 def reconstruct(seq, Hs, MVFs, bs=8, tlp=MIDDLE):
350
351 Ls = []
352 364
353 if len(Hs) == 0: 365 if len(Hs) == 0:
354 return seq 366 return seq
355 367
356 if tlp == RIGHT: left = 0; mid = len(seq); right = 0 368 if tlp == RIGHT: left = 0; mid = len(seq); right = 0
357 elif tlp == LEFT: left = 0; mid = 0; right = len(seq) 369 elif tlp == LEFT: left = 0; mid = 0; right = len(seq)
358 else: left = 0; mid = max(len(seq)/2, 1); right = len(seq) 370 else: left = 0; mid = max(len(seq)/2, 1); right = len(seq)
359 371
372 Ls = []
360 for i in xrange(0, mid): 373 for i in xrange(0, mid):
361 mvf = MVFs[0] 374 mvf = MVFs[0]
362 mvf, imvf = inverse_mvf(mvf, bs) 375 mvf, imvf = inverse_mvf(mvf, bs)
363 a, b = it_mvf(seq[i], Hs[0], mvf, imvf, bs) 376 a, b = it_mvf(seq[i], Hs[0], mvf, imvf, bs)
364 Ls += [a] + [b] 377 Ls += [a] + [b]
371 a, b = it_mvf(seq[i], Hs[0], mvf, imvf, bs) 384 a, b = it_mvf(seq[i], Hs[0], mvf, imvf, bs)
372 Ls += [b] + [a] 385 Ls += [b] + [a]
373 del Hs[0] 386 del Hs[0]
374 del MVFs[0] 387 del MVFs[0]
375 388
376 return reconstruct_sequence(Ls, Hs, MVFs, bs, tlp) 389 return Ls
377 390
391 def reconstruct_sequence(seq, Hs, MVFs, bs=8, tlp=MIDDLE):
392 '''
393 Recursively reconstruct a frame sequence from temporal low- and high-pass subbands
394 and motion fields.
395 '''
396
397 while len(seq) <= len(Hs):
398 seq = reconstruct(seq, Hs, MVFs, bs, tlp)
399 return seq
400

Repositories maintained by Peter Meerwald, pmeerw@pmeerw.net.