(math.) a Fourier transform
Image processing is generally categorized into spatial domain processing and frequency domain processing.
Spatial domain processing is done directly on the pixels within the image.
The spatial domain processing is mainly divided into two forms: gray scale transformation and spatial filtering.
- Gray scale transformation is the processing of individual pixels within an image, such as adjusting contrast and processing thresholds.
- Spatial filtering involves changes in image quality, such as image smoothing. The spatial domain processing is simple and easy to compute and faster to operate.
Frequency domain processing involves first transforming the image to the frequency domain, then processing the image in the frequency domain, and finally transforming the image from the frequency domain to the spatial domain by inverse transformation.
theoretical foundation
The time difference, in the Fourier transform, is the phase. The phase expresses the information associated with the time difference.
In image processing, the Fourier transform is the decomposition of an image into two parts, the sine component and the cosine component, i.e., the image is converted from the spatial domain to the frequency domain.
Digital images are Fourier transformed to obtain frequency domain values that are complex numbers. Therefore, displaying the results of the Fourier transform requires the use of a real image plus a complex image, or a magnitude image plus a phase image.
Because the magnitude image contains most of the information we need in the original image, only the magnitude image is usually used in the image processing process.
If it is desired to first process the image in the frequency domain and then obtain a modified null domain image by inverse Fourier transform, both the magnitude image and the phase image must be preserved.
The Fourier transform of an image yields low and high frequency information in the image.
Low-frequency information corresponds to gray-scale components that change slowly within the image. High-frequency information corresponds to gray-scale components that are changing faster and faster within the image and is caused by sharp transitions in the gray scale.
The purpose of the Fourier transform is to convert the image from the null domain to the frequency domain and to realize the processing of specific objects within the image in the frequency domain, and then the inverse Fourier transform is performed on the processed frequency domain image to obtain the null domain image.
Fourier transform plays a very crucial role in the field of image processing, which can realize image enhancement, image denoising, edge detection, feature extraction, image compression and encryption.
Numpy implementation of the Fourier transform
The fft2() function in the Numpy module implements the Fourier transform of an image.
Realization of the Fourier transform
The function provided by Numpy to implement the Fourier transform is .fft2(), which has the syntax format:
return value = .fft2(original image)
The parameter "original image" is a grayscale image and the return value of the function is a complex ndarray.
After the processing of this function, the spectral information of the image is obtained.
At this point, the zero-frequency component of the image spectrum is located in the upper left corner of the spectral image (frequency domain image)
For ease of observation, it is common to use the () function to move the zero-frequency component to the center of the frequency-domain image.
The syntax format of function() is:
return value=(raw spectrum)
After processing with this function, the zero-frequency component of the image spectrum is shifted to the center of the frequency-domain image, which is very effective for observing the zero-frequency part of the spectrum after Fourier transform.
The Fourier transform of the image yields an array of complex numbers.
In order to be displayed as an image, their values need to be adjusted to the gray space of [0, 255] using the formula:
Pixel new value=20*((spectral value))
The Fourier transform is implemented using Numpy and the resulting spectral image is observed.
import cv2 import numpy as np import as plt img = ('./img/',0) f = .fft2(img) fshift = (f) magnitude_spectrum = 20*((fshift)) (121) (img, cmap = 'gray') ('original') ('off') (122) (magnitude_spectrum, cmap = 'gray') ('result') ('off') ()
Realization of the inverse Fourier transform
Caution. If the () function is used to move the zero frequency component during the Fourier transform process, then during the inverse Fourier transform process, the () function needs to be used to move the zero frequency component to its original position before performing the inverse Fourier transform
The function () is the inverse of () and has the syntax format:
Adjusted spectrum = (raw spectrum)
The .ifft2() function implements the inverse Fourier transform, returning an array of complex numbers in the null domain.
It is the inverse function of .fft2(), which has the syntax format:
return value=.ifft2(Frequency domain data)
The return value of the function .ifft2() is still a complex ndarray.
The information in the null domain obtained by the inverse Fourier transform is an array of complex numbers, which needs to be adjusted into the [0, 255] gray space using the formula:
iimg = (Inverse Fourier transform results)
Implement Fourier Transform, Inverse Fourier Transform within Numpy and observe the resulting image of the Inverse Fourier Transform.
import cv2 import numpy as np import as plt img = ('./img/',0) f = .fft2(img) fshift = (f) ishift = (fshift) iimg = .ifft2(ishift) iimg = (iimg) (121), (img, cmap = 'gray') ('original'), ('off') (122), (iimg, cmap = 'gray') ('iimg'), ('off') ()
High-pass filtering example
Within an image, both high-frequency and low-frequency signals are present.
- Low-frequency signals correspond to gray-scale components that change slowly within an image. For example, in an image of a prairie, the low-frequency signal corresponds to a vast prairie that tends to be uniform in color.
- The high frequency signal corresponds to the gray scale components that are changing faster and faster within the image, caused by sharp transitions in the gray scale. If there is also a lion in the savannah image above, then the high frequency signal corresponds to information such as the edges of the lion.
Filters are capable of allowing or rejecting the passage of components of a certain frequency, and can be categorized into low-pass and high-pass filters according to their mode of action.
- A filter that allows low-frequency signals to pass through is called a low-pass filter. A low-pass filter attenuates high-frequency signals and lets go of low-frequency signals, blurring the image.
- A filter that allows high frequency signals to pass through is called a high pass filter. A high-pass filter attenuates low-frequency signals and allows high-frequency signals to pass through, which will enhance sharp details in the image, but will result in a reduction in the contrast of the image.
The Fourier transform separates the high frequency signals from the low frequency signals of an image.
Operations such as image enhancement, image denoising, edge detection, feature extraction, compression and encryption can be realized through frequency domain processing of images.
The image is Fourier transformed within Numpy to get its frequency domain image. Then, high pass filtering is achieved by processing the values of low frequency components to 0 in the frequency domain. Finally, the inverse Fourier transform is performed on the image to get the recovered original image.
import cv2 import numpy as np import as plt img = ('./img/',0) f = .fft2(img) fshift = (f) rows, cols = crow, ccol = int(rows/2) , int(cols/2) fshift[crow-30:crow+30, ccol-30:ccol+30] = 0 ishift = (fshift) iimg = .ifft2(ishift) iimg = (iimg) (121), (img, cmap = 'gray') ('original'), ('off') (122), (iimg, cmap = 'gray') ('iimg'), ('off') ()
OpenCV Implementation of the Fourier Transform
OpenCV provides functions () and () to implement the Fourier transform and the inverse Fourier transform
Realization of the Fourier transform
The syntax format of the function () is:
Return results=(original image,conversion mark)
When using this function, you need to pay attention to the specification of the parameters:
- For the parameter "original image", first use the np.float32() function to convert the image to np.float32 format.
- The value of "conversion identifier" is usually "cv2.DFT_COMPLEX_OUTPUT", which is used to output an array of complex numbers.
The result returned by function() is the same as that obtained by performing a Fourier transform using Numpy, but it returns the value ofdual, the 1st channel is the real part of the result and the 2nd channel is the imaginary part of the result.
After the transformation of the function (), the spectral information of the original image is obtained.
At this time, the zero-frequency component is not in the center position, in order to deal with the convenience of the need to move it to the center position, you can use the function () to achieve.
For example, the following statement shifts the zero-frequency component in the spectral image dft to the center of the spectrum, yielding the spectral image dftshift with the zero-frequency component at the center.
dftShift = (dft)
After the above processing, the spectrum image is still just a value consisting of real and imaginary parts. To display it, further processing is required before it can be displayed.
The function () calculates the magnitude of the spectral information. The syntactic format of the function is:
return value=(parameters1,parameters2)
- Parameter 1: Floating-point x-coordinate value, which is the real part.
- Parameter 2: floating-point y-coordinate value, i.e. the imaginary part, which must have the same size as parameter 1.
The return value of the function () is the square root of the sum of the squares of argument 1 and argument 2 with the formula:
After obtaining the magnitude of the spectral information, it is usually necessary to do further conversion of the magnitude values in order to display the spectral information in the form of an image. In short, it is necessary to map the magnitude value into the gray space [0, 255] of a gray-scale image so that it can be displayed as a gray-scale image.
The formula used here is:
result = 20*((Ministry of Foreign Affairs of the *,imaginary part of the body))
import numpy as np import cv2 img = ('./img/',0) dft = (np.float32(img), flags = cv2.DFT_COMPLEX_OUTPUT) print(dft) dftShift = (dft) print(dftShift) result = 20*((dftShift[:, :,0], dftShift[:, :,1])) #Two parameters,Need to split the channelprint(result)
Fourier transform the image with OpenCV functions and show its spectral information.
import numpy as np import cv2 import as plt img = ('./img/',0) dft = (np.float32(img), flags = cv2.DFT_COMPLEX_OUTPUT) dftShift = (dft) result = 20*((dftShift[:, :,0], dftShift[:, :,1])) (121), (img, cmap = 'gray') ('original'), ('off') (122), (result, cmap = 'gray') ('result'), ('off') ()
Realization of the inverse Fourier transform
In OpenCV, the inverse Fourier transform is implemented using the function (), which is the inverse of the Fourier transform function (). Its syntax format is:
Return results=(raw data)
After performing a Fourier transform on an image, the zero frequency component is usually moved to the center of the spectral image. If the zero frequency component is shifted using function(), then use function() to restore the zero frequency component to its original position before performing the inverse Fourier transform.
Caution. After performing the inverse Fourier transform, the resulting value is still complex and the function () needs to be used to calculate its magnitude.
Perform Fourier Transform, Inverse Fourier Transform on the image using OpenCV functions and show the original image and the image obtained after Inverse Fourier Transform.
import numpy as np import cv2 import as plt img = ('./img/',0) dft = (np.float32(img), flags = cv2.DFT_COMPLEX_OUTPUT) dftShift = (dft) ishift = (dftShift) iImg = (ishift) iImg= (iImg[:, :,0], iImg[:, :,1]) # Calculated range (121), (img, cmap = 'gray') ('original'), ('off') (122), (iImg, cmap = 'gray') ('inverse'), ('off') ()
Example of low-pass filtering
Within an image, low-frequency signals correspond to gray-scale components that change slowly within the image. The image becomes blurred when it is low-pass filtered.
Intermediate steps towards realization
rows, cols = crow, ccol = int(rows/2) , int(cols/2) mask = ((rows, cols,2), np.uint8) # Two-dimensional cause there's a real part and an imaginary part # mask[crow-30:crow+30, ccol-30:ccol+30,:] = 1
Then, it is operated with the spectral image to realize the low-pass filtering. The form of operation used here is:
fShift = dftShift*mask
The image is Fourier transformed using the function () to obtain its spectral image. Then, low-pass filtering is achieved by processing the values of its high-frequency components to zero in the frequency domain. Finally, the inverse Fourier transform is performed on the image to obtain the recovered original image.
import numpy as np import cv2 import as plt img = ('./img/',0) dft = (np.float32(img), flags = cv2.DFT_COMPLEX_OUTPUT) dftShift = (dft) rows, cols = crow, ccol = int(rows/2) , int(cols/2) mask = ((rows, cols,2), np.uint8) # Two channels, matched to frequency domain image mask[crow-30:crow+30, ccol-30:ccol+30,:] = 1 fShift = dftShift*mask ishift = (fShift) iImg = (ishift) iImg= (iImg[:, :,0], iImg[:, :,1]) (121), (img, cmap = 'gray') ('original'), ('off') (122), (iImg, cmap = 'gray') ('inverse'), ('off') ()
After low-pass filtering, the edge information of the image is weakened.
Time domain convolution --> frequency domain product
This article on the realization of the opencv Fourier transform is introduced to this article, more related opencv Fourier transform content, please search for my previous articles or continue to browse the following related articles I hope you will support me more in the future!