<jupyter_start><jupyter_text>Function idct SynopseInverse Discrete Cossine Transform.- **g = idct(F)**  - **g**:output: image after inverse dct transform (spatial domain).   - **F**:input: input dct image (frequency domain).<jupyter_code>import numpy as np

def idct(F):
    import ea979.src as ia
    F = F.astype(np.float64)
    if len(F.shape) == 1: F = F[:,newaxis]
    (m, n) = F.shape
    if (n == 1):
        A = ia.dctmatrix(m)
        g = np.dot(np.transpose(A), F)
    else:
        A=ia.dctmatrix(m)
        B=ia.dctmatrix(n)
        g = np.dot(np.dot(np.transpose(A), F), B)
    return g<jupyter_output><empty_output><jupyter_text>Examples<jupyter_code>testing = (__name__ == "__main__")

if testing:
    ! jupyter nbconvert --to python idct.ipynb
    import numpy as np
    import sys,os
    %matplotlib inline
    import matplotlib.image as mpimg
    import matplotlib.pyplot as plt
    ea979path = os.path.abspath('../../')
    if ea979path not in sys.path:
        sys.path.append(ea979path)
    import ea979.src as ia<jupyter_output>[NbConvertApp] Converting notebook idct.ipynb to python
[NbConvertApp] Writing 3273 bytes to idct.py<jupyter_text>Example 1<jupyter_code>if testing:
    np.set_printoptions(suppress=True, precision=2)
    f = np.array([[4,3,2,1],[3,3,2,1],[2,2,2,1],[1,1,1,1]])
    print('Matriz original:\n',f)
    F = ia.dct(f)
    print('\nDCT:\n',F)
    g = ia.idct(F)
    print('\nMatriz restaurada:\n',g)<jupyter_output>Matriz original:
 [[4 3 2 1]
 [3 3 2 1]
 [2 2 2 1]
 [1 1 1 1]]

DCT:
 [[ 7.5   2.23 -0.5   0.16]
 [ 2.23  1.71  0.    0.  ]
 [-0.5   0.    0.5  -0.  ]
 [ 0.16 -0.   -0.    0.29]]

Matriz restaurada:
 [[ 4.  3.  2.  1.]
 [ 3.  3.  2.  1.]
 [ 2.  2.  2.  1.]
 [ 1.  1.  1.  1.]]<jupyter_text>Example 2<jupyter_code>if testing:
    f = mpimg.imread('../data/cameraman.tif')
    ia.adshow(f,'Imagem original (f)')
    F = ia.dct(f)
    g = ia.idct(F)
    ia.adshow(g.astype(np.uint8),'Imagem dct/idct (g)')
    
    print('Diferença entre imagem original f e imagem após dct e idct (g):', np.sum(np.sum(abs(f-g))))<jupyter_output><empty_output><jupyter_text>Example 3Image compression using dct<jupyter_code>if testing:
    f = mpimg.imread('../data/cameraman.tif')
    nb = ia.nbshow(2)
    nb.nbshow(ia.normalize(f),'Imagem original (f)')
    F = ia.dct(f)
    nb.nbshow( ia.normalize(np.log(abs(F)+1)),'Visualização da DCT de f')
    F1 = F[:256//2,:256//2]
    nb.nbshow( ia.normalize(np.log(abs(F1)+1)),'Selecionando apenas 1 quarto da imagem')

    F2 = np.zeros(F.shape)
    F2[:256//2,:256//2] = F1
    nb.nbshow( ia.normalize(np.log(abs(F2)+1)),'Visualização da DCT da imagem comprimida')
    g = ia.idct(F2)
    nb.nbshow(ia.normalize(g),'Imagem restaurada após compressão')
    nb.nbshow(ia.normalize(f),'Imagem original (f)')
    
    nb.nbshow()<jupyter_output><empty_output><jupyter_text>Example 4compare with scipy function<jupyter_code>if testing:
    from scipy.fftpack import idct as spidct
    f = mpimg.imread('../data/cameraman.tif')
    F = ia.dct(f)
    g = ia.idct(F)
    gscipy = spidct(spidct(F,norm='ortho',axis=0),norm='ortho',axis=1)
    
    nb = ia.nbshow(3)
    nb.nbshow( ia.normalize(f),'Imagem original')
    nb.nbshow( ia.normalize(g),'Imagem restaurada - scipy')
    nb.nbshow( ia.normalize(gscipy),'Imagem restaurada - toolbox')
    nb.nbshow()
    
    print('Diferença entre as duas funções (scipy e implementada):',np.sum(np.abs(gscipy-g)))

    print('\nTempo de execução função implementada:')
    %%timeit g = ia.idct(f)
    print('Tempo de execução scipy:')
    %%timeit gscipy = spidct(spidct(F,norm='ortho',axis=0),norm='ortho',axis=1)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1712571584401418, 'fraction_numerical': 0.03872375238614671, 'mean_word_length': 3.235565819861432, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '210982', 'license': 'MIT', 'n_tokens_mistral': 1679, 'n_tokens_neox': 1599, 'n_words': 306, 'path': 'src/idct.ipynb', 'repo_name': 'lrittner/ia979', 'size': '3667'}