How to find discrete cosine transform of an image using OpenCV Python?

The discrete cosine transform (DCT) is a mathematical technique used in image processing for frequency domain analysis and compression. OpenCV provides cv2.dct() to compute DCT of an image and cv2.idct() to apply inverse DCT.

Syntax

cv2.dct(src, flags)
cv2.idct(src, flags)

Parameters

  • src ? Input image as float32 array

  • flags ? Transformation flags (cv2.DCT_INVERSE or cv2.DCT_ROWS)

Steps to Apply DCT

To find discrete cosine transform of an input image, follow these steps ?

  • Import the required libraries OpenCV and NumPy

  • Read the input image using cv2.imread() and convert to grayscale

  • Convert the grayscale image to np.float32 data type

  • Apply cv2.dct() with appropriate flags

  • Use cv2.idct() to convert back to original domain

Example 1: Using DCT_INVERSE Flag

In this example, we apply DCT with cv2.DCT_INVERSE flag ?

import cv2
import numpy as np

# Create a simple test image
img = np.random.randint(0, 256, (100, 100), dtype=np.uint8)

# Convert to float32
imf = np.float32(img)

# Apply discrete cosine transform
dst = cv2.dct(imf, cv2.DCT_INVERSE)

# Apply inverse discrete cosine transform
img_back = cv2.idct(dst)

# Convert back to uint8
img_back = np.uint8(img_back)

print("Original image shape:", img.shape)
print("DCT result shape:", dst.shape)
print("DCT max value:", np.max(dst))
print("DCT min value:", np.min(dst))

Example 2: Using DCT_ROWS Flag

The cv2.DCT_ROWS flag applies DCT to each row independently ?

import cv2
import numpy as np

# Create a test image with patterns
img = np.zeros((64, 64), dtype=np.uint8)
img[:32, :] = 255  # Top half white, bottom half black

# Convert to float32
imf = np.float32(img)

# Apply DCT with DCT_ROWS flag
dct_result = cv2.dct(imf, cv2.DCT_ROWS)

# Apply inverse DCT
img_restored = cv2.idct(dct_result)
img_restored = np.uint8(np.clip(img_restored, 0, 255))

print("Original image mean:", np.mean(img))
print("DCT result mean:", np.mean(dct_result))
print("Restored image mean:", np.mean(img_restored))

Understanding DCT Flags

Flag Description Use Case
cv2.DCT_INVERSE Applies inverse DCT Standard DCT computation
cv2.DCT_ROWS Applies DCT to each row Row-wise frequency analysis

Complete Example with Visualization

Here's a complete example showing DCT transformation and reconstruction ?

import cv2
import numpy as np
import matplotlib.pyplot as plt

# Create a sample image with checkerboard pattern
img = np.zeros((64, 64), dtype=np.uint8)
for i in range(0, 64, 8):
    for j in range(0, 64, 8):
        if (i//8 + j//8) % 2 == 0:
            img[i:i+8, j:j+8] = 255

# Convert to float32
imf = np.float32(img)

# Apply DCT
dct_result = cv2.dct(imf)

# Zero out high frequency components (compression simulation)
dct_compressed = dct_result.copy()
dct_compressed[32:, :] = 0
dct_compressed[:, 32:] = 0

# Reconstruct image
img_reconstructed = cv2.idct(dct_compressed)
img_reconstructed = np.uint8(np.clip(img_reconstructed, 0, 255))

print("Original image size:", img.shape)
print("Compression ratio: 75% (kept only low frequencies)")
print("PSNR:", cv2.PSNR(img, img_reconstructed))

Conclusion

DCT is fundamental for image compression and frequency analysis. Use cv2.dct() for forward transformation and cv2.idct() for reconstruction, ensuring proper float32 conversion for accurate results.

Updated on: 2026-03-26T22:57:47+05:30

7K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements