diff 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
line wrap: on
line diff
--- a/pymctf.py	Tue Dec 18 10:09:28 2007 +0100
+++ b/pymctf.py	Tue Dec 18 15:16:56 2007 +0100
@@ -265,7 +265,59 @@
 
     return mvf, imvf
 
-def decompose_sequence(seq, Hs=[], MVFs=[], bs=8, sr=8, hlevel=2, tlp=MIDDLE):
+def decompose(seq, bs=8, sr=8, hlevel=2, tlp=MIDDLE):
+    if len(seq) == 1:
+        return seq, [], []
+
+    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)
+    
+    MVFs = []; Ls = []; Hs = []
+    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)
+        MVFs += [mvf]
+        L, H = ft_mvf(seq[i], seq[i+1], mvf, imvf, bs)
+        Ls += [L]
+        Hs += [H]
+
+    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)
+        MVFs += [mvf]
+        L, H = ft_mvf(seq[i+1], seq[i], mvf, imvf, bs)
+        Ls += [L]
+        Hs += [H]
+
+    return Ls, Hs, MVFs
+
+def decompose_using_mvf(seq, MVFs, bs=8, tlp=MIDDLE):
+    if len(seq) == 1:
+        return seq, []
+
+    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)
+    
+    Ls = []; Hs = []
+    for i in xrange(left, mid, 2):
+        mvf = MVFs[i//2]
+        mvf, imvf = inverse_mvf(mvf, bs)
+        L, H = ft_mvf(seq[i], seq[i+1], mvf, imvf, bs)
+        Ls += [L]
+        Hs += [H]
+
+    for i in xrange(mid, right, 2):
+        mvf = MVFs[i//2]
+        mvf, imvf = inverse_mvf(mvf, bs)
+        L, H = ft_mvf(seq[i+1], seq[i], mvf, imvf, bs)
+        Ls += [L]
+        Hs += [H]
+
+    return Ls, Hs
+
+def decompose_sequence(seq, 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.
@@ -276,34 +328,17 @@
     The tlp argument allows to move the temporal low-pass frame to the left, 
     middle or right.
     '''
-    Ls = []
+
+    Ls, Hs, MVFs = decompose(seq, bs, sr, hlevel, tlp)
 
-    if len(seq) == 1:
-        return seq, Hs, MVFs
-
-    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)
+    while len(Ls) > 1:
+        Ls, hs, mvfs = decompose(Ls, bs, sr, hlevel, tlp)
+        Hs = hs + Hs; MVFs = mvfs + MVFs
     
-    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)
-        MVFs.insert(i//2, mvf)
-        L, H = ft_mvf(seq[i], seq[i+1], mvf, imvf, bs)
-        Ls.append(L)
-        Hs.insert(i//2, H)
+    return Ls, Hs, MVFs
 
-    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)
-        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)
-
-def decompose_sequence_using_mvf(seq, Hs=[], MVFs=[], bs=8, tlp=MIDDLE):
+def decompose_sequence_using_mvf(seq, MVFs, bs=8, tlp=MIDDLE):
     '''
     Recursively decompose frame sequence using motion-compensated temporal filtering 
     employing the given motion vector field.
@@ -314,41 +349,18 @@
     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)
-
+    Ls, Hs = decompose_using_mvf(seq, MVFs[-len(seq)/2:], bs, tlp)
     del MVFs[-len(seq)/2:]
+    
+    while len(Ls) > 1:
+        Ls, hs = decompose_using_mvf(Ls, MVFs[-len(Ls)/2:], bs, tlp)
+        del MVFs[-len(Ls):]        
+        Hs = hs + Hs
+    
+    return Ls, Hs
 
-    return decompose_sequence_using_mvf(Ls, Hs, MVFs, bs, tlp)
-
-
-def reconstruct_sequence(seq, Hs, MVFs, bs=8, tlp=MIDDLE):
-    '''
-    Recursively reconstruct a frame sequence from temporal low- and high-pass subbands
-    and motion fields.
-    '''
-
-    Ls = []
+def reconstruct(seq, Hs, MVFs, bs=8, tlp=MIDDLE):
 
     if len(Hs) == 0:
       return seq
@@ -357,6 +369,7 @@
     elif tlp == LEFT: left = 0; mid = 0; right = len(seq)
     else: left = 0; mid = max(len(seq)/2, 1); right = len(seq)
     
+    Ls = []
     for i in xrange(0, mid):
         mvf = MVFs[0]
         mvf, imvf = inverse_mvf(mvf, bs)
@@ -373,5 +386,15 @@
         del Hs[0]
         del MVFs[0]
 
-    return reconstruct_sequence(Ls, Hs, MVFs, bs, tlp)
+    return Ls
 
+def reconstruct_sequence(seq, Hs, MVFs, bs=8, tlp=MIDDLE):
+    '''
+    Recursively reconstruct a frame sequence from temporal low- and high-pass subbands
+    and motion fields.
+    '''
+
+    while len(seq) <= len(Hs):
+        seq = reconstruct(seq, Hs, MVFs, bs, tlp)
+    return seq
+

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