Example of Fourier transform of image by Matlab

1. The purpose of the experiment

1. Understand the meaning and means of image transformation;
2. Be familiar with the basic properties of Fourier transform;
3. Be familiar with the method and application of FFT;
4. Understand the distribution characteristics of two-dimensional spectrum through experiments;
5. Master the utilization through this experiment MATLAB programming to realize the Fourier transform of digital images.

2. Experimental Instruments

1. Computer;
2. MATLAB program;
3. Mobile memory (floppy disk, U disk, etc.);
4. Pen and paper for recording.

3. Experimental principle

1. Image processing using Fourier transform
Fourier transform is a powerful tool for linear system analysis. It can quantitatively analyze the effects of digital systems, sampling points, electronic amplifiers, convolution filters, noise, and display points. . Cultivating this skill through experiments will help solve most image processing problems. For anyone who wants to effectively apply digital image technology in their work, it is necessary to spend time on learning and mastering the Fourier transform.
2. Definition
of Fourier transform For two-dimensional signals, two-dimensional Fourier transform is defined as:
Two-dimensional Fourier transform
inverse transform:
Two-dimensional discrete Fourier
two-dimensional discrete Fourier transform is:
Two-dimensional discrete Fourier transform
inverse transform:
Inverse transform
Fourier transform of image and Fourier transform of one-dimensional signal Like the Fourier transform, there are fast algorithms. For details, please refer to the bibliography. It is not difficult to find a program for the fast algorithm of the Fourier transform. In fact, there are now chips that implement Fourier transform, which can implement Fourier transform in real time.
3. Programs for realizing Fourier transform and DCT transform of digital images using MATLAB software.

4. Experimental steps

1. Turn on the computer, install and start the MATLAB program; the image file to be processed should be in the "work" folder in the program group;
2. Use the functions in the MATLAB toolbox to draw the function of the FFT spectrum display;
3. a) Call in, Display the image;
b) Do FFT and DCT on the image and display the spectrum using a self-edited function;
c) Discuss the correspondence between different image contents and the FFT and DCT spectrum.
4. Record and organize the experiment report.

V. Experimental content

MATLAB source program is as follows:

clear
clc
img=imread('peppers.png');
subplot(2,2,1);imshow(img);title('原图');
f=rgb2gray(img);    %对于RGB图像必须做的一步,也可以用im2double函数
F=fft2(f);          %傅里叶变换
F1=log(abs(F)+1);   %取模并进行缩放
subplot(2,2,2);imshow(F1,[]);title('傅里叶变换频谱图');
Fs=fftshift(F);      %将频谱图中零频率成分移动至频谱图中心
S=log(abs(Fs)+1);    %取模并进行缩放
subplot(2,2,3);imshow(S,[]);title('频移后的频谱图');
fr=real(ifft2(ifftshift(Fs)));  %频率域反变换到空间域,并取实部
ret=im2uint8(mat2gray(fr));    %更改图像类型
subplot(2,2,4);imshow(ret),title('逆傅里叶变换');

6. Experimental results

After running in MATLAB, the experimental results are as follows:
In Figure1, the upper left corner shows the original picture read into the MATLAB program, the upper right corner shows the spectrum image after two-dimensional Fourier fast transformation, and the lower left corner shows It is the spectrum image after moving the zero frequency component in the spectrum to the center of the matrix. The lower right corner shows the image after inverse Fourier transform.

Figure 1 Results after running MATLAB program
Figure 1 Results after running MATLAB program

7. Problems encountered in the experiment and reflections on the experiment process

1. About the imshow function:
pay attention to the image matrix type when using the imshow function to display images. When the image isdoubleFor the type, use imshow (I, []) to automatically set the gray image display range according to the value range of the data matrix.
For details, please refer to the matlab official website reference document: display image-MATLAB imshow-MathWorks China

2. About the warning that may appear at runtime:
If the image is displayed with the imshow function immediately after Fourier conversion, it may be displayed on the command line: Warning: Displaying real part of complex input (Warning: Display the real part of complex input items ). This is because the image matrix after Fourier transform is mostly a complex matrix, including real and imaginary parts. At this time, if you want to display the image, you need to use abs to take the modulus of the complex matrix before displaying it.
After the modulus is taken, the value of the image matrix is ​​generally very large, and it cannot be displayed directly by the imshow function. At this time, the log function can be used to take its logarithm, such as log (abs (F) +1), so that the spectrum can be scaled . As for why log (F + 1) is used, as shown in the figure below, the x value between (0, 1) will become a negative value after taking the logarithm, and log (x + 1) will convert all x values Map to a range of positive numbers.
Figure 2 log
Figure 2 log

3. Regarding the use of the im2double and rgb2gray functions at the beginning:
For RGB true-color images, the three-dimensional matrix is ​​stored after reading. If the Fourier transform is performed directly at this time, the spectrogram will be displayed as a blank or Densely packed. Therefore, before the Fourier transform of the RGB image, type conversion is required. You can use im2double to convert it to a double type (image operations rarely have integer types, so it is conservative to use this function to convert any image first) , Or use rgb2gray to convert it to a grayscale image.
The transformation effect is different when using different functions. For example, if im2double is used, the spectrogram will have a white tone after Fourier transform, and if rgb2gray, the spectrogram will have a gray tone.

4. Finally, use im2uint8 to convert the image type:
this step is to convert the matrix obtained by the inverse conversion into a grayscale image (mat2gray), and then convert the image type to uint8. In fact, whether this step is called is related to the use of the first two type conversion functions. If the RGB image has been converted to a grayscale image at the beginning, this step can be omitted, and the image after inverse conversion is directly displayed. If im2double is called at the beginning, a color image can be displayed after this step is omitted. Of course, this step plays different roles in different aspects, and the grayscale image taken can provide a basis for subsequent operations.

For more function information, please refer to the MATLAB website MATLAB-function

Please correct me if there are any mistakes

Published 13 original articles · Like 32 · Visits 10,000+

Guess you like

Origin blog.csdn.net/weixin_43637490/article/details/89196212