https://pin.it/2pd7BNC

Basics of Image Processing with OpenCV in Python: Fourier Transformations

Naveed Ul Mustafa
4 min readMar 5, 2023

Fourier Transformation is a mathematical tool used for analyzing signals, including digital images. In image processing, it is primarily used for frequency domain analysis and manipulation of images. This transformation converts a signal from the spatial domain to the frequency domain, which provides a representation of the image’s frequency components. This representation can be used to perform a wide range of image processing operations, including noise reduction, edge detection, and image enhancement.

Before moving forward with Fourier Transformations in Image Processing, it is important to grasp the theory of Spatial Domain and Frequency Domain.

Spatial domain

The spatial domain refers to the image plane, where the pixel values of an image are represented in 2D space. In other words, the spatial domain is the representation of an image in its original form, where the value of each pixel represents the intensity of the corresponding point in the image. The spatial domain of an image can be manipulated using various image processing techniques such as filtering, segmentation, and feature extraction.

Frequency domain

The frequency domain is obtained through mathematical operations like Fourier Transform, which converts an image from the spatial domain to the frequency domain. In the frequency domain, the image is represented as a function of its frequency components. These frequency components can be manipulated to perform operations like filtering, enhancement, and compression. Once the desired operations are performed, the image can be transformed back to the spatial domain using Inverse Fourier Transform.

The main reason Fourier Transformation is used in image processing is that it provides a way to represent an image in terms of its frequency components. The frequency domain representation of an image is often more convenient for analysis and manipulation than the spatial domain representation. By analyzing an image in the frequency domain, we can extract information that is difficult to obtain from the spatial domain representation. For example, the frequency domain representation of an image can reveal information about the texture, edges, and other features that are not apparent in the spatial domain.

The Fourier Transform is commonly used in image processing to remove noise from an image. Noise can be introduced into an image during the image acquisition process, transmission or storage. Fourier transformation provides a way to identify the frequency components of the noise and remove them, while preserving the useful image features.

Another common application of Fourier Transformation in image processing is edge detection. Edges in an image represent significant changes in intensity or color, and they are essential for object recognition and tracking. Fourier Transformation can be used to enhance the edges in an image by filtering out low-frequency components and emphasizing the high-frequency components.

In image enhancement, Fourier Transformation can be used to adjust the contrast of an image. The contrast of an image is the difference between the lightest and darkest parts of the image. Often, images captured from cameras may have low contrast. The Fourier Transform can be used to enhance the contrast of the image by adjusting the frequency components.

Now let’s see how to use Fourier Transformation in image processing with the OpenCV library in Python. OpenCV provides several functions for performing Fourier Transformation on images, including cv2.dft() and cv2.idft(). The former function performs the forward Fourier Transform, and the latter function performs the inverse Fourier Transform. spectrum using .

import cv2
import numpy as np

# Read the image
img = cv2.imread('image.jpg', cv2.IMREAD_GRAYSCALE)

# Compute the Fourier Transform
dft = cv2.dft(np.float32(img), flags=cv2.DFT_COMPLEX_OUTPUT)
dft_shift = np.fft.fftshift(dft)

# Set a cutoff frequency
rows, cols = img.shape
crow, ccol = rows // 2, cols // 2
mask = np.zeros((rows, cols, 2), np.uint8)
mask[crow - 30:crow + 30, ccol - 30:ccol + 30] = 1

# Apply the filter
dft_shift_filtered = dft_shift * mask

# Compute the inverse Fourier Transform
dft_filtered = np.fft.ifftshift(dft_shift_filtered)
img_back = cv2.idft(dft_filtered)
img_back = cv2.magnitude(img_back[:, :, 0], img_back[:, :, 1])

# Normalize and display the image
img_back = cv2.normalize(img_back, None, 0, 255, cv2.NORM_MINMAX)
cv2.imshow('Image', img_back.astype('uint8'))
cv2.waitKey(0)
cv2.destroyAllWindows()

This Python code performs a frequency domain filtering operation on a grayscale image using the Fourier Transform. In this code, we first read an image and convert it to grayscale. We then compute the Fourier Transform of the image and shift the zero frequency component to the center of the spectrum using np.fft.fftshift(). This is necessary because the Fourier Transform generates a spectrum with low frequencies at the corners and high frequencies at the center.

The next step involves creating a mask with a cutoff frequency using the np.zeros() function from the NumPy library, followed by computing the inverse Fourier Transform. Finally, the output image is normalized to the range 0–255.

--

--

Naveed Ul Mustafa
Naveed Ul Mustafa

Written by Naveed Ul Mustafa

Student, interested in Machine Learning & Gen AI, Computational Neuroscience & Computer Vision

No responses yet