In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces how to achieve the Fourier transform of the image based on Python, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let the editor take you to understand it.
Preface
The first is the overall code of this article, which can be run by changing the reading path of the image, but I suggest you first look at the following steps and type the code line by line, so that the effect is better:
"Author:XiaoMadate:2021/11/7" import cv2import matplotlib.pyplot as pltimport numpy as np # reads image information from numpy.fft import ifftshift img0 = cv2.imread ("E:\ From Zhihu\ For the desk\ cvthirteen2.jpg") img1 = cv2.resize (img0, dsize = None, fx = 0.5, fy = 0.5) img2 = cv2.cvtColor (img1, cv2.COLOR_BGR2GRAY) # converted to grayscale image h, w = img1.shape [: 2] print (h W) cv2.namedWindow ("W0") cv2.imshow ("W0") Img2) cv2.waitKey (delay = 0) # convert the image into the frequency domain and draw the spectrum # # numpy implement plt.rcParams ['font.family'] =' SimHei' # change the global Chinese font to boldface f = np.fft.fft2 (img2) fshift = np.fft.fftshift (f) # move the 0 frequency component to the center of the image magnitude_spectrum0 = 20*np.log (np.abs (fshift)) # Fourier inverse Transform # Numpy to realize ifshift = np.fft.ifftshift (fshift) # convert complex numbers to floating-point numbers for Fourier spectrum display ifimg = np.log (np.abs (ifshift)) if_img = np.fft.ifft2 (ifshift) origin_img = np.abs (if_img) imggroup = [img2 Magnitude_spectrum0, ifimg, origin_img] titles0 = ['original image', 'spectrum image after moving', 'spectrum image obtained by inverse transform', 'original image obtained by inverse transform'] for i in range (4): plt.subplot (2,2) I + 1) plt.xticks ([]) # remove the tick mark plt.yticks ([]) plt.title (titles0 [I]) plt.imshow (imggroup [I], cmap = 'gray') plt.show () # # OpenCV implement dft = cv2.dft (np.float32 (img2)) Flags = cv2.DFT_COMPLEX_OUTPUT) dft_shift = np.fft.fftshift (dft) magnitude_spectrum1 = 20*np.log (cv2.magnitude (dft_shift [:,:, 0], dft_shift [:,: 1]) plt.subplot (121), plt.imshow (img2, cmap = 'gray') plt.title (' original'), plt.xticks ([]), plt.yticks ([]) plt.subplot (122), plt.imshow (magnitude_spectrum1) Cmap = 'gray') plt.title (' spectrum'), plt.xticks ([]), plt.yticks ([]) plt.show () (1) basic concepts
In general, we observe the signal directly in the time domain (sound signal) or space (image) to analyze it, although this is in line with common sense, but some useful conditions in the signal will not be considered by us, so we can not achieve the effect of analysis, so we have to transform the signal to some other transform domains for analysis.
(2) read image information
Classic actions in this series of articles:
"Author:XiaoMadate:2021/11/7" import cv2import matplotlib.pyplot as pltimport numpy as np # reads image information img0 = cv2.imread ("E:\ From Zhihu\ For the desk\ cvthirteen2.jpg") img1 = cv2.resize (img0, dsize = None, fx = 0.5, fy = 0.5) img2 = cv2.cvtColor (img1, cv2.COLOR_BGR2GRAY) # converted to grayscale image h, w = img1.shape [: 2] print (h W) cv2.namedWindow ("W0") cv2.imshow ("W0", img1) cv2.waitKey (delay = 0)
The image information is as follows:
540 960
1. Fourier transform
Code reference: OpenCV official website
(1) basic concepts
When we describe a sound, we will not only talk about its volume, but also whether its frequency is high or low, so how do we understand the concept of frequency? In the past, when we learned trigonometric functions, we were told that each sine signal has its fixed frequency, which is the reciprocal of its period. So what is the frequency domain? We have also come into contact with waveforms of other shapes, such as square waves, triangular waves, and so on, and these waves of different shapes are composed of sine waves with different frequencies. If we arrange those sine waves with different frequencies according to their frequency, we get a frequency axis (this is one-dimensional). Then we give the amplitude values of each frequency to them (just like the xoy plane) to get a two-dimensional plane, which is the frequency domain. Fourier transform is a tool to transform signals from time domain to frequency domain. For the understanding of Fourier transform, please refer to the following picture:
Of course, if you want to learn more about Fourier transform, you can search according to the watermark on the picture. He spoke very clearly.
(2) numpy implementation # convert the image into the frequency domain and draw the spectrum plt.rcParams ['font.family'] =' SimHei' # change the global Chinese font to bold f = np.fft.fft2 (img2) fshift = np.fft.fftshift (f) # move the 0 frequency component to the center magnitude_spectrum = 20*np.log (np.abs (fshift)) plt.xticks ([]) # remove the scale line plt.yticks ([]) plt.title ("spectrum") plt.imshow (magnitude_spectrum) Cmap = 'gray') plt.show ()
(3) OpevCV implementation # OpenCV implementation dft = cv2.dft (np.float32 (img2), flags = cv2.DFT_COMPLEX_OUTPUT) dft_shift = np.fft.fftshift (dft) magnitude_spectrum1 = 20*np.log (cv2.magnitude (dft_shift [:,:, 0], dft_shift [:,: 1]) plt.subplot (121), plt.imshow (img2, cmap = 'gray') plt.title (' original'), plt.xticks ([]) Plt.yticks ([]) plt.subplot (122), plt.imshow (magnitude_spectrum1, cmap = 'gray') plt.title (' spectrum'), plt.xticks ([]), plt.yticks ([]) plt.show ()
two。 Inverse Fourier transform (1) basic Concepts
As mentioned earlier, after Fourier transform, the image can be transformed into the frequency domain, then after the inverse Fourier transform, the image can certainly be transformed from the frequency domain to the time domain, so the inverse Fourier transform is a tool to transform the signal from the frequency domain to the time domain.
(2) Code implementation
The code here is followed by the Fourier transform using Numpy above.
# inverse Fourier transform # Numpy implementation ifshift = np.fft.ifftshift (fshift) # convert complex numbers to floating-point numbers for Fourier spectrum ifimg = np.log (np.abs (ifshift)) if_img = np.fft.ifft2 (ifshift) origin_img = np.abs (if_img) imggroup = [img2, magnitude_spectrum0, ifimg, origin_img] titles0 = ['original image', 'moved spectrum', 'spectrum obtained by inverse transform' For i in range (4): plt.subplot (2,2, I + 1) plt.xticks ([]) # remove the tick line plt.yticks ([]) plt.title (titles0 [I]) plt.imshow (imggroup [I], cmap = 'gray') plt.show ()
Thank you for reading this article carefully. I hope the article "how to realize Fourier transform of Image based on Python" shared by the editor will be helpful to everyone. At the same time, I also hope that you will support and pay attention to the industry information channel. More related knowledge is waiting for you to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.