diff 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
line wrap: on
line diff
--- a/pymctf.py	Thu Sep 06 14:18:51 2007 +0200
+++ b/pymctf.py	Tue Dec 18 10:04:01 2007 +0100
@@ -19,6 +19,40 @@
 MIDDLE = 0
 RIGHT = 1
 
+def apply_mc(a, mvf=None, bs=8):
+    mc_a = numpy.empty(a.shape, numpy.float)
+    for r in xrange(0, a.shape[0], bs):
+        for c in xrange(0, a.shape[1], bs):
+            mv = mvf[r,c]
+            rm, cm = r+mv[0],c+mv[1]
+            mc_a[r:r+bs,c:c+bs] = a[rm:rm+bs,cm:cm+bs]
+    return mc_a
+
+def apply_mc_all(w, mvfs, bs=8, tlp=MIDDLE):
+    ws_in = [w]
+
+    take = 1
+    while take <= len(mvfs):
+        ws_out = [] 
+        
+        if tlp == RIGHT: left = 0; mid = take; right = 0
+        elif tlp == LEFT: left = 0; mid = 0; right = take
+        else: left = 0; mid = max(take/2, 1); right = take
+
+        for i in xrange(left, mid):
+            ws_out.append(apply_mc(ws_in[i], mvfs[i], bs))
+            ws_out.append(ws_in[i])
+        for i in xrange(mid, right):
+            ws_out.append(ws_in[i])
+            ws_out.append(apply_mc(ws_in[i], mvfs[i]))
+
+        ws_in = ws_out
+        del mvfs[:take]
+
+        take *= 2
+
+    return ws_in
+
 def me(a, refblock, rc, cc, sr):
     min_sad = sys.maxint
     min_r, min_c = 0, 0
@@ -231,7 +265,7 @@
 
     return mvf, imvf
 
-def decompose_sequence(seq, Hs=[], MVFs=[], bs=8, sr=8, hlevel=2, tlp=MIDDLE, visualize_mvf=False, dlevel=-1):
+def decompose_sequence(seq, Hs=[], MVFs=[], bs=8, sr=8, hlevel=2, tlp=MIDDLE):
     '''
     Recursively decompose frame sequence using motion-compensated temporal filtering 
     employing the parameters blocksize, searchrange and hierarchy level for motion estimation.
@@ -243,7 +277,6 @@
     middle or right.
     '''
     Ls = []
-    if dlevel < 0: dlevel = int(math.log(len(seq), 2))
 
     if len(seq) == 1:
         return seq, Hs, MVFs
@@ -255,8 +288,6 @@
     for i in xrange(left, mid, 2):
         sad, mvf = motion_estimation(seq[i+1], seq[i], bs, sr, hlevel)
         mvf, imvf = inverse_mvf(mvf, bs)
-        if visualize_mvf: 
-            show_mvf(mvf, imvf, i, dlevel-1, bs, sr) 
         MVFs.insert(i//2, mvf)
         L, H = ft_mvf(seq[i], seq[i+1], mvf, imvf, bs)
         Ls.append(L)
@@ -265,14 +296,51 @@
     for i in xrange(mid, right, 2):
         sad, mvf = motion_estimation(seq[i], seq[i+1], bs, sr, hlevel)
         mvf, imvf = inverse_mvf(mvf, bs)
-        if visualize_mvf: 
-            show_mvf(mvf, imvf, i, dlevel-1, bs, sr) 
         MVFs.insert(i//2, mvf)
         L, H = ft_mvf(seq[i+1], seq[i], mvf, imvf, bs)
         Ls.append(L)
         Hs.insert(i//2, H)
 
-    return decompose_sequence(Ls, Hs, MVFs, bs, sr, hlevel, tlp, visualize_mvf, dlevel-1)
+    return decompose_sequence(Ls, Hs, MVFs, bs, sr, hlevel, tlp)
+
+def decompose_sequence_using_mvf(seq, Hs=[], MVFs=[], bs=8, tlp=MIDDLE):
+    '''
+    Recursively decompose frame sequence using motion-compensated temporal filtering 
+    employing the given motion vector field.
+    
+    Output is [L], [H0, H1, H1, H2, H2, H2, H2] for
+    a sequence of length 8.
+    
+    The tlp argument allows to move the temporal low-pass frame to the left, 
+    middle or right.
+    '''
+    Ls = []
+
+    if len(seq) == 1:
+        return seq, Hs
+
+    if tlp == RIGHT: left = 0; mid = len(seq); right = 0
+    elif tlp == LEFT: left = 0; mid = 0; right = len(seq)
+    else: left = 0; mid = max(len(seq)/2, 2); right = len(seq)
+    
+    for i in xrange(left, mid, 2):
+        mvf = MVFs[(-len(seq)+i)/2]
+        mvf, imvf = inverse_mvf(mvf, bs)
+        L, H = ft_mvf(seq[i], seq[i+1], mvf, imvf, bs)
+        Ls.append(L)
+        Hs.insert(i//2, H)
+
+    for i in xrange(mid, right, 2):
+        mvf = MVFs[(-len(seq)+i)/2 ]
+        mvf, imvf = inverse_mvf(mvf, bs)
+        L, H = ft_mvf(seq[i+1], seq[i], mvf, imvf, bs)
+        Ls.append(L)
+        Hs.insert(i//2, H)
+
+    del MVFs[-len(seq)/2:]
+
+    return decompose_sequence_using_mvf(Ls, Hs, MVFs, bs, tlp)
+
 
 def reconstruct_sequence(seq, Hs, MVFs, bs=8, tlp=MIDDLE):
     '''

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