How to find the Fourier Transform of an image using OpenCV Python?
In image processing, Fourier Transform is a powerful tool used to decompose an image into its frequency components. This tool is essential in image compression, filtering, and restoration. To find the Fourier Transform of an image, we need to use OpenCV Python. In this article, we will take a look at how to use OpenCV Python to find the Fourier Transform of an image.
Install OpenCV Python
Before we can start to use OpenCV Python, we need to install it. The easiest way to install OpenCV is by using pip. Here is the command to install OpenCV using pip.
!pip install opencv-python
Once installed, we can start to use OpenCV functions in Python.
Reading an image using OpenCV
The first step is to read an image using OpenCV. Here is the sample code to read an image using OpenCV.
import cv2
img = cv2.imread('lena.png', 0)
cv2.imshow('image', img)
cv2.waitKey(0)
cv2.destroyAllWindows()
In the above code, we have used the cv2.imread()
function to read an image with the name lena.png
. We have set the second parameter to 0 to read the image in grayscale. Once we have read the image, we can display it using the cv2.imshow()
function.
Finding the Fourier Transform of an image
To find the Fourier Transform of an image, we need to use the function cv2.dft()
. We can also use cv2.dct()
for discrete cosine transform. Here is the sample code to find the Fourier Transform of an image.
import cv2
import numpy as np
from matplotlib import pyplot as plt
img = cv2.imread('lena.png', 0)
dft = cv2.dft(np.float32(img), flags=cv2.DFT_COMPLEX_OUTPUT)
dft_shift = np.fft.fftshift(dft)
magnitude_spectrum = 20 * np.log(cv2.magnitude(dft_shift[:, :, 0], dft_shift[:, :, 1]))
plt.subplot(121), plt.imshow(img, cmap='gray')
plt.title('Input Image'), plt.xticks([]), plt.yticks([])
plt.subplot(122), plt.imshow(magnitude_spectrum, cmap='gray')
plt.title('Magnitude Spectrum'), plt.xticks([]), plt.yticks([])
plt.show()
In the above code, we have used the cv2.dft()
function to find the Fourier Transform of the lena.png
image. The second parameter for the function is set to cv2.DFT_COMPLEX_OUTPUT
to get the complex output. Once we have found the Fourier Transform, we need to shift the zero-frequency component from the top-left corner to the center of the image using the np.fft.fftshift()
function. Finally, we can find the magnitude spectrum of the image using the cv2.magnitude()
function.
Applying Inverse Fourier Transform
Once we have found the Fourier Transform of an image, we can apply the Inverse Fourier Transform to reconstruct the image. Here is the sample code to apply Inverse Fourier Transform.
import cv2
import numpy as np
from matplotlib import pyplot as plt
img = cv2.imread('lena.png', 0)
dft = cv2.dft(np.float32(img), flags=cv2.DFT_COMPLEX_OUTPUT)
dft_shift = np.fft.fftshift(dft)
rows, cols = img.shape
crow, ccol = int(rows / 2), int(cols / 2)
mask = np.zeros((rows, cols), np.uint8)
mask[crow - 30:crow + 30, ccol - 30:ccol + 30] = 1
fshift = dft_shift * mask
f_ishift = np.fft.ifftshift(fshift)
img_back = cv2.idft(f_ishift)
img_back = cv2.magnitude(img_back[:, :, 0], img_back[:, :, 1])
plt.subplot(131), plt.imshow(img, cmap='gray')
plt.title('Input Image'), plt.xticks([]), plt.yticks([])
plt.subplot(132), plt.imshow(mask, cmap='gray')
plt.title('Mask'), plt.xticks([]), plt.yticks([])
plt.subplot(133), plt.imshow(img_back, cmap='gray')
plt.title('Magnitude Spectrum'), plt.xticks([]), plt.yticks([])
plt.show()
In the above code, we have applied the Inverse Fourier Transform to reconstruct the image. The first step is to create a mask to filter out unwanted frequency components. Once we have created a mask, we need to apply it to the Fourier Transform of the image. To do this, we need to multiply the Fourier Transform with the mask using the *
operator. Oncewe have applied the mask, we need to apply the Inverse Fourier Transform to reconstruct the image using the cv2.idft()
function. Finally, we can find the magnitude of the reconstructed image using the cv2.magnitude()
function.
Conclusion
In this article, we have learned how to find the Fourier Transform of an image using OpenCV Python. We have used the cv2.dft()
function to find the Fourier Transform of an image and the cv2.idft()
function to apply the Inverse Fourier Transform to reconstruct the image. We have also seen how to shift the zero-frequency component from the top-left corner to the center of the image using the np.fft.fftshift()
function. Fourier Transform is a powerful tool in image processing and OpenCV Python provides a simple and effective way to use this tool in our projects.