OpenCV Python – Fourier Transform
Fourier Transform is a powerful technique in image processing that converts an image from the spatial domain to the frequency domain. It helps analyze image frequencies, remove noise, and enhance image features.
In OpenCV Python, Fourier Transform is widely used for filtering, image restoration, and compression.
1. What is Fourier Transform?
Fourier Transform breaks an image into:
- Low frequencies → Smooth areas
- High frequencies → Edges and details
This helps in understanding how image information is distributed.
2. Import OpenCV and NumPy
import cv2
import numpy as np
import matplotlib.pyplot as plt
3. Read Image in Grayscale
img = cv2.imread("image.jpg", 0)
cv2.imshow("Original Image", img)
cv2.waitKey(0)
cv2.destroyAllWindows()
4. Apply Fourier Transform
Convert to frequency domain
dft = cv2.dft(np.float32(img), flags=cv2.DFT_COMPLEX_OUTPUT)
5. Shift Zero Frequency to Center
dft_shift = np.fft.fftshift(dft)
6. Calculate Magnitude Spectrum
magnitude = 20 * np.log(cv2.magnitude(dft_shift[:,:,0], dft_shift[:,:,1]))
7. Display Frequency Spectrum
plt.imshow(magnitude, cmap='gray')
plt.title("Fourier Spectrum")
plt.show()
8. Inverse Fourier Transform
Convert back to spatial domain:
f_ishift = np.fft.ifftshift(dft_shift)
img_back = cv2.idft(f_ishift)
img_back = cv2.magnitude(img_back[:,:,0], img_back[:,:,1])
cv2.imshow("Reconstructed Image", img_back)
cv2.waitKey(0)
cv2.destroyAllWindows()
9. Why Fourier Transform is Important
Fourier Transform is used in:
- Image filtering
- Noise removal
- Image compression
- Medical imaging
- Feature analysis
10. Low Pass & High Pass Filtering
Low Pass Filter (Blurring)
Removes noise and smooths image.
High Pass Filter (Edge enhancement)
Enhances edges and details.
11. Applications of Fourier Transform
- MRI and medical imaging
- Audio and signal processing
- Image restoration
- Pattern recognition
- Computer vision research
12. Common Mistakes
❌ Using color image directly
✔ Solution:
- Convert to grayscale first
❌ Not shifting frequency center
✔ Solution:
np.fft.fftshift()
13. Conclusion
Fourier Transform in OpenCV Python is a powerful technique for analyzing image frequencies. It helps in filtering, enhancement, and advanced image processing tasks.
Once you master Fourier Transform, you can move to advanced topics like frequency filtering and image restoration techniques.


0 Comments