preface

In the previous blog post, we explained in detail how the Fourier transform works and how to implement it using the Numpy library. But OpenCV actually has functions that implement the Fourier transform directly.

In OpenCV, we use cv2.dft() to implement the Fourier transform and cv2.idft() to implement the inverse Fourier transform. The two functions are defined as follows:

Cv2.dft (original image, transformation identifier)Copy the code

The original image here must be in NP. float32 format. So, we first need to convert the image using the cv2.Float32 () function. The value of the transformation identifier is usually cv2.dft_complex_output, which is used to output a complex array.

After the transformation of cv2.dft() function, we can get the spectral information of the original image. The zero component, like the Numpy library implementation, is not central at this point. Here again we need to use the numpy.fft.fftshift() function to move it to the middle position.

It is important to note that the function cv2.dft() returns two channels, with the first channel being the real part of the result and the second the imaginary part of the result. After using the numpy.fft.fftshift() function, the spectrum image is still only a value consisting of real and imaginary parts. To display it, use another function, cv2.magnitude().

This function is defined as follows:

Cv2. Magnitude (parameters1, the parameter2)
Copy the code

Argument 1: the value of the floating point x coordinate, that is, the real part

Argument 2: the floating point y coordinate, the imaginary part, must have the same size as argument 1.

After obtaining the amplitude of the spectral image, it is also necessary to map the amplitude to the gray space [0,255], so that it can be displayed as a gray image. As in the previous blog post, use 20*np.log(cv2.magnitude()).

Realize the Fourier transform

Next, we will use the above OpenCV function to realize the Fourier transform, and display its spectrum information.

import cv2
import numpy as np
import matplotlib.pyplot as plt

img = cv2.imread("4.jpg".0)

dft = cv2.dft(np.float32(img), flags=cv2.DFT_COMPLEX_OUTPUT)
dftShift = np.fft.fftshift(dft)
result = 20 * np.log(cv2.magnitude(dftShift[:, :, 0], dftShift[:, :, 1]))


plt.subplot(121)
plt.imshow(img, cmap="gray")
plt.axis('off')
plt.subplot(122)
plt.imshow(result, cmap="gray")
plt.axis('off')
plt.show()
Copy the code

After running, the display is the same as the previous blog post.

Realize the inverse Fourier transform

Again, as in the last blog post, here we filter the spectral information of the image, here we filter the low-frequency information.

import cv2
import numpy as np
import matplotlib.pyplot as plt

img = cv2.imread("4.jpg".0)

dft = cv2.dft(np.float32(img), flags=cv2.DFT_COMPLEX_OUTPUT)
dftShift = np.fft.fftshift(dft)
result = 20 * np.log(cv2.magnitude(dftShift[:, :, 0], dftShift[:, :, 1]))

rows,cols=img.shape
rows_half,cols_half=int(rows/2),int(cols/2)
mask=np.zeros((rows,cols,2),dtype=np.uint8)
mask[rows_half-30:rows_half+30,cols_half-30:cols_half+30] =1


Inverse Fourier Transform
fShift=dftShift*mask
ishift=np.fft.ifftshift(fShift)
iimg=cv2.idft(ishift)
iimg=cv2.magnitude(iimg[:,:,0],iimg[:,:,1])


plt.subplot(121)
plt.imshow(img, cmap="gray")
plt.axis('off')
plt.subplot(122)
plt.imshow(iimg, cmap="gray")
plt.axis('off')
plt.show()
Copy the code

After running, the effect is as follows:

It can be seen that after filtering the low frequency information, the edge information of the image is weakened.