Communication Principles in Image Processing - Gonzalez's Reading Notes (2)

Refer to an article on Bole Online to do the Fourier transform of the image . An error occurred when installing cv2 in PyCharm, as shown below:

According to the prompt, executing the command pip install cv2 on the command line is still No matching distribution found for cv2. Note that pip can be updated, but this is not the problem. However, adding the same py file to the venv folder gave no import errors. venv is short for virtual environment. The information found is as follows: the running environment required by each application is often different (package version/python version, etc.), so it is necessary to use virtualenv to create a set of "isolated" Python running environments belonging to this program . And the system Python environment will not be modified, keep it clean. It is possible to move directories after packaging, but not cross-platform . Pay attention to which python interpreter is the current project , and be sure to set it to python in the virtual environment. The virtual environment created by virtualenv is completely independent of the host's Python environment. Libraries configured by your host cannot be used directly in virtualenv. You need to use pip install to install the configuration again in the virtual environment to use it.

Note that the interpreter also has different versions according to the project, which distinguishes which modules are installed. When changing an interpreter in venv, the installation of cv2 still fails, but the result of the program is correct.


#!/usr/bin/env.python
import cv2 as cv
import numpy as np
from matplotlib import pyplot as plt

img = cv.imread('Joseph_Fourier_250.jpg', 0)
f = np.fft.fft2(img) # Fast Fourier transform algorithm to get the frequency distribution
fshift = np.fft.fftshift(f) # The default result center point position is in the upper left corner, shift to the middle position

fimg = np.log(np.abs(fshift)) # fft The result is a complex number, and the absolute value is the amplitude
fimg0=np.log(np.abs(f))

# show results
plt.subplot(131), plt.imshow(img, 'gray'), plt.title('Original Fourier')
plt.subplot(132), plt.imshow(fimg0, 'gray'), plt.title('Fourier Fourier') #Spectrum before center displacement
plt.subplot(133), plt.imshow(fimg, 'gray'), plt.title('Fourier Fourier-shift') #The spectrum after the center shift, but the coordinate axis does not change, the frequency of the center should be 0 at this time
plt.show()

Moving the center of the spectrum is mentioned in the code above. The specific operation usually multiplies the input image function by (-1)^(x+y) before the Fourier transform, so that the origin of the Fourier transform F(0,0) can be moved to (M/2 , N/2) on.In addition to clearly seeing the frequency distribution of the image, shifting the frequency spectrum to the center of the circle has another advantage. It can separate periodic interference signals, such as sinusoidal interference , a pair of sinusoidal interference, frequency shifted to the origin. On the spectrogram, it can be seen that in addition to the center, there is a set of bright spots centered on a certain point and symmetrically distributed, and this set is generated by the interference noise.



The essence of the Fourier transform is to decompose a signal into a superposition of a series of sinusoids. A sinusoid has three properties: frequency; amplitude; phase. We see that each term of the Fourier transform (the value of F(u,v) for each pair of frequencies u,v) consists of the sum of all the values ​​of the f(x) function. The value of f(x) is multiplied by the sine and cosine values ​​of various frequencies. If there are exactly the same frequency components, multiply to get a value, the magnitude. If there is no specific frequency component, the product should be 0, in fact, the Fourier transform is an orthogonal transform.

Amplitude describes contrast, the difference between the lightest and darkest parts of an image. Represented by brightness in the spectrum, higher brightness means greater contrast.

Frequency is the frequency at which the brightness changes over the spatial domain. The spectrum obtained by the Fourier transform, with frequencies from 0 to the Nyquist frequency (the highest frequency at which a digital image can be encoded, related to resolution and pixel size). The component whose frequency is 0 is the DC component, because the brightness cannot be negative, so the DC component of all images is not 0.

The phase represents how the sinusoid is shifted (offset) relative to the original state.



I=imread('sin3.PNG');

figure;
subplot(1,2,1),imshow(I),title('Original image');
I=rgb2gray(I);% convert to grayscale

I=im2double(I);% Convert to double type, the image gray value changes from 0~255 to 0~1

F=fft2(I);

F=fftshift(F);% quadrant shift

F=abs(F);
%F=real(F); Some programs are for touch, and some are for real

T=log(F+1);% Guaranteed to be positive after taking the logarithm

subplot(1,2,2),imshow(T,[]),title('Fourier');%imshow(T) will only display the value of [0,1], while imshow(T,[]) will The range of values ​​for a grayscale image to display the image

The center point of the spectrogram represents the DC component, and the spectrogram is symmetrical about the center point. As shown in the figure below, although there is only one frequency component, there are three bright spots. The screenshot is from reference link 1. The picture given in link 1 is in gif format. After checking it, we know that gif is actually a lossless compression format. The original meaning of GIF (Graphics Interchange Format) is " image interchange format", but it can be The ability to save multiple images to the same file enables them to be displayed as moving images as they are read to the screen, image by image. The program just now directly reads the gif file and the suffix is ​​changed to jpg. Both errors are reported. OpenCV does not support the decoding of gif (it seems to be due to the patent of the LZW algorithm in gif). It can be read by the third-party library FreeImage. Here I use a screenshot The software only draws the spectrum, as shown in the right half below, but unlike the analysis just now and the experimental results on the left, the graph on the right is a straight line, and the "background color" of the spectrogram is white. When using the above matlab to draw the spectrogram, the color is just the opposite of that drawn by python. Breakpoint debugging found that a large number of -inf appeared in the fimg variable, which caused the "background" to be completely white. At the same time, I noticed that the console displayed a prompt divide by zero, and it was judged that negative infinity was caused in the process of taking logarithms. The root matlab is the same as Correction by +1 before taking the logarithm yields the same result as matlab. Opencv also supports image Fourier transform, you can click here if you are interested .

Now that the problem of "background color" has been solved, there is another problem that the experimental result of the own spectrum is a line and the Fouier Transform is three points. The reference link is actually explaining this problem at the end. In fact, this is the "ringing effect", which is also the "Gibbs phenomenon". Carefully observe that the input black and white fringe image is gradual, so what law does it change according to? The answer is the sine and cosine function. If it is completely gradient according to the cosine function, its corresponding spectrum is a single frequency point, considering the positive and negative symmetry and the DC component are just three points. Here I noticed the gradual change factor at first, because as long as the interval is the same, there is only one frequency, and I gave Zhengke a wrong explanation.

However, what happens to the spectrum when the black and white stripes are not as smooth as the cosine function, at an extreme, we consider black and white stripes with no gradient at all, no transition between black and white stripes, which corresponds to a one-dimensional rectangle. We know that a rectangle needs an infinite number of cosine functions to fit, and it needs a lot of high-frequency components. In a two-dimensional black and white jump image, its spectrum is an infinite set of "three points". As shown in the figure below, from 1 to 7, the fringes are getting denser and the frequency is getting bigger and bigger (respectively 3, 5, 7 times of the reference frequency), and the coordinate value of the corresponding mid-point of the spectrum is getting bigger and bigger, and the interval is bigger and bigger. come bigger. When the fringe patterns of different frequencies are superimposed, their spectrums are also the superposition of the corresponding spectrums. It can be observed that with the superposition of high-frequency components, the white bar in the middle of the fringe luminance map becomes more and more prominent, other places become more and more black background, and the spectrogram becomes more and more close to a straight line. In fact, this explains why the spectrogram of the last plot is a straight line.


Then analyze the above two spectrums. The spectrum on the left seems to have four lines, but it is actually just two vertical lines, representing the horizontal and vertical brightness changes respectively, but they do not pass through the center point because of the phase shift brought by the original image. Combined with the case on the right, the spectrum is independent of where the brightness change occurs.

Look, it's just that the most familiar Fourier transform has so many situations in discrete and two-dimensional situations. It's really shallow on paper, keep working hard!

Reference:

1.http://cns-alumni.bu.edu/~slehar/fourier/fourier.html#filtering

2.https://blog.csdn.net/abcjennifer/article/details/7622228

3.http: //blog.jobbole.com/94144/

4.https://blog.csdn.net/keith_bb/article/details/53389819

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325529332&siteId=291194637