The Wayback Machine - https://web.archive.org/web/20240605042056/https://www.geeksforgeeks.org/how-to-find-the-fourier-transform-of-an-image-using-opencv-python/
Open In App

How to find the Fourier Transform of an image using OpenCV Python?

Last Updated : 03 Jan, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

The Fourier Transform is a mathematical tool used to decompose a signal into its frequency components. In the case of image processing, the Fourier Transform can be used to analyze the frequency content of an image, which can be useful for tasks such as image filtering and feature extraction.

In this article, we will discuss how to find the Fourier Transform of an image using the OpenCV Python library. We will begin by explaining the basics of the Fourier Transform and its application in image processing, then we will move on to the steps involved in finding the Fourier Transform of an image using OpenCV.

Basics of the Fourier Transform

The Fourier Transform decomposes a signal into its frequency components by representing it as a sum of sinusoidal functions. For a signal represented as a function of time, t, the Fourier Transform is given by the following equation:

f(t) = \int_{-\infty}^{\infty} F(x) e^{-j 2 \pi f x} dx \\ F(x) = \int_{-\infty}^{\infty} f(t) e^{-j 2 \pi f t} dt

Where F(x)        is the Fourier Transform of the signal f(t), and f is the frequency in Hertz (Hz). The Fourier Transform can be thought of as a representation of the signal in the frequency domain, rather than the time domain.

In the case of image processing, the Fourier Transform can be used to analyze the frequency content of an image. This can be useful for tasks such as image filtering, where we want to remove certain frequency components from the image, or feature extraction, where we want to identify certain frequency patterns in the image.

Steps to find the Fourier Transform of an image using OpenCV

Step 1: Load the image using the cv2.imread() function. This function takes in the path to the image file as an argument and returns the image as a NumPy array.

Step 2: Convert the image to grayscale using the cv2.cvtColor() function. This is optional, but it is generally easier to work with grayscale images when performing image processing tasks.

Step 3: Use the cv2.dft() function to compute the discrete Fourier Transform of the image. This function takes in the image as an argument and returns the Fourier Transform as a NumPy array.

Step 4: Shift the zero-frequency component of the Fourier Transform to the center of the array using the numpy.fft.fftshift() function. This step is necessary because the cv2.dft() function returns the Fourier Transform with the zero-frequency component at the top-left corner of the array.

Step 5: Compute the magnitude of the Fourier Transform using the numpy.abs() function. This step is optional, but it is generally easier to visualize the frequency content of an image by looking at the magnitude of the Fourier Transform rather than the complex values.

Step 6: Scale the magnitude of the Fourier Transform using the cv2.normalize() function. This step is also optional, but it can be useful for improving the contrast of the resulting image.

Step 7: Use the cv2.imshow() function to display the magnitude of the Fourier Transform.

Example 1

Here is the complete example of finding the Fourier Transform of an image using OpenCV:

Input Image :

Fourier Transform -Geeksforgeeks

Input Image

Python3

import cv2
import numpy as np
 
# now we will be loading the image and converting it to grayscale
image = cv2.imread(r"Dhoni-dive_165121_730x419-m.jpg")
 
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
 
# Compute the discrete Fourier Transform of the image
fourier = cv2.dft(np.float32(gray), flags=cv2.DFT_COMPLEX_OUTPUT)
 
# Shift the zero-frequency component to the center of the spectrum
fourier_shift = np.fft.fftshift(fourier)
 
# calculate the magnitude of the Fourier Transform
magnitude = 20*np.log(cv2.magnitude(fourier_shift[:,:,0],fourier_shift[:,:,1]))
 
# Scale the magnitude for display
magnitude = cv2.normalize(magnitude, None, 0, 255, cv2.NORM_MINMAX, cv2.CV_8UC1)
 
# Display the magnitude of the Fourier Transform
cv2.imshow('Fourier Transform', magnitude)
cv2.waitKey(0)
cv2.destroyAllWindows()

                    

Output image:

fourier transform - Geeksforgeeks

Fourier transform


In this example, we first load the image and convert it to grayscale using the cv2.imread() and cv2.cvtColor() functions. Then, we compute the discrete Fourier Transform of the image using the cv2.dft() function and store the result in the ‘fourier’ variable.

Example 2

In this program, we find the discrete Fourier transform of the input image. We find and plot the magnitude spectrum.

Input Image : 

Fourier Transform -Geeksforgeeks

Input Image

Python3

import numpy as np
import cv2
from matplotlib import pyplot as plt
 
# read the input image
# you can specify the path to image
image_path = r"Dhoni-dive_165121_730x419-m.jpg"
image = cv2.imread(image_path, 0)
 
# calculating the discrete Fourier transform
DFT = cv2.dft(np.float32(image), flags=cv2.DFT_COMPLEX_OUTPUT)
 
# reposition the zero-frequency component to the spectrum's middle
shift = np.fft.fftshift(DFT)
row, col = image.shape
center_row, center_col = row // 2, col // 2
 
# create a mask with a centered square of 1s
mask = np.zeros((row, col, 2), np.uint8)
mask[center_row - 30:center_row + 30, center_col - 30:center_col + 30] = 1
 
# put the mask and inverse DFT in place.
fft_shift = shift * mask
fft_ifft_shift = np.fft.ifftshift(fft_shift)
imageThen = cv2.idft(fft_ifft_shift)
 
# calculate the magnitude of the inverse DFT
imageThen = cv2.magnitude(imageThen[:,:,0], imageThen[:,:,1])
 
# visualize the original image and the magnitude spectrum
plt.figure(figsize=(10,10))
plt.subplot(121), plt.imshow(image, cmap='gray')
plt.title('Input Image'), plt.xticks([]), plt.yticks([])
plt.subplot(122), plt.imshow(imageThen, cmap='gray')
plt.title('Magnitude Spectrum'), plt.xticks([]), plt.yticks([])
plt.show()

                    

Output image:

Discrete Fourier transform - Geeksforgeeks

Discrete Fourier transform



Previous Article
Next Article

Similar Reads

How to perform faster convolutions using Fast Fourier Transform(FFT) in Python?
Convolution is one of the most important mathematical operations used in signal processing. This simple mathematical operation pops up in many scientific and industrial applications, from its use in a billion-layer large CNN to simple image denoising. Convolution can be both analog and discrete in nature, but because of modern-day computers' digita
3 min read
Top Hat and Black Hat Transform using Python-OpenCV
In morphology and digital image processing, top-hat and black-hat transform are operations that are used to extract small elements and details from given images. These two types of transforms in which, the top-hat transform is defined as the difference between the input image and its opening by some structuring element, while the black-hat transfor
2 min read
Python | Fast Fourier Transformation
It is an algorithm which plays a very important role in the computation of the Discrete Fourier Transform of a sequence. It converts a space or time signal to signal of the frequency domain. The DFT signal is generated by the distribution of value sequences to different frequency component. Working directly to convert on Fourier transform is comput
2 min read
Python | Inverse Fast Fourier Transformation
Inverse Fast Fourier transform (IDFT) is an algorithm to undoes the process of DFT. It is also known as backward Fourier transform. It converts a space or time signal to a signal of the frequency domain. The DFT signal is generated by the distribution of value sequences to different frequency components. Working directly to convert on Fourier trans
2 min read
Convert OpenCV image to PIL image in Python
OpenCV is a huge open-source library for computer vision, machine learning, and image processing. OpenCV supports a wide variety of programming languages like Python, C++, Java, etc. It can process images and videos to identify objects, faces, or even the handwriting of a human. When it is integrated with various libraries, such as Numpy which is a
3 min read
OpenCV - Facial Landmarks and Face Detection using dlib and OpenCV
Content has been removed on Author's request.
1 min read
Find the Solidity and Equivalent Diameter of an Image Object Using OpenCV Python
In this article, we will see how we can find the solidity and the equivalent diameter of an object present in an image with help of Python OpenCV. Function to Find Solidity The solidity of an image is the measurement of the overall concavity of a particle. We can define the solidity of an object as the ratio of the contour area to its convex hull a
4 min read
Find Circles and Ellipses in an Image using OpenCV | Python
To identify circles, ellipses, or in general, any shape in which the pixels are connected we use the SimpleBlobDetector() function of OpenCV. In non-technical terms, a blob is understood as a thick liquid drop. Here, we are going to call all shapes a blob. Our task is to detect and recognize whether the blob is a circle or not. OpenCV provides a co
2 min read
Image resizing using Seam carving using OpenCV in Python
Seam carving is an effective image processing technique with the help of which an image can be resized without removing important elements from the image. The basic approach is to find all the continuous pixels with low energy from left to right or from top to bottom. After the region is selected, it is removed from the original image, leaving only
2 min read
Transition from OpenCV 2 to OpenCV 3.x
OpenCV is one of the most popular and most used Computer vision libraries. It contains tools to carry out image and video processing. When OpenCV 3..4.1 is an improved version of OpenCV 2.4 as it introduced new algorithms and features. Although some of the existing modules were rewritten and moved to sub-modules. In this articles, I will focus on t
2 min read