annotate 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
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
1 # MCTF following Ohm04a
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
2
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
3 import pywt
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
4 import numpy
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
5 import math
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
6 import sys
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
7 import _me
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
8
2
f22cbbbb6814 enable psyco, add README
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents: 1
diff changeset
9 import psyco
f22cbbbb6814 enable psyco, add README
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents: 1
diff changeset
10 psyco.full()
f22cbbbb6814 enable psyco, add README
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents: 1
diff changeset
11
0
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
12 # type of motion vectors
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
13 UNCONNECTED = -(sys.maxint)
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
14 CONNECTED = -(sys.maxint-1)
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
15 MULTIPLE_CONNECTED = -(sys.maxint-2)
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
16
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
17 # temporal low-pass frame position
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
18 LEFT = -1
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
19 MIDDLE = 0
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
20 RIGHT = 1
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
21
3
0e5a584fd6b2 add apply_mc()
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents: 2
diff changeset
22 def apply_mc(a, mvf=None, bs=8):
0e5a584fd6b2 add apply_mc()
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents: 2
diff changeset
23 mc_a = numpy.empty(a.shape, numpy.float)
0e5a584fd6b2 add apply_mc()
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents: 2
diff changeset
24 for r in xrange(0, a.shape[0], bs):
0e5a584fd6b2 add apply_mc()
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents: 2
diff changeset
25 for c in xrange(0, a.shape[1], bs):
0e5a584fd6b2 add apply_mc()
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents: 2
diff changeset
26 mv = mvf[r,c]
0e5a584fd6b2 add apply_mc()
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents: 2
diff changeset
27 rm, cm = r+mv[0],c+mv[1]
0e5a584fd6b2 add apply_mc()
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents: 2
diff changeset
28 mc_a[r:r+bs,c:c+bs] = a[rm:rm+bs,cm:cm+bs]
0e5a584fd6b2 add apply_mc()
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents: 2
diff changeset
29 return mc_a
0e5a584fd6b2 add apply_mc()
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents: 2
diff changeset
30
0e5a584fd6b2 add apply_mc()
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents: 2
diff changeset
31 def apply_mc_all(w, mvfs, bs=8, tlp=MIDDLE):
0e5a584fd6b2 add apply_mc()
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents: 2
diff changeset
32 ws_in = [w]
0e5a584fd6b2 add apply_mc()
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents: 2
diff changeset
33
0e5a584fd6b2 add apply_mc()
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents: 2
diff changeset
34 take = 1
0e5a584fd6b2 add apply_mc()
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents: 2
diff changeset
35 while take <= len(mvfs):
0e5a584fd6b2 add apply_mc()
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents: 2
diff changeset
36 ws_out = []
0e5a584fd6b2 add apply_mc()
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents: 2
diff changeset
37
0e5a584fd6b2 add apply_mc()
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents: 2
diff changeset
38 if tlp == RIGHT: left = 0; mid = take; right = 0
0e5a584fd6b2 add apply_mc()
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents: 2
diff changeset
39 elif tlp == LEFT: left = 0; mid = 0; right = take
0e5a584fd6b2 add apply_mc()
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents: 2
diff changeset
40 else: left = 0; mid = max(take/2, 1); right = take
0e5a584fd6b2 add apply_mc()
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents: 2
diff changeset
41
0e5a584fd6b2 add apply_mc()
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents: 2
diff changeset
42 for i in xrange(left, mid):
0e5a584fd6b2 add apply_mc()
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents: 2
diff changeset
43 ws_out.append(apply_mc(ws_in[i], mvfs[i], bs))
0e5a584fd6b2 add apply_mc()
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents: 2
diff changeset
44 ws_out.append(ws_in[i])
0e5a584fd6b2 add apply_mc()
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents: 2
diff changeset
45 for i in xrange(mid, right):
0e5a584fd6b2 add apply_mc()
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents: 2
diff changeset
46 ws_out.append(ws_in[i])
0e5a584fd6b2 add apply_mc()
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents: 2
diff changeset
47 ws_out.append(apply_mc(ws_in[i], mvfs[i]))
0e5a584fd6b2 add apply_mc()
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents: 2
diff changeset
48
0e5a584fd6b2 add apply_mc()
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents: 2
diff changeset
49 ws_in = ws_out
0e5a584fd6b2 add apply_mc()
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents: 2
diff changeset
50 del mvfs[:take]
0e5a584fd6b2 add apply_mc()
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents: 2
diff changeset
51
0e5a584fd6b2 add apply_mc()
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents: 2
diff changeset
52 take *= 2
0e5a584fd6b2 add apply_mc()
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents: 2
diff changeset
53
0e5a584fd6b2 add apply_mc()
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents: 2
diff changeset
54 return ws_in
0e5a584fd6b2 add apply_mc()
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents: 2
diff changeset
55
0
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
56 def me(a, refblock, rc, cc, sr):
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
57 min_sad = sys.maxint
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
58 min_r, min_c = 0, 0
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
59 bs = refblock.shape[0]
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
60 for rs in xrange(max(0,rc-sr),min(a.shape[0]-bs,rc+sr)+1):
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
61 for cs in xrange(max(0,cc-sr),min(cc+sr,a.shape[1]-bs)+1):
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
62 sad = numpy.sum(numpy.abs(refblock - a[rs:rs+bs, cs:cs+bs]))
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
63 if sad < min_sad:
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
64 # found new local block SAD minimum, store motion vector
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
65 min_r, min_c, min_sad = rs, cs, sad
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
66 return min_r, min_c, min_sad
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
67
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
68 def motion_estimation(a, b, blocksize=8, searchrange=8, hlevel=2):
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
69 '''
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
70 Hierarchical motion estimation from frame a to frame b.
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
71 Parameters are blocksize, searchrange and search hierarchy level.
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
72 Precision is full pixel only.
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
73 Returns the sum-of-absolute-differences (SAD) and the motion
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
74 vector field (MVF).
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
75 '''
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
76
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
77 mvf = numpy.zeros((b.shape[0], b.shape[1], 3), numpy.int)
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
78 mvf[:,:,2] = UNCONNECTED
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
79
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
80 ref = numpy.asarray(b, numpy.float)
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
81
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
82 # downsample frame data using Haar wavelet
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
83 w = pywt.Wavelet('haar')
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
84 ha = pywt.wavedec2(a, w, level=hlevel)
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
85 href = pywt.wavedec2(ref, w, level=hlevel)
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
86
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
87 # grows by 2 for every level
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
88 hbs = blocksize//2**hlevel
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
89 hsr = searchrange//2**hlevel
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
90
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
91 while True:
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
92 total_sad = 0.0
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
93 _2hlevel = 2**hlevel
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
94 for r in xrange(0, href[0].shape[0], hbs):
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
95 for c in xrange(0, href[0].shape[1], hbs):
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
96 rm = r * _2hlevel
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
97 cm = c * _2hlevel
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
98
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
99 # set center of new search of previously found vector at higher level
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
100 if mvf[rm,cm,2] >= 0: rc, cc = mvf[rm,cm,0]*2 + r, mvf[rm,cm,1]*2 + c
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
101 else: rc, cc = r, c
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
102 rs, cs, sad = _me.me(ha[0], href[0][r:r+hbs,c:c+hbs], rc, cc, hsr)
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
103 mvf[rm:rm+blocksize,cm:cm+blocksize,:] = rs - r, cs - c, int(sad)
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
104 total_sad += sad
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
105
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
106 if hlevel == 0: break
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
107
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
108 # upsample frame data using Haar wavelet
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
109 ha = [pywt.waverec2(ha[:2], w)] + ha[2:]
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
110 href = [pywt.waverec2(href[:2], w)] + href[2:]
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
111 hbs *= 2
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
112 hlevel -= 1
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
113
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
114 return total_sad, mvf
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
115
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
116 def ft_mvf(a, b, mvf, imvf, bs=8):
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
117 '''
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
118 Motion-compensated temporal decomposition between frame a and b
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
119 using Haar wavelet applying a given forward and inverse motion field.
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
120 '''
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
121
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
122 H = numpy.empty(a.shape, numpy.float)
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
123 L = numpy.empty(a.shape, numpy.float)
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
124
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
125 i0 = numpy.indices((bs,bs))[0]
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
126 i1 = numpy.indices((bs,bs))[1]
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
127
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
128 for r in xrange(0, a.shape[0], bs):
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
129 for c in xrange(0, a.shape[1], bs):
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
130 rm = mvf[r, c, 0] + r
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
131 cm = mvf[r, c, 1] + c
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
132 H[r:r+bs, c:c+bs] = numpy.asarray(a[r:r+bs,c:c+bs], numpy.float) - b[rm:rm+bs,cm:cm+bs]
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
133 rm = r + imvf[r:r+bs,c:c+bs,0] + i0
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
134 cm = c + imvf[r:r+bs,c:c+bs,1] + i1
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
135 _a = a[rm, cm]
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
136 L[r:r+bs, c:c+bs] = numpy.where( \
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
137 imvf[r:r+bs, c:c+bs, 2] == UNCONNECTED, \
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
138 numpy.asarray(b[r:r+bs, c:c+bs], numpy.float), \
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
139 0.5 * (numpy.asarray(b[r:r+bs, c:c+bs], numpy.float) + _a))
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
140
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
141 return L, H
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
142
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
143 def it_mvf(L, H, mvf, imvf, bs=8):
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
144 '''
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
145 Reconstruction of two frames a and b from temporal low- and high-pass subband
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
146 using Haar wavelet and applying the given forward and inverse motion field.
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
147 '''
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
148
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
149 i0 = numpy.indices((bs,bs))[0]
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
150 i1 = numpy.indices((bs,bs))[1]
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
151
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
152 b = numpy.empty(L.shape, numpy.float)
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
153 for r in xrange(0, L.shape[0], bs):
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
154 for c in xrange(0, L.shape[1], bs):
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
155 _L = L[r:r+bs,c:c+bs]
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
156 rm = r + imvf[r:r+bs,c:c+bs,0] + i0
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
157 cm = c + imvf[r:r+bs,c:c+bs,1] + i1
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
158 _H = H[rm, cm]
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
159 b[r:r+bs,c:c+bs] = numpy.where( \
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
160 imvf[r:r+bs,c:c+bs,2] == UNCONNECTED, \
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
161 _L, \
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
162 _L - 0.5 * _H)
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
163
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
164 a = numpy.empty(L.shape, numpy.float)
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
165 for r in xrange(0, L.shape[0], bs):
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
166 for c in xrange(0, L.shape[1], bs):
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
167 rm = mvf[r, c, 0] + r
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
168 cm = mvf[r, c, 1] + c
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
169 _H = H[r:r+bs,c:c+bs]
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
170 a[r:r+bs, c:c+bs] = numpy.where( \
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
171 mvf[r:r+bs,c:c+bs,2] == MULTIPLE_CONNECTED, \
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
172 b[rm:rm+bs,cm:cm+bs] + _H, \
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
173 L[rm:rm+bs,cm:cm+bs] + 0.5 * _H)
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
174
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
175 return a, b
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
176
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
177 def _show_mv_dist(mvf, idx=0, level=0, sr=8, fname='mv_dist'):
1
b67c5ec1a9f0 import only needed modules
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents: 0
diff changeset
178 import Image, ImageDraw
0
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
179 im = Image.new('RGB', (mvf.shape[1], mvf.shape[0]))
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
180 d = ImageDraw.Draw(im)
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
181
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
182 for r in xrange(mvf.shape[0]):
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
183 for c in xrange(mvf.shape[1]):
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
184 mv = mvf[r][c]
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
185
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
186 if sr > 0: w = int(math.sqrt(mv[0]**2 + mv[1]**2)*255/(sr*math.sqrt(2.0)))
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
187 else: w = 0
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
188
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
189 if mv[2] >= 0 or mv[2] == CONNECTED: color = (0, w, 0)
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
190 elif mv[2] == UNCONNECTED: color = (255, 0, 0)
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
191 elif mv[2] == MULTIPLE_CONNECTED: color = (0, 0, w)
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
192
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
193 d.point((c, r), fill=color)
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
194
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
195 del d
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
196 im.save('%s-%02d-%04d.png' % (fname, level, idx), 'PNG')
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
197 del im
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
198
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
199 def show_mvf(mvf, imvf, idx=0, level=0, bs=8, sr=8):
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
200 '''
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
201 Visualize the motion field as .png and output motion vectors to .txt.
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
202 '''
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
203
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
204 im = Image.new('RGB', (mvf.shape[1]*2, mvf.shape[0]*2))
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
205 d = ImageDraw.Draw(im)
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
206 f = open('mv-%02d-%04d.txt' % (level, idx), 'wt')
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
207 sad = mvf[:,:,2].ravel()
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
208 sad_min = numpy.min(numpy.where(sad < 0.0, 0, sad))
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
209 sad_max = numpy.max(sad)
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
210 for r in xrange(0,mvf.shape[0],bs):
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
211 for c in xrange(0,mvf.shape[1],bs):
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
212 mv = mvf[r][c]
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
213 print >>f, '(%d %d)' % (mv[1], mv[0]),
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
214
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
215 # fill block according to SAD
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
216 if sad_max > 0 and mv[2] > 0:
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
217 d.rectangle([(c*2,r*2),(c*2+bs*2,r*2+bs*2)], fill=((mv[2]-sad_min)*255/sad_max,0,0))
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
218
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
219 # draw motion vector
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
220 if sr > 0: w = int(math.sqrt(mv[0]**2 + mv[1]**2)/(sr*math.sqrt(2.0)))
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
221 else: w = 0
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
222
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
223 d.line([ \
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
224 (c*2+bs, r*2+bs), \
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
225 (c*2+bs+mv[1]*2, r*2+bs+mv[0]*2)], \
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
226 fill=(0,int(32+(255-32)*w),0))
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
227 d.point((c*2+bs, r*2+bs), fill=(255,255,255))
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
228
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
229 print >>f
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
230 print >>f
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
231
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
232 f.close()
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
233 del d
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
234
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
235 im.save('mv-%02d-%04d.png' % (level, idx), 'PNG')
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
236 del im
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
237
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
238 _show_mv_dist(mvf, idx, level, sr, 'mvf_dist')
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
239 _show_mv_dist(imvf, idx, level, sr, 'mvi_dist')
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
240
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
241
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
242 def inverse_mvf(mvf, bs=8):
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
243 '''
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
244 Compute the inverse of the motion field.
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
245 '''
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
246
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
247 imvf = numpy.zeros((mvf.shape[0], mvf.shape[1], 3), numpy.int)
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
248 imvf[:,:,2] = UNCONNECTED
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
249 for r in xrange(0, mvf.shape[0], bs):
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
250 for c in xrange(0, mvf.shape[1], bs):
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
251 rm = mvf[r,c,0] + r
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
252 cm = mvf[r,c,1] + c
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
253
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
254 blockmvf = mvf[r:r+bs,c:c+bs]
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
255 blockimvf = imvf[rm:rm+bs,cm:cm+bs]
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
256
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
257 # mark multiple connected in forward motion field if pixel already connected
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
258 numpy.place(blockmvf[:,:,2], blockimvf[:,:,2] > UNCONNECTED, MULTIPLE_CONNECTED)
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
259
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
260 # invert motion vector and store in inverse motion field, mark pixel as connected
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
261 unconnected = blockimvf[:,:,2] == UNCONNECTED
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
262 numpy.place(blockimvf[:,:,0], unconnected, -mvf[r,c,0])
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
263 numpy.place(blockimvf[:,:,1], unconnected, -mvf[r,c,1])
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
264 numpy.place(blockimvf[:,:,2], unconnected, CONNECTED)
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
265
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
266 return mvf, imvf
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
267
3
0e5a584fd6b2 add apply_mc()
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents: 2
diff changeset
268 def decompose_sequence(seq, Hs=[], MVFs=[], bs=8, sr=8, hlevel=2, tlp=MIDDLE):
0
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
269 '''
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
270 Recursively decompose frame sequence using motion-compensated temporal filtering
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
271 employing the parameters blocksize, searchrange and hierarchy level for motion estimation.
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
272
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
273 Output is [L], [H0, H1, H1, H2, H2, H2, H2], [MVF0, MVF1, MVF1, MVF2, MVF2, MVF2, MVF2] for
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
274 a sequence of length 8.
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
275
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
276 The tlp argument allows to move the temporal low-pass frame to the left,
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
277 middle or right.
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
278 '''
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
279 Ls = []
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
280
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
281 if len(seq) == 1:
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
282 return seq, Hs, MVFs
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
283
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
284 if tlp == RIGHT: left = 0; mid = len(seq); right = 0
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
285 elif tlp == LEFT: left = 0; mid = 0; right = len(seq)
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
286 else: left = 0; mid = max(len(seq)/2, 2); right = len(seq)
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
287
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
288 for i in xrange(left, mid, 2):
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
289 sad, mvf = motion_estimation(seq[i+1], seq[i], bs, sr, hlevel)
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
290 mvf, imvf = inverse_mvf(mvf, bs)
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
291 MVFs.insert(i//2, mvf)
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
292 L, H = ft_mvf(seq[i], seq[i+1], mvf, imvf, bs)
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
293 Ls.append(L)
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
294 Hs.insert(i//2, H)
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
295
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
296 for i in xrange(mid, right, 2):
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
297 sad, mvf = motion_estimation(seq[i], seq[i+1], bs, sr, hlevel)
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
298 mvf, imvf = inverse_mvf(mvf, bs)
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
299 MVFs.insert(i//2, mvf)
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
300 L, H = ft_mvf(seq[i+1], seq[i], mvf, imvf, bs)
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
301 Ls.append(L)
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
302 Hs.insert(i//2, H)
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
303
3
0e5a584fd6b2 add apply_mc()
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents: 2
diff changeset
304 return decompose_sequence(Ls, Hs, MVFs, bs, sr, hlevel, tlp)
0e5a584fd6b2 add apply_mc()
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents: 2
diff changeset
305
0e5a584fd6b2 add apply_mc()
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents: 2
diff changeset
306 def decompose_sequence_using_mvf(seq, Hs=[], MVFs=[], bs=8, tlp=MIDDLE):
0e5a584fd6b2 add apply_mc()
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents: 2
diff changeset
307 '''
0e5a584fd6b2 add apply_mc()
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents: 2
diff changeset
308 Recursively decompose frame sequence using motion-compensated temporal filtering
0e5a584fd6b2 add apply_mc()
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents: 2
diff changeset
309 employing the given motion vector field.
0e5a584fd6b2 add apply_mc()
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents: 2
diff changeset
310
0e5a584fd6b2 add apply_mc()
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents: 2
diff changeset
311 Output is [L], [H0, H1, H1, H2, H2, H2, H2] for
0e5a584fd6b2 add apply_mc()
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents: 2
diff changeset
312 a sequence of length 8.
0e5a584fd6b2 add apply_mc()
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents: 2
diff changeset
313
0e5a584fd6b2 add apply_mc()
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents: 2
diff changeset
314 The tlp argument allows to move the temporal low-pass frame to the left,
0e5a584fd6b2 add apply_mc()
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents: 2
diff changeset
315 middle or right.
0e5a584fd6b2 add apply_mc()
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents: 2
diff changeset
316 '''
0e5a584fd6b2 add apply_mc()
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents: 2
diff changeset
317 Ls = []
0e5a584fd6b2 add apply_mc()
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents: 2
diff changeset
318
0e5a584fd6b2 add apply_mc()
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents: 2
diff changeset
319 if len(seq) == 1:
0e5a584fd6b2 add apply_mc()
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents: 2
diff changeset
320 return seq, Hs
0e5a584fd6b2 add apply_mc()
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents: 2
diff changeset
321
0e5a584fd6b2 add apply_mc()
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents: 2
diff changeset
322 if tlp == RIGHT: left = 0; mid = len(seq); right = 0
0e5a584fd6b2 add apply_mc()
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents: 2
diff changeset
323 elif tlp == LEFT: left = 0; mid = 0; right = len(seq)
0e5a584fd6b2 add apply_mc()
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents: 2
diff changeset
324 else: left = 0; mid = max(len(seq)/2, 2); right = len(seq)
0e5a584fd6b2 add apply_mc()
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents: 2
diff changeset
325
0e5a584fd6b2 add apply_mc()
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents: 2
diff changeset
326 for i in xrange(left, mid, 2):
0e5a584fd6b2 add apply_mc()
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents: 2
diff changeset
327 mvf = MVFs[(-len(seq)+i)/2]
0e5a584fd6b2 add apply_mc()
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents: 2
diff changeset
328 mvf, imvf = inverse_mvf(mvf, bs)
0e5a584fd6b2 add apply_mc()
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents: 2
diff changeset
329 L, H = ft_mvf(seq[i], seq[i+1], mvf, imvf, bs)
0e5a584fd6b2 add apply_mc()
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents: 2
diff changeset
330 Ls.append(L)
0e5a584fd6b2 add apply_mc()
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents: 2
diff changeset
331 Hs.insert(i//2, H)
0e5a584fd6b2 add apply_mc()
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents: 2
diff changeset
332
0e5a584fd6b2 add apply_mc()
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents: 2
diff changeset
333 for i in xrange(mid, right, 2):
0e5a584fd6b2 add apply_mc()
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents: 2
diff changeset
334 mvf = MVFs[(-len(seq)+i)/2 ]
0e5a584fd6b2 add apply_mc()
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents: 2
diff changeset
335 mvf, imvf = inverse_mvf(mvf, bs)
0e5a584fd6b2 add apply_mc()
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents: 2
diff changeset
336 L, H = ft_mvf(seq[i+1], seq[i], mvf, imvf, bs)
0e5a584fd6b2 add apply_mc()
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents: 2
diff changeset
337 Ls.append(L)
0e5a584fd6b2 add apply_mc()
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents: 2
diff changeset
338 Hs.insert(i//2, H)
0e5a584fd6b2 add apply_mc()
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents: 2
diff changeset
339
0e5a584fd6b2 add apply_mc()
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents: 2
diff changeset
340 del MVFs[-len(seq)/2:]
0e5a584fd6b2 add apply_mc()
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents: 2
diff changeset
341
0e5a584fd6b2 add apply_mc()
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents: 2
diff changeset
342 return decompose_sequence_using_mvf(Ls, Hs, MVFs, bs, tlp)
0e5a584fd6b2 add apply_mc()
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents: 2
diff changeset
343
0
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
344
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
345 def reconstruct_sequence(seq, Hs, MVFs, bs=8, tlp=MIDDLE):
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
346 '''
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
347 Recursively reconstruct a frame sequence from temporal low- and high-pass subbands
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
348 and motion fields.
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
349 '''
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
350
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
351 Ls = []
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
352
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
353 if len(Hs) == 0:
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
354 return seq
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
355
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
356 if tlp == RIGHT: left = 0; mid = len(seq); right = 0
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
357 elif tlp == LEFT: left = 0; mid = 0; right = len(seq)
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
358 else: left = 0; mid = max(len(seq)/2, 1); right = len(seq)
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
359
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
360 for i in xrange(0, mid):
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
361 mvf = MVFs[0]
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
362 mvf, imvf = inverse_mvf(mvf, bs)
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
363 a, b = it_mvf(seq[i], Hs[0], mvf, imvf, bs)
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
364 Ls += [a] + [b]
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
365 del Hs[0]
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
366 del MVFs[0]
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
367
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
368 for i in xrange(mid, right):
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
369 mvf = MVFs[0]
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
370 mvf, imvf = inverse_mvf(mvf, bs)
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
371 a, b = it_mvf(seq[i], Hs[0], mvf, imvf, bs)
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
372 Ls += [b] + [a]
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
373 del Hs[0]
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
374 del MVFs[0]
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
375
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
376 return reconstruct_sequence(Ls, Hs, MVFs, bs, tlp)
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
377

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