def forward(self, prefix="/tmp/", N=5, T=2): '''Forward MCOLP. Compute a MC 1D-DWT, estimating in the L subbands and compensaing in the H subbands of the Orthogonal Laplacian Pyramid. The input video (as a sequence of 1-iteration decompositions) must be stored in disk in the directory <prefix>, and the output (as a 1-iteration MC decompositions) will generated in the same directory. Input ----- prefix : str Localization of the input/output images. Example: "/tmp/". N : int Number of decompositions to process. T : int Number of iterations of the MCOLP (temporal scales). Controls the GOP size. T | GOP_size ----+---------- 0 | 1 1 | 2 2 | 4 3 | 8 4 | 16 5 | 32 : | : Returns ------- The output motion compensated decompositions. ''' x = 2 for t in range(T): # Temporal scale #print("a={}".format(0), end=' ') i = 0 aL, aH = decomposition.read(prefix, "{:03d}".format(0)) while i < (N // x): #print("b={} c={}".format(x*i+x//2, x*i+x)) bL, bH = decomposition.read(prefix, "{:03d}".format(x * i + x // 2)) cL, cH = decomposition.read(prefix, "{:03d}".format(x * i + x)) residue_bH = self.__forward_butterfly(aL, aH, bL, bH, cL, cH) decomposition.writeH(residue_bH, prefix, "{:03d}".format(x * i + x // 2)) aL, aH = cL, cH #print("a={}".format(x*i+x), end=' ') i += 1 x *= 2
def forward(self, prefix = "/tmp/", N=5, T=2): '''A Motion Compensated Discrete Wavelet Transform. Compute the MC 1D-DWT. The input video (as a sequence of 1-levels decompositions) must be stored in disk in the directory <prefix>, and the output (as a 1-levels MC decompositions) will generated in the same directory. Input ----- prefix : str Localization of the input/output images. Example: "/tmp/". N : int Number of decompositions to process. T : int Number of levels of the MCDWT (temporal scales). Controls the GOP size. T | GOP_size ----+----------- 0 | 1 1 | 2 2 | 4 3 | 8 4 | 16 5 | 32 : | : P : int Predictor to use: 1 --> Average Predictor. 2 --> Weighted Average Predictor. Returns ------- None. ''' x = 2 for t in range(T): # Temporal scale i = 0 aL, aH = decomposition.read(prefix, "{:03d}".format(0)) while i < (N//x): bL, bH = decomposition.read(prefix, "{:03d}".format(x*i+x//2)) cL, cH = decomposition.read(prefix, "{:03d}".format(x*i+x)) bH = self.__forward_butterfly(aL, aH, bL, bH, cL, cH) decomposition.writeH(bH, prefix, "{:03d}".format(x*i+x//2)) aL, aH = cL, cH i += 1 x *= 2
def forward(self, s="/tmp/stockholm_", S="/tmp/mc_stockholm_", N=5, T=2): '''A Motion Compensated Discrete Wavelet Transform. Compute the MC 1D-DWT. The input video s (as a sequence of 1-levels decompositions) must be stored in disk and the output (as a 1-levels MC decompositions) will be stored in S. Imput: ----- prefix : s Localization of the input images. Example: "/tmp/stockholm_". N : int Number of images to process. T : int Number of levels of the MCDWT (temporal scales). Controls the GOP size. T | GOP_size ----+----------- 0 | 1 1 | 2 2 | 4 3 | 8 4 | 16 5 | 32 : | : Returns ------- prefix : S Localization of the output decompositions. For example: "/tmp/mc_stockholm_". ''' x = 2 for t in range(T): # Temporal scale i = 0 aL, aH = decomposition.read("{}{:03d}".format(s, 0)) decomposition.write((aL, aH), "{}{:03d}".format(S, 0)) while i < (N // x): bL, bH = decomposition.read("{}{:03d}".format( s, x * i + x // 2)) cL, cH = decomposition.read("{}{:03d}".format(s, x * i + x)) bH = self.__forward_butterfly(aL, aH, bL, bH, cL, cH) decomposition.write((bL, bH), "{}{:03d}".format(S, x * i + x // 2)) decomposition.write((cL, cH), "{}{:03d}".format(S, x * i + x)) aL, aH = cL, cH i += 1 x *= 2
def forward(self, prefix="/tmp/", N=5, I=2): '''Forward MCDWT. Compute the MC 1D-DWT. The input video (as a sequence of 1-iteration decompositions) must be stored in disk in the directory <prefix>, and the output (as a 1-iteration MC decompositions) will generated in the same directory. Input ----- prefix : str Localization of the input/output images. Example: "/tmp/". N : int Number of decompositions to process. I : int Number of iterations of the MCDWT (temporal scales). Controls the GOP size. I | GOP_size ----+---------- 0 | 1 1 | 2 2 | 4 3 | 8 4 | 16 5 | 32 : | : Returns ------- The output motion compensated decompositions. ''' x = 2 for t in range(I): # Temporal scale i = 0 aL, aH = decomposition.read(prefix, "{:03d}".format(0)) while i < (N // x): bL, bH = decomposition.read(prefix, "{:03d}".format(x * i + x // 2)) cL, cH = decomposition.read(prefix, "{:03d}".format(x * i + x)) residue_bH = self.__forward_butterfly(aL, aH, bL, bH, cL, cH) decomposition.writeH(residue_bH, prefix, "{:03d}".format(x * i + x // 2)) aL, aH = cL, cH i += 1 x *= 2
def backward(self, prefix = "/tmp/", N=5, T=2): x = 2**T for t in range(T): # Temporal scale i = 0 aL, aH = decomposition.read(prefix, "{:03d}".format(0)) while i < (N//x): bL, bH = decomposition.read(prefix, "{:03d}".format(x*i+x//2)) cL, cH = decomposition.read(prefix, "{:03d}".format(x*i+x)) bH = self.__backward_butterfly(aL, aH, bL, bH, cL, cH) decomposition.writeH(bH, "{:03d}".format(x*i+x//2)) aL, aH = cL, cH i += 1 x //=2
def backward(self, S="/tmp/mc_stockholm_", s="/tmp/stockholm_", N=5, T=2): x = 2**T for t in range(T): # Temporal scale i = 0 aL, aH = decomposition.read("{}{:03d}".format(S, 0)) decomposition.write((aL, aH), "{}{:03d}".format(s, 0)) while i < (N // x): bL, bH = decomposition.read("{}{:03d}".format( S, x * i + x // 2)) cL, cH = decomposition.read("{}{:03d}".format(S, x * i + x)) bH = self.__backward_butterfly(aL, aH, bL, bH, cL, cH) decomposition.write((bL, bH), "{}{:03d}".format(s, x * i + x // 2)) decomposition.write((cL, cH), "{}{:03d}".format(s, x * i + x)) aL, aH = cL, cH i += 1 x //= 2
def backward(self, prefix="/tmp/", N=5, T=2): '''Backward MCDWT. Compute the inverse MC 1D-DWT. The input sequence of 1-iteration MC decompositions must be stored in disk in the directory <prefix>, and the output (as a 1-iteration decompositions) will generated in the same directory. Input ----- prefix : str Localization of the input/output images. Example: "/tmp/". N : int Number of decompositions to process. T : int Number of iterations of the MCDWT (temporal scales). Returns ------- The sequence of 1-iteration decompositions. ''' x = 2**T for t in range(T): # Temporal scale i = 0 aL, aH = decomposition.read(prefix, "{:03d}".format(0)) while i < (N // x): bL, residue_bH = decomposition.read( prefix, "{:03d}".format(x * i + x // 2)) cL, cH = decomposition.read(prefix, "{:03d}".format(x * i + x)) bH = self.__backward_butterfly(aL, aH, bL, residue_bH, cL, cH) decomposition.writeH(bH, prefix, "{:03d}".format(x * i + x // 2)) aL, aH = cL, cH i += 1 x //= 2
def backward(self, S="/tmp/stockholm_", s="/tmp/stockholm_", N=5): ''' Motion 1-iteration forward 2D DWT of a sequence of decompositions. Compute the inverse 2D-DWT of each decomposition of the sequence S. Input: ----- S: the sequence of decompositions to be transformed. Output: ------ s: the sequence of images. ''' for i in range(N): pyr = decomposition.read("{}{:03d}".format(S, i)) img = self.dwt.backward(pyr) image.write(img, "{}{:03d}".format(s, i))
def backward(self, prefix="/tmp/", N=5): '''Motion 1-iteration forward 2D DWT of a sequence of decompositions. Compute the inverse 2D-DWT of each decomposition of the sequence of decompositions placed at <prefix>. Input: ----- prefix: the sequence of decompositions to be transformed. N: the number of decompositions. Output: ------ (disk): the sequence of images. ''' for i in range(N): pyr = decomposition.read(prefix, "{:03d}".format(i)) img = self.dwt.backward(pyr) image.write(img, prefix, "{:03d}".format(i))
"--index", help="Index of the image/decomposition", default="000") parser.add_argument("-w", "--wavelet", help="Wavelet name", default="bior3.5") parser.add_argument("-s", "--show", action='store_true', help="Show available wavelet names") args = parser.parse_args() dwt = DWT(wavelet=args.wavelet) if args.show: dwt.show() else: if args.backward: if __debug__: print("Backward transform") d = decomposition.read(args.prefix, args.index) i = dwt.backward(d) image.write(i, args.prefix, args.index) else: if __debug__: print("Forward transform") i = image.read(args.prefix, args.index) d = dwt.forward(i) decomposition.write(d, args.prefix, args.index)
"--image", help="Image to be transformed", default="/tmp/stockholm/000") parser.add_argument("-d", "--decomposition", help="Decomposition to be transformed", default="/tmp/stockholm_000") args = parser.parse_args() dwt = DWT() if args.backward: if __debug__: print("Backward transform") d = decomposition.read("{}".format(args.decomposition)) i = dwt.backward(d) #i = np.rint(i) image.write(i, "{}".format(args.image)) else: if __debug__: print("Forward transform") i = image.read("{}".format(args.image)) d = dwt.forward(i) #LL = np.rint(d[0]) #LH = np.rint(d[1][0]) #HL = np.rint(d[1][1]) #HH = np.rint(d[1][2]) #decomposition.write((LL, (LH, HL, HH)), "{}".format(args.decomposition)) decomposition.write(d, "{}".format(args.decomposition))