<>opencv-python Fourier transform

<> one , Image Fourier transform

<> Principle of two-dimensional Fourier transform

​ Gray scale image is composed of two-dimensional discrete points . Two dimensional discrete Fourier transform (Two-Dimensional Discrete Fourier
Transform) Commonly used in image processing , Fourier transform the image to get its spectrum . The frequency level in the spectrum indicates the intensity of gray level change in the image . Edges and noises in images are often high frequency signals , The image background is often low-frequency signal . We can easily operate the high-frequency or low-frequency information of the image in the frequency domain , Complete image denoising , image enhancement , Image edge extraction and other operations .

his in H , W branch other by chart image of high , wide , F ( u , v ) surface show frequency field chart image , f ( x , y ) surface show Hour field chart
image ( some spot of image element ) , u Fan around by [ 0 , H − 1 ] , v Fan around by [ 0 , W − 1 ]
among H,W Height of image , wide ,F(u,v) Represents a frequency domain image ,f(x,y) Represents a time domain image ( Pixels of a point ),u Range is [0,H-1],v Range is [0,W-1] among H,W Figure
Image height , wide ,F(u,v) Represents a frequency domain image ,f(x,y) Represents a time domain image ( Pixels of a point ),u Range is [0,H−1],v Range is [0,W−1]

<> Custom image Fourier transform function

<>1, Custom Fourier transform function
# Custom Fourier transform function def dft(img): # Get image properties H,W,channel=img.shape
# Define frequency domain diagram , It can be seen from the formula that the result is complex , therefore , Need to be defined as a complex matrix F = np.zeros((H, W,channel), dtype=np.
complex) # Prepare the processing index corresponding to the original image position x = np.tile(np.arange(W), (H, 1)) y = np.arange(H).
repeat(W).reshape(H, -1) # Traversal by formula for c in range(channel):# For colored 3 Number of channels to traverse for u in
range(H): for v in range(W): F[u, v, c] = np.sum(img[... c] * np.exp(-2j * np.
pi* (x * u / W + y * v / H))) / np.sqrt(H * W) return F
<>2, Read image , Perform Fourier transform and display the transformed results
gray=cv2_imread("C://Users// Xiaobai No.2 //Desktop//dd.png",1) gray=cv2.resize(gray,(400
,400)) img_dft = dft(gray) dft_shift = np.fft.fftshift(img_dft) fimg = np.log(np
.abs(dft_shift)) cv2.imshow("fimg", fimg) cv2.imshow("gray", gray) cv2.waitKey(0
) cv2.destroyAllWindows()
<>opencv Implementation of Fourier transform with library function
<>(1) Fourier Transform Functions cv2.dft()
The function prototype is as follows :
img=cv2.dft(src, flags=None, nonzeroRows=None)
src Indicates the input image , Need to pass np.float32 Convert format

flag Represents a conversion tag , among cv2.DFT _INVERSE Perform reverse 1-D or 2-D conversion , Instead of the default forward conversion ;cv2.DFT
_SCALE Represents the zoom result , Divided by the number of array elements ;cv2.DFT
_ROWS Performs a forward or reverse transformation of each individual row of the input matrix , This flag can convert multiple vectors at the same time , And can be used to reduce overhead to perform 3D And higher dimension conversion, etc ;cv2.DFT
_COMPLEX_OUTPUT implement 1D or 2D Forward conversion of real group , This is the fastest choice , Default function ;cv2.DFT
_REAL_OUTPUT Perform an inverse transform of a one-dimensional or two-dimensional complex array , The result is usually a complex array of the same size , But if the input array has conjugate complex symmetry , The output is a real array .

nonzeroRows Indicates when the parameter is not zero , Function assumes that only nonzeroRows Enter the first row of the array ( Not set ) Or only the first of the output array ( set up ) Contains non-zero , So the function can process the rest of the rows more efficiently , And save some time ; This technique is used for computing array cross correlation or DFT Convolution is very useful .
<>(2) Complex modular function cv2.magnitude()
Because the output spectrum result is a complex number , Need to call cv2.magnitude() Function to convert the two channel result of Fourier transform to 0 reach 255 Scope of , The function prototype is as follows :
result=cv2.magnitude(x, y)
x Represents floating point X Coordinate value , Real part

y Represents floating point Y Coordinate value , Namely imaginary part

Modulo whose final result is a complex number
<>(3) Application examples def cv2_imread(file_path, flag=1): # Read picture data return cv2.imdecode(np.
fromfile(file_path, dtype=np.uint8), flag) gray=cv2_imread(
"C://Users// Xiaobai No.2 //Desktop// wallpaper 2.png",1) gray=cv2.cvtColor(gray,cv2.COLOR_BGR2GRAY
) gray=cv2.resize(gray,(640,420)) np.float32(gray) img_dft = cv2.dft(np.float32(
gray),flags=cv2.DFT_COMPLEX_OUTPUT) dft_shift = np.fft.fftshift(img_dft) fimg =
20 * np.log(cv2.magnitude(dft_shift[:, :, 1], dft_shift[:, :, 0])) plt.subplot(
121), plt.imshow(gray,'gray'), plt.title(' Original image ') plt.axis('off') plt.subplot(122)
, plt.imshow(np.int8(fimg),'gray'), plt.title(' Fourier transform ') plt.axis('off') plt.show(
) cv2.waitKey(0) cv2.destroyAllWindows()
Running results :

<> two , Inverse Fourier transform of image

<> Principle of two-dimensional inverse Fourier transform

The principle of inverse Fourier transform is to restore the image frequency domain to the image time domain , The specific principle formula is as follows :

his in H , W branch other by chart image of high , wide , F ( u , v ) surface show frequency field chart image , f ( x , y ) surface show Hour field chart
image ( some spot of image element ) , x Fan around by [ 0 , H − 1 ] , y Fan around by [ 0 , W − 1 ]
among H,W Height of image , wide ,F(u,v) Represents a frequency domain image ,f(x,y) Represents a time domain image ( Pixels of a point ),x Range is [0,H-1],y Range is [0,W-1] among H,W Figure
Image height , wide ,F(u,v) Represents a frequency domain image ,f(x,y) Represents a time domain image ( Pixels of a point ),x Range is [0,H−1],y Range is [0,W−1]

<> User defined inverse Fourier transform function

User defined function details are as follows :
def idft(G): H, W, channel = G.shape # Define blank time domain image out = np.zeros((H, W, channel),
dtype=np.float32) # Prepare the processing index corresponding to the original image position x = np.tile(np.arange(W), (H, 1)) y = np.
arange(H).repeat(W).reshape(H, -1) # Traversal by formula for c in range(channel): for u in
range(H): for v in range(W): out[u, v, c] = np.abs(np.sum(G[... c] * np.exp(2j
* np.pi * (x * u / W + y * v / H)))) / np.sqrt(W * H) # Clipping out = np.clip(out, 0,
255) out = out.astype(np.uint8) return out
<>OpenCV Implementation of inverse Fourier transform with library function
<>(1) Inverse Fourier transform function
opencv The function that can realize the inverse Fourier transform is cv2.idft(), The function prototype is as follows :
img=cv2.idft(src,flags,nonzeroRows)
src Indicates the input image , Include real or plural
flags Represents a conversion tag
nonzeroRows Indicates the number of picture lines to process , The contents of the remaining lines are undefined
<>(2) Application examples def cv2_imread(file_path, flag=1): # Read picture data return cv2.imdecode(np.
fromfile(file_path, dtype=np.uint8), flag) gray = cv2_imread(
"C://Users// Xiaobai No.2 //Desktop// wallpaper 2.png", 1) gray = cv2.cvtColor(gray, cv2.
COLOR_BGR2GRAY) gray = cv2.resize(gray, (640, 420)) # Fourier transform img_dft = cv2.dft(np
.float32(gray), flags=cv2.DFT_COMPLEX_OUTPUT) dft_shift = np.fft.fftshift(
img_dft)# Move the frequency domain from the upper left corner to the middle fimg = 20 * np.log(cv2.magnitude(dft_shift[:, :, 0],
dft_shift[:, :, 1])) # Inverse Fourier transform idft_shift = np.fft.ifftshift(dft_shift) #
Move the frequency domain from the middle to the upper left corner ifimg = cv2.idft(idft_shift)# Fourier library function call ifimg = 20*np.log(cv2.
magnitude(ifimg[:, :, 0], ifimg[:, :, 1]))# Convert to 0-255 ifimg=np.abs(ifimg) # Draw picture
plt.subplot(131), plt.imshow(gray, 'gray'), plt.title(' Original image ') plt.axis('off') plt
.subplot(132), plt.imshow(np.int8(fimg), 'gray'), plt.title(' Fourier transform ') plt.axis(
'off') plt.subplot(133), plt.imshow(np.int8(ifimg), 'gray'), plt.title(' Inverse Fourier transform ')
plt.show() cv2.waitKey(0) cv2.destroyAllWindows()
Running results :

Technology
©2019-2020 Toolsou All rights reserved,
【C++ Must see for entry 】C++ from 0 reach 1 Introductory programming axios Interceptor packaging and use Spring Boot Interview must ask : Automatic configuration principle VMware 16 install centos 7 Detailed tutorial C Language data structure - Sequence table delete duplicates V2.0.0 The 12th Blue Bridge Cup c++b Group personal problem solving On sending data from serial port single chip microcomputer to upper computer centos7 install RabbitMqjava Polymorphic array of opencv-python Fourier transform and inverse transform