-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDCT.py
207 lines (164 loc) · 6.2 KB
/
DCT.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
:Author: Tomatis D.
:Date: 01/07/2018
:Company: CEA/DEN/DANS/DM2S/SERMA/LPEC
Implementation of the discrete cosine transform, according to
chap. 4 of [1] and the FFTPACK implementation of SciPy v0.14.0
( docs.scipy.org/doc/scipy-0.14.0/reference/generated/
scipy.fftpack.dct.html ).
References
----------
[1] SUN, Huifang et SHI, Yun Q. Image and Video Compression
for Multimedia Engineering: Fundamentals, Algorithms,
and standards. CRC press, 2008.
"""
import numpy as np
from scipy.fftpack import dct, idct
from numpy import random, testing
import warnings
def dct2D(A, norm=None, ttype=2):
"""Apply the Discrete Cosine Tranform to the element of the
2D input matrix A. Default normalization option is `ortho` to
have a orthonormal DCT-II transform.
:param A: input real 2D matrix
:param norm: type of normalization
:type A: ndarray, 2D array of `float` type
:type norm: str
:return B: transformed matrix (with column-wise vectors),
:rtype B: ndarray, 2D array of `float` type
"""
B = dct( dct( A, axis=0, norm=norm, type=ttype ),
axis=1, norm=norm, type=ttype )
return B
def idctII2D(A, norm='ortho'):
"""Apply the Inverse Transform of the orthonormalized DCT-II to
the 2D input matrix A. That is the orthonormalized DCT-III.
:param A: input real 2D matrix
:param norm: type of normalization
:type A: ndarray, 2D array of `float` type
:type norm: str
:return B: transformed matrix (with column-wise vectors),
:rtype B: ndarray, 2D array of `float` type
"""
B = dct( dct( A, axis=0, norm=norm, type=3 ),
axis=1, norm=norm, type=3 )
return B
def idctI2D(A):
"""Apply the Inverse Transform of the orthonormalized DCT-I to
the 2D input matrix A. That is the unnormalized DCT-I up to the
factor c.
:param A: input real 2D matrix
:param norm: type of normalization
:type A: ndarray, 2D array of `float` type
:type norm: str
:return B: transformed matrix (with column-wise vectors),
:rtype B: ndarray, 2D array of `float` type
"""
I, J = A.shape
cI, cJ = .5 / float(I - 1), .5 / float(J - 1)
B = dct( dct( A, axis=0, norm=None, type=1 ),
axis=1, norm=None, type=1 )
return B*(cI*cJ)
def idctIII2D(A):
"""Apply the Inverse Transform of the orthonormalized DCT-III to
the 2D input matrix A. That is the unnormalized DCT-II up to the
factor c.
:param A: input real 2D matrix
:param norm: type of normalization
:type A: ndarray, 2D array of `float` type
:type norm: str
:return B: transformed matrix (with column-wise vectors),
:rtype B: ndarray, 2D array of `float` type
"""
I, J = A.shape
cI, cJ = .5 / float(I), .5 / float(J)
B = dct( dct( A, axis=0, norm=None, type=2 ),
axis=1, norm=None, type=2 )
return B*(cI*cJ)
def dctnD(A, norm=None, ttype=2):
"""Apply the Discrete Cosine Tranform to the element of the input
multi-dimensional array A. Default option is no normalization and
DCT-II transform.
:param A: input real ndarray
:param norm: type of normalization
:type A: ndarray of `float` type
:type norm: str
:return B: ndarray of transformed coefficients,
:rtype B: ndarray of `float` type
"""
dims = A.shape
nb_dims, nb_els = len(dims), np.prod(dims)
B = np.array(A, copy=True)
for di, d in enumerate(dims):
B = dct( B.reshape(d, nb_els // d), \
norm=norm, type=ttype, axis=0 ).T
return B.reshape(dims)
def idctnD(A, norm=None, ttype=2):
"""Apply the Inverse Transform of the selected DCT-(ttype) to the
input multi-dimensional n-D array A.
:param A: input real n-D array
:param norm: type of requested normalization
:type A: ndarray of `float` type
:type norm: str
:return B: ndarray of transformed coefficients
:rtype B: ndarray of `float` type
"""
if ttype == 2: ttype = 3
elif ttype == 3: ttype = 2
elif ttype == 1: pass
else:
raise ValueError('Not yet implemented')
return dctnD(A, norm, ttype)
if __name__ == "__main__":
dim = 4
A = random.rand(dim,dim+2)
I, J = A.shape
# --- test 1 ---
# get the orthonorm.zed DCT-II coefficients
C = dct2D(A, norm='ortho')
# test the inverse transform
testing.assert_array_almost_equal(A, idctII2D(C),
err_msg="IDCT of ortho. DCT-II failed!")
# --- test 2 ---
# get the unnorm.zed DCT-II coefficients
C = dct2D(A)
# test the inverse transform
testing.assert_array_almost_equal(A,
idctII2D(C, norm=None)/(4.*I*J),
err_msg="IDCT of unnorm. DCT-II failed!")
# --- test 3 ---
# get the unnorm.zed DCT-I coefficients
C = dct2D(A, ttype=1)
# test the inverse transform
testing.assert_array_almost_equal(A, idctI2D(C),
err_msg="IDCT of norm. DCT-I failed!")
# --- test 4 ---
# get the unnorm.zed DCT-III coefficients
C = dct2D(A, ttype=3)
# test the inverse transform
testing.assert_array_almost_equal(A, idctIII2D(C),
err_msg="IDCT of norm. DCT-III failed!")
# --- test 5 ---
# create a multi-dimensional array
A = random.rand(2,3,4)
nb_els, nb_dims, dims = A.size, A.ndim, A.shape
# 5.1 multi-dimensional orthonorm.zed DCT-II
C = dctnD(A, norm='ortho')
testing.assert_array_almost_equal(A, idctnD(C, norm='ortho'),
err_msg="IDCT of norm. n-D DCT-II failed!")
# 5.2 multi-dimensional unnorm.zed DCT-II
C, c = dctnD(A), 1. / (2**nb_dims * nb_els)
testing.assert_array_almost_equal(A, idctnD(C, norm=None)*c,
err_msg="IDCT of unnorm. n-D DCT-II failed!")
# 5.3 multi-dimensional unnorm.zed DCT-I
C = dctnD(A, ttype=1)
c = 1. / (2**nb_dims * np.prod(np.array(dims) - 1))
testing.assert_array_almost_equal(A, idctnD(C, ttype=1)*c,
err_msg="IDCT of unnorm. n-D DCT-II failed!")
# 5.4 multi-dimensional unnorm.zed DCT-III
C = dctnD(A, ttype=3)
c = 1. / (2**nb_dims * nb_els)
testing.assert_array_almost_equal(A, idctnD(C, ttype=3)*c,
err_msg="IDCT of unnorm. n-D DCT-II failed!")