본문 바로가기
Language/Python

[Python] DCT 8x8

by VIR&US 2023. 7. 23.
반응형

목차


    DCT에 대하여

    간략하게 정리한 글입니다. DCT에 대한 내용은 해당 글 참고바랍니다.

     

    2023.07.23 - [Knowledge/Basic] - 2차원 DCT(Discrete cosine transform), 이산 코사인 변환

     

    2차원 DCT(Discrete cosine transform), 이산 코사인 변환

    목차 DCT 흔히 DCT라 부르는 DCT2, DCT역변환은 DCT3이자 IDCT(Inverse DCT)라 부릅니다. 간단하게 DCT2는 공간 영역에서 주파수 영역으로 변환하는 방법입니다. 흔히 사용하는 이미지 확장자 JPEG라는 국제

    vir-us.tistory.com


    DCT 8x8 변환식

    $$ f_{x,y}=\frac{1}{4}\alpha \left (u \right )\alpha \left ( v \right )\sum_{u=0}^{7}\sum_{v=0}^{7}F_{u,v}cos\left [ \frac{(2x+1)u\pi }{16}\right ]cos\left [\frac{(2y+1)v\pi }{16}\right ] $$

    수학적으로 계산해보자. x=0, y=0일때를 가정하면 다음과 같다.

     

    위 식에 x=0, y=0을 대입하여 식 정리:

     

    $$ f_{0,0}=\frac{1}{4}\sum_{u=0}^{7}\sum_{v=0}^{7}\alpha(u)\alpha(v)F_{u,v}\cos\left(\frac{u\pi}{16}\right)\cos\left(\frac{v\pi}{16}\right) $$

    각 \( u \)와 \( v \)에 대해, 각 항들:

     

    • When \( u=0 \), \( v=0 \):
    • $$ \frac{1}{4} \cdot \frac{1}{\sqrt{2}} \cdot \frac{1}{\sqrt{2}} \cdot F_{0,0} \cdot \cos\left(\frac{0\pi}{16}\right) \cdot \cos\left(\frac{0\pi}{16}\right) $$
    • When \( u=0 \), \( v=1 \):
    • $$ \frac{1}{4} \cdot \frac{1}{\sqrt{2}} \cdot 1 \cdot F_{0,1} \cdot \cos\left(\frac{0\pi}{16}\right) \cdot \cos\left(\frac{1\pi}{16}\right) $$
    • When \( u=1 \), \( v=0 \):
    • $$ \frac{1}{4} \cdot 1 \cdot \frac{1}{\sqrt{2}} \cdot F_{1,0} \cdot \cos\left(\frac{1\pi}{16}\right) \cdot \cos\left(\frac{0\pi}{16}\right) $$
    • When \( u=1 \), \( v=1 \):
    • $$ \frac{1}{4} \cdot 1 \cdot 1 \cdot F_{1,1} \cdot \cos\left(\frac{1\pi}{16}\right) \cdot \cos\left(\frac{1\pi}{16}\right) $$

    ...

     

    • When \( u=7 \), \( v=6 \):
    • $$ \frac{1}{4} \cdot 1 \cdot 1 \cdot F_{7,6} \cdot \cos\left(\frac{7\pi}{16}\right) \cdot \cos\left(\frac{6\pi}{16}\right) $$
    • When \( u=7 \), \( v=7 \):
    • $$ \frac{1}{4} \cdot 1 \cdot 1 \cdot F_{7,7} \cdot \cos\left(\frac{7\pi}{16}\right) \cdot \cos\left(\frac{7\pi}{16}\right) $$

    최종합계는 위 항들을 모두 더하면 된다.

    8x8 행렬이기에 이과정을 64번 반복하면 행렬이 완성된다.


    Python DCT, IDCT

    일단 Python으로 DCT를 계산하는 방법은 인터넷에 많이 있습니다.

    해당 코드는 numpy와 scipy가 필요합니다. 터미널에서 해당 패키지를 설치해줍니다.

     

    python -m pip install scipy  
    

     

    python -m pip install numpy  
    

    Code

    import numpy as np
    from scipy.fftpack import dct, idct
    
    # 8x8 입력 데이터
    x = np.array([
        [16, 11, 10, 16, 24, 40, 51, 61],
        [12, 12, 14, 19, 26, 58, 60, 55],
        [14, 13, 16, 24, 40, 57, 69, 56],
        [14, 17, 22, 29, 51, 87, 80, 62],
        [18, 22, 37, 56, 68, 109, 103, 77],
        [24, 35, 55, 64, 81, 104, 113, 92],
        [49, 64, 78, 87, 103, 121, 120, 101],
        [72, 92, 95, 98, 112, 100, 103, 99]
    ])
    
    # DCT-II 변환
    y = dct(dct(x.T, type=2, norm='ortho').T, type=2, norm='ortho')
    
    # IDCT 변환 (DCT의 역변환)
    x_reconstructed = idct(idct(y.T, type=2, norm='ortho').T, type=2, norm='ortho')
    
    print("DCT:\n", y)
    print("Reconstructed from IDCT:\n", x_reconstructed)
    

    Result

    DCT:
     [[ 4.61000000e+02 -1.68360713e+02 -1.49830296e+01  3.00003677e+01
      -3.10000000e+01  8.86180477e+00  1.06481113e+00 -2.59644590e+00]
     [-1.94429190e+02 -1.81503392e+00  3.82910959e+01  4.75835106e+00
       6.56842027e+00  3.91244792e+00  5.81915341e+00 -5.84319007e+00]
     [ 3.24246554e+01  4.35581181e+01  1.05585047e+01 -2.22948525e+01
       1.54642002e+01 -1.08418427e+01 -5.53033009e+00  4.49921782e+00]
     [-2.61581186e+00 -2.78610569e+01 -1.01537314e+00 -4.12800097e-01
      -2.85250260e+00  7.37082368e+00  4.33235360e+00 -3.03983774e+00]
     [ 2.00000000e+00  1.13192702e+01  7.09324174e-01  1.47173664e+00
       4.50000000e+00 -4.72939344e+00  1.05917856e+00 -2.59151517e+00]
     [-6.98382951e-01 -3.53858939e+00  2.40510445e+00  2.51727029e+00
       6.23940851e-01 -2.58719990e+00  9.24032709e-02  2.61686521e+00]
     [-9.91295736e+00  6.33264357e+00  4.46966991e+00 -8.72645131e+00
       7.93621519e+00 -1.19223780e+00 -6.05850468e+00  7.56086146e+00]
     [ 8.35673614e+00 -1.69674346e+00 -3.29987112e+00  1.10963002e+00
       2.73034464e+00  3.83677819e+00 -1.85659463e+00 -1.18496608e+00]]
    
    Reconstructed from IDCT:
     [[ 16.  11.  10.  16.  24.  40.  51.  61.]
     [ 12.  12.  14.  19.  26.  58.  60.  55.]
     [ 14.  13.  16.  24.  40.  57.  69.  56.]
     [ 14.  17.  22.  29.  51.  87.  80.  62.]
     [ 18.  22.  37.  56.  68. 109. 103.  77.]
     [ 24.  35.  55.  64.  81. 104. 113.  92.]
     [ 49.  64.  78.  87. 103. 121. 120. 101.]
     [ 72.  92.  95.  98. 112. 100. 103.  99.]]
    

     

    728x90
    반응형