Calculate 2D IDCT using Python.
code
test.py
from numpy import *
from scipy.fftpack import dct, idct
set_printoptions(linewidth=200, precision=1, suppress=True)
def dct2(a):
return dct( dct( a, axis=0, norm='ortho' ), axis=1, norm='ortho' )
def idct2(a):
return idct( idct( a, axis=0 , norm='ortho'), axis=1 , norm='ortho')
a = array(
[
( 568, 0, 0, -4, -4, 0, 4, 0 ),
( -27, 9, -4, -4, 0, -5, 5, -5 ),
( -49, -4, 4, 4, 0, 0, 0, 0 ),
( -12, -4, 0, 0, 5, 0, 0, 0 ),
( -14, -5, 0, 0, 0, 0, 0, 0 ),
( -5, 0, 0, 0, 0, 0, 0, 0 ),
( -5, 0, 0, 0, 0, 0, 0, 0 ),
( 0, 0, 0, 0, 0, 0, 0, 1 ),
]
);
print("a=")
print(a)
print("idct2(a)=")
print(idct2(a))
print("dct2(idct2(a))=")
print(dct2(idct2(a)))
I will do it.
> python --version
Python 3.4.3
> python .\test.py
a=
[[568 0 0 -4 -4 0 4 0]
[-27 9 -4 -4 0 -5 5 -5]
[-49 -4 4 4 0 0 0 0]
[-12 -4 0 0 5 0 0 0]
[-14 -5 0 0 0 0 0 0]
[ -5 0 0 0 0 0 0 0]
[ -5 0 0 0 0 0 0 0]
[ 0 0 0 0 0 0 0 1]]
idct2(a)=
[[ 52.9 53.6 54.8 54. 53. 56.3 50.3 56.9]
[ 67.9 70.5 71.4 68.1 65.2 68.6 62.4 66.4]
[ 74.4 78.4 80.5 75. 72. 74.5 70. 71.2]
[ 74.7 78.5 81.3 78.3 75.3 77.5 75.1 76.2]
[ 75.7 76.4 80.1 78.9 78.2 77.5 78.4 79.5]
[ 75.6 74. 76.1 76.4 76. 75. 77.3 77.4]
[ 73.3 71. 72.3 70.7 72.5 72.7 77.1 73.9]
[ 67. 65.6 66.1 63.6 66.5 69.6 75. 69.4]]
dct2(idct2(a))=
[[ 568. -0. -0. -4. -4. -0. 4. -0.]
[ -27. 9. -4. -4. 0. -5. 5. -5.]
[ -49. -4. 4. 4. -0. 0. 0. -0.]
[ -12. -4. -0. -0. 5. 0. 0. -0.]
[ -14. -5. -0. 0. 0. 0. -0. -0.]
[ -5. -0. 0. 0. -0. 0. 0. -0.]
[ -5. 0. 0. 0. -0. -0. 0. -0.]
[ 0. 0. 0. -0. 0. -0. 0. 1.]]
Recommended Posts