Frequency Domain Enhancement of Matlab's Image Enhancement

From the review in the previous section, we know that image enhancement can be divided into point enhancement, spatial domain enhancement, and frequency domain enhancement;

 

Spatial domain enhancement is directly enhanced on the picture, and there are various ready-made operators available; but for frequency domain enhancement, it needs to convert the picture to the frequency domain before conversion, and the frequency domain generally performs filtering. Such as low-pass filtering, high-pass filtering, etc. Today, let’s talk about the filtering methods used in frequency domain enhancement;

The first part: low-pass filter (keep low frequencies, filter high frequencies)

First, the ideal low-pass filter. The function of the low-pass filter is to save the low frequency and filter the high frequency; the content is to set a fixed value as the range, the part exceeding this number is 0, and the part below this number is 1. Example:

%低通滤波器
clc
clear all
I = imread('dream.jpg');
I = rgb2gray(I);
I = im2double(I);

m = 2*size(I, 1);%不乘2的话,D的范围减半以至于后面的部分都要减半,使得L无法成型
n = 2*size(I, 2);
x = -m/2 : (m/2-1);%获得在x方向的所有长度
y = -n/2 : (n/2-1);%获得在y方向上所有的长度
[X, Y] = meshgrid(x, y);%建立网格,方便进行计算到零点的距离,若步建立网格,会导致数组大小不兼容,建网格实际上是让他们的模板大小一样
D = sqrt(X.^2 + Y.^2);  %构造D矩阵,D是各位置与原点的距离
D0 = 80;  %D0为截止频率(中心频率)
H = double(D <= D0);%小于等于D0的位置为1,不满足的为0
J = fftshift(fft2(I, size(H, 1), size(H, 2))); %转换为频域再用fftshift进行平移,转换为与H相等大小的图像数据
K = J.*H;   %频域内滤波
L = ifft2(ifftshift(K));   %逆变换回到空域
L = L(1: size(I, 1), 1: size(I, 2));%因为L一开始是694*1040大小的,然后需要将其中转换为与原图片一样大的,若前面的M与N里面不乘以2的话,L的范围会比I小,同时显示出来的范围是不完整的

figure
subplot(121), imshow(I);
subplot(122), imshow(L);

Because I am also just learning, this program is my image enhancement technology from Matlab (9)_I want sugar blog-CSDN blog_matlab image enhancement

It is borrowed from it and made some own understanding; the low-frequency filter filters out the high-frequency and blurs the edge of the picture;

There is another way of writing:

clc
clear all
I = imread('dream.jpg');
I = rgb2gray(I);
I = im2double(I);
[m,n]=size(I);
M=floor(m/2);
N=floor(n/2);
H=zeros(m,n);
D0=60;
for i=1:1:m
    for j=1:1:n
        if(sqrt((i-m/2)^2+(j-n/2)^2)<=D0)
        H(i,j)=1;
        end
    end
end
J=fftshift(fft2(I));
K = J.*H;   
L = ifft2(ifftshift(K));
figure
subplot(121), imshow(I);
subplot(122), imshow(L);

In comparison, the second method is better to understand, and the second way of writing is recommended;

Second, the Butterworth low-frequency filter;

Example:

I = imread('dream.jpg');
I = rgb2gray(I);
I = im2double(I);

m = 2*size(I, 1);
n = 2*size(I, 2);
x = -m/2 : (m/2-1);
y = -n/2 : (n/2-1);
[X, Y] = meshgrid(x, y);
D = sqrt(X.^2 + Y.^2);%距离
D0 = 50;  %D0为截止频率
n = 6;      %阶数为6
H = 1./(1+(D./D0).^(2*n));%巴特沃斯的公式
J = fftshift(fft2(I, size(H, 1), size(H, 2))); %转换为频域再用fftshift进行平移
K = J.*H;   %频域内滤波
L = ifft2(ifftshift(K));   %逆变换回到空域
L = L(1: size(I, 1), 1: size(I, 2));

figure
subplot(121), imshow(I);
subplot(122), imshow(L);

Above is the formula for the Butterworth low-pass filter; 

There is also a second way of writing:

clc
clear all
I = imread('dream.jpg');
I = rgb2gray(I);
I = im2double(I);
[m,n]=size(I);
M=floor(m/2);
N=floor(n/2);
H=zeros(m,n);
D0=60;
for i=1:1:m
    for j=1:1:n
        D=sqrt((i-m/2)^2+(j-n/2)^2);
        H(i,j)=1./(1+(D./D0).^(2*n));
    end
end
J=fftshift(fft2(I));
K = J.*H;   
L = ifft2(ifftshift(K));   
figure
subplot(121), imshow(I);
subplot(122), imshow(L);

The second way of writing is recommended;

Third, a Gaussian low-pass filter

The above is a function of the Gaussian low-pass filter, D is the distance; for example:

clc
clear all
I = imread('dream.jpg');
I = rgb2gray(I);
I = im2double(I);
[m,n]=size(I);
M=floor(m/2);
N=floor(n/2);
H=zeros(m,n);
D0=60;
for i=1:1:m
    for j=1:1:n
        D=sqrt((i-m/2)^2+(j-n/2)^2);
        H(i,j)=exp(-(D)/(2*(D0^2)));
    end
end
J=fftshift(fft2(I));
K = J.*H; 
L = ifft2(ifftshift(K)); 
figure
subplot(121), imshow(I);
subplot(122), imshow(L);

Really grasp the principle (and some small order should also be remembered), these codes are really easy to have ideas; 

The second part: high frequency filter (filter low frequency, retain high frequency)

First, a Gaussian high-pass filter

The above formula is the formula of Gaussian high-pass filter; for example:

clc
clear all
I = imread('dream.jpg');
I = rgb2gray(I);
I = im2double(I);
[m,n]=size(I);
M=floor(m/2);
N=floor(n/2);
H=zeros(m,n);
D0=10;
for i=1:1:m
    for j=1:1:n
        D=sqrt((i-m/2)^2+(j-n/2)^2);
        H(i,j)=1-exp(-(D)/(2*(D0^2)));
    end
end
J=fftshift(fft2(I));
K = J.*H;  
L = ifft2(ifftshift(K)); 
figure
subplot(121), imshow(I);
subplot(122), imshow(L);

Same as Gaussian low pass, just changed the formula;

Second, the Butterworth high-pass filter

The above formula is a Butterworth high-pass filter, for example:

clc
clear all
I = imread('dream.jpg');
I = rgb2gray(I);
I = im2double(I);
[m,n]=size(I);
M=floor(m/2);
N=floor(n/2);
H=zeros(m,n);
D0=10;
for i=1:1:m
    for j=1:1:n
        D=sqrt((i-m/2)^2+(j-n/2)^2);
        H(i,j)=1./(1+(D0./D).^(2*n));
    end
end
J=fftshift(fft2(I));
K = J.*H;   
L = ifft2(ifftshift(K));  
figure
subplot(121), imshow(I);
subplot(122), imshow(L);

The third part, the band stop filter

Because I am also just getting started, I read a lot of codes on the Internet, and I found that there are many types of band-rent filters, such as Butterworth band-stop filters, ideal band-stop filters, and Gaussian band-stop filters; The ideal band-stop filter is to set a circular range, as long as it is 0 within this range, and 1 outside this range;

Example: I am learning Matlab's image enhancement technology (9)_I want sugar blog-CSDN blog_matlab image enhancement code:

clc
clear all
I = imread('dream.jpg');
I = rgb2gray(I);
I = imnoise(I, 'gaussian');
I = im2double(I);

m = 2*size(I, 1);
n = 2*size(I, 2);
x = -m/2 : (m/2-1);
y = -n/2 : (n/2-1);
[X, Y] = meshgrid(x, y);
D = sqrt(X.^2 + Y.^2);
D0 = 40;  
W = 20;   
H = double(or(D<(D0-W/2), D>(D0+W/2)));  %阻带为30到50,在这个范围内的为0,在这个范围之外的为1;  
orign = fftshift(fft2(I, size(H, 1), size(H, 2)));
K = orign.*H; 
picture = ifftshift(K);
picture=ifft2(picture);
picture = picture(1: size(I, 1), 1: size(I, 2));

figure
subplot(121), imshow(I);
subplot(122), imshow(picture);

You can also use a for loop;

Second, the Butterworth band-stop filter, the general content is the same as the high frequency and low frequency, but the calculation is different. Here, he needs to get the distance between each point and the noise starting point, that is, two distances are required. At the same time, the formula not the same

%D is the distance, D0 is the center frequency, W is the bandwidth, and n is the order;

Third, the Gaussian band-stop filter, the formula is:

The codes of these three band-stop filters refer to low frequency and high frequency, and can be used only by changing the formula; but the selection of bandwidth needs to be considered by oneself;

The fourth part: homomorphic filtering

Homomorphic filtering, the reflection component and illumination component of a picture are multiplicative. If the Fourier transform is direct, the Fourier form is convolution. We don’t want this form. In order to avoid this, log them in advance , so that after Fourier transform, it is not a convolution form; the function of homomorphic filtering is to adjust the light, so that we can see what cannot be seen in the dark, increase the contrast, and reduce the influence of light;

 When rH (high frequency weight) is greater than 1 and rL (low frequency weight) is less than 1, the purpose of filtering low and increasing can be achieved, and the constant c controls the steepness of the Gaussian function. The larger the D0, the better the enhancement of details and the brighter the normalized image. The D0 of each picture is different and defined according to the picture; 

The fifth part: bandpass filter

The function of the band-pass filter is opposite to that of the band-stop filter. The function is to allow the signal within a certain range to pass through, the signal in it is 1, and the signal outside it is 0; the formula is: 1-band-stop filter; 

Part Six: Notch Filter

Like a band-stop filter, it can also filter, but it will also attenuate signals other than noise; like a notch filter, it attenuates at a certain point, and the rest will not be affected; 

The above formula is the formula of the notch filter. Because of the periodicity of Fourier, a noise corresponds to a symmetrical point, and Dk and Dk correspond to two distances respectively; (symmetrical about the center point);

 

Guess you like

Origin blog.csdn.net/new_EAGLE/article/details/125803490