I have a grayscale image, available at https://pasteboard.co/IdX7NfA.png. Now I want to low-pass filter this image in the Discrete Cosine Transform (DCT) spectrum.

Specifically, I centered the image so that it has both positive and negative values, DCT'ed that centered image, set to zero the coefficients for components with higher frequency than (5, 5) (DC component being (0, 0)), and finally inverse DCT'ed it back to an image.

Here's how I did it in Python (assuming you download that source image to ~/gs.png):

import cv2
from scipy.fftpack import dct, idct

# Read and zero-mean the image
im = cv2.imread('~/gs.png', cv2.IMREAD_GRAYSCALE)
offset = np.iinfo(im.dtype).max / 2
im = im.astype(float) - offset

coeffs = dct(dct(im.T, type=2, norm='ortho').T, type=2, norm='ortho')

# Discard high-frequency components
coeffs[6:, 6:] = 0

# Inverse DCT
recon = idct(idct(coeffs, type=2, norm='ortho').T, type=2, norm='ortho').T

# Add back the mean
recon += offset

To my surprise, the low-pass filtered image recon has many blocky, high-frequency structures. It looks like this:

enter image description here

What am I doing wrong? If nothing, why keeping only the first few DCT bases gives me so many high-frequency components?


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.