Digital Image Processing: Fourier Transform

Jeffrey
NTUST-AIVC
Published in
6 min readJul 17, 2022

The Fourier transform that an imaging engineer must know.

Prolegomenon

The meaning represented by the Fourier transform is: “Any periodic wave can be divided into many sine waves, and the meaning of the Fourier transform is to find the sine waves of each frequency”, and the same is true for images, By converting the image into sine waves of various frequencies, it can be converted or operated on, such as filtering out unwanted information through a filter. This is the practical application of the Fourier transform of the image, and it is also a must for image engineers. the reason for the meeting.

Principle

Here is just a little mention of the formula part, if you just want to know how to use it or feel that Fourier transform is too cumbersome, you can skip to the next part!

Definition of one-dimensional continuous Fourier transform

This is the most original definition of Fourier transform, which is used to transform continuous signals, but the picture is not a continuous analog signal, but a discrete digital signal, so we use the following discrete Fourier transform to transform the picture.

Definition of one-dimensional discrete Fourier transform

This is a one-dimensional Fourier transform, but the image we want to convert is two-dimensional, so a two-dimensional Fourier transform should be used, but if you directly write a two-dimensional Fourier transform, the amount of calculation will be too large, so in practice, we will perform a Fourier transform on the data vertically and horizontally to achieve the effect of a two-dimensional Fourier transform. Similarly, the inverse Fourier transform can also be achieved in the same way. Therefore, the formula for the two-dimensional conversion will not be repeated here.

The left is the original image, and the right is the frequency distribution map after Fourier transformation

Implement

Fourier Transform

There are already ready-made fast Fourier transform functions available in the opencv and numpy suites in python, and the result of the transformation is a complex np.ndarray. The following are the various Fourier transform functions provided by numpy:

#One-dimensional Fourier transform
numpy.fft.fft(src, n=None, axis=-1, norm=None)

#Two-dimensional Fourier transform
numpy.fft.fft2(src, n=None, axis=-1, norm=None)

#n-dimensional Fourier transform
numpy.fft.fftn()

src represents the input image, a numpy array, and only this parameter can be input.
n represents a sequence of integers that can determine the size of the output array, where n[0] represents axis 0 and n[1] represents axis 1. If the given shape is smaller than the input shape, the output will be clipped. If it is greater than it will be filled with zeros.
axes represents a sequence of integers, optional axes for computing the FFT. Repeated indices in “axes” mean that multiple transformations are performed for that axis, and a sequence of elements means that a 1D FFT is performed.
norm includes two options, None and ortho, normalization mode, and the default value is None.
See
the official website for more details.

Opencv also provides the following fast Fourier transform functions. Note that the input image must be converted to np.float32 format first. And it is the same result with numpy API, but it is two-channel, the first channel is the real part of the result, and the second channel is the imaginary part of the result.

dst = cv2.dft(src, dst=None, flags=None, nonzeroRows=None)

src represents the input image, which needs to be in np.float32 format. Can only input this parameter.
dst represents the output image, including output size and dimensions.
flags represents the transition flags, where:
DFT_INVERSE performs the reverse transformation instead of the preset forward transformation;
DFT_SCALE represents the scaling result, divided by the number of array elements;
DFT_ROWS This flag can transform multiple vectors in the forward or reverse direction at the same time, and can be used to reduce overhead when performing higher-dimensional transformations;
DFT_COMPLEX_OUTPUT performs forward conversion, the fastest choice, the default is this;
DFT_REAL_OUTPUT performs the inverse transform of a complex array, the result is usually a complex array of the same size, and if the input array has conjugate complex symmetry, the output is a real array.
nonzeroRows means that this function can handle and make it more efficient when when some specific argument is not zero.
See
the official website for more details.

Show Conversion Result

Fourier theory assumes the Fourier spectrum is periodic and is an infinite image repeating itself infinitely in both directions. It is usually shifted to show the result of the Fourier transform. The shift is to move the low-frequency information to the center point, so we can more easily to described the Fourier spectrum.

https://www.cs.auckland.ac.nz/courses/compsci773s1c/lectures/ImageProcessing-html/topic1.htm
https://staff-old.najah.edu/sites/default/files/Chapter4_Frequency_Enhancement.pdf

Using the following function to transfer to the middle position:

np.fft.fftshift(fft_img)

In addition, because the result after Fourier transformation is a complex number, the absolute value function of numpy is also used to calculate the amplitude of the complex number, and some numerical adjustments are usually made.

10 * np.log(np.abs(fft_shift) + 1)

fft_shift is the result after shifting.
np.abs() is to take vibration.
+1 to avoid errors when log reaches log 0.
np.log() is numpy’s log function.
Taking the log multiplied by 10 is to adjust the value between 0 and 255. I suggest that the previous 10 can be adjusted to 15 or 20 depending on the difference in value.

Opencv also provides another function to take vibration.

cv2.magnitude(x, y, magnitude=None)

x represents the floating-point X coordinate value, that is, the real part.
y represents the floating-point Y coordinate value, that is, the imaginary part.

Inverse Fourier Transform

When you want to convert the transformed or processed result back to the original image, numpy and opencv also provide related functions. The result can be obtained through a similar process but the process is reversed from that of the positive conversion.

np.fft.ifftshift(fft_shift)# Inverse Fourier Transform
cv2.idft(fft_ishift)
# or
np.fft.ifft2(fft_ishift)

In the end, the sample program:

import cv2
import numpy as np

path = 'Lenna.jpg'
img = cv2.imread(path, 0)
cv2.imshow("orgin:", img)
f = np.fft.fft2(img)
fs = np.fft.fftshift(f)
lfs = 15 * np.log(1 + np.abs(fs))
cv2.imshow("dft:", np.uint8(lfs))
ishift_img = np.fft.ifftshift(fs)
ifft_img = np.fft.ifft2(ishift_img)
abs_img = np.abs(ifft_img)
cv2.imshow("idft:",np.uint8(abs_img))
cv2.waitKey(0)
Results

Application

After learning how to use Fourier transform, the next step is to enter the most important part, using Fourier transform to make various filters. The following is an example of the most simple and easy-to-understand high and low pass filters.

Low Pass Filter

After we get the Fourier spectrum, it represents the sine waves that build the image. The center of the image is low frequency, and the surrounding area is high frequency. Knowns that implementing a low-pass filter is to mask the surrounding image.

low pass filter

The result after the inverse transformation is as follows:

High Pass Filter

Contrary to the low-pass filter, the image in the center after Fourier can be masked.

For example, image denoising, image enhancement and sharpening, etc., also use similar principles as the basic processing of image processing, which shows the importance of Fourier Transform to image processing.

Done!

Now you know how to use OpenCV to deal with the image, so just go try to do it by yourself. Thanks for your watching, If you like my article don’t forget to leave your clap or share it with your friend. Your appreciation is my motivation. — Jeffrey

Co-Author: Y. S. Huang, a master’s student studying AIVC, likes open-source.
If you are interested, go to check my Github!

--

--