Search code examples
pythonimage-processingsignal-processingdftdct

Apply Discrete Cosine Transform to an Image


I want to apply Discrete Cosine Transform to my image

import os.path
from PIL import Image

if __name__ == '__main__':
    image_counter = 1  
while True:      
    if not os.path.isfile('noise_images/' + str (image_counter) + '.png'):
        break
    print(image_counter)
    noise_image_path = 'noise_images/' + str(image_counter) + '.png'
    noise_image = Image.open(noise_image_path)  
    # Apply DCT to an image.

    image_counter = image_counter + 1

Solution

  • Have a look at this scipy.fftpack.dct

    The following is a sample code of using this:

    import cv2
    from scipy.fftpack import dct
    
    def dct2(block):
        return dct(dct(block.T, norm='ortho').T, norm='ortho')
    
    img = cv2.imread(filename)
    img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    dctImage = dct2(img.astype(float))