Image Fourier transform experiment

1. Purpose of the experiment

1. Master the basic principles and methods of Fourier transform and inverse transform.
2. Understand the basic principles and methods of frequency domain filtering.
3. Master the method of image frequency domain filtering.

2. Experimental principle

1. Smoothing filter

(1) Linear smoothing filter

All coefficients of the linear low-pass smoothing filter are positive numbers. For the 3×3template, the simplest mean filter takes all coefficients as 1. In order to keep the output image still within the gray value range of the original image, the template is adjacent to the pixel All domain products are divided by 9. MATLABProvides fspecialthe template used by the function to generate the filter, and provides filter2the function to operate on the image with the specified filter template. A function is MATLABprovided imnoiseto add noise to the image, please refer to the command and usage MATLABby yourself. There are similar functions, please check and master by yourself.helpfspecialfilter2imnoiseOpenCV

(2) Nonlinear smoothing filter

The median filter is a commonly used nonlinear smoothing filter. Its filtering principle is similar to the mean filter method, but instead of calculating the weighted sum, it sorts the pixels of the image in the neighborhood according to the gray level, and then The median value of this group is chosen as the output pixel value. MATLABFunctions are provided medfilt2to implement median filtering. OpenCVThere are also similar functions, please ask students to inquire by themselves.

2. Sharpening filter

Image smoothing often blurs the boundaries and outlines in the image. In order to reduce the impact of such adverse effects, it is necessary to use image sharpening technology to make the edges of the image clear.

(1) Linear sharpening filter

A linear high-pass filter is the most commonly used linear sharpening filter. The central coefficients of this filter are all positive, while the surrounding coefficients are all negative, and the sum of all coefficients is 0.

(2) Non-linear sharpening filter

Neighborhood averaging can blur images, and differentiation can sharpen images because averaging corresponds to integrals. The most commonly used method of differentiation in image processing utilizes gradients. Commonly used spatial domain nonlinear sharpening filter differential operators include sobeloperator, prewittoperator, logoperator and so on.

3. Frequency Domain Enhancement

Frequency domain enhancement is to use the image transformation method to transform the image in the original image space into other spaces in some form, then use the unique properties of this space to conveniently process the image, and finally convert it back to the original image space, so that Get the processed image.
The main steps of frequency domain enhancement are:
(1) Select the transformation method to transform the input image into the frequency domain space.
(2) In the frequency domain space, design a transfer function according to the processing purpose, and process it.
(3) Inversely transform the obtained result to obtain an enhanced image.
(4) Commonly used frequency domain enhancement methods include low-pass filtering and high-pass filtering.

4. Low-pass filtering

Most of the energy of the image is concentrated in the low and middle frequency parts of the amplitude spectrum, while the edges and noise of the image correspond to the high frequency part. Therefore, a filter that reduces the amplitude of high-frequency components can reduce the influence of noise. According to the convolution theorem, the mathematical expression of low-pass filtering is realized in the frequency domain:

(1) Ideal low-pass filter ( ILPF)

insert image description here

(2) Butterworth low-pass filter ( BLPF)

insert image description here

(3) Exponential low-pass filter ( ELPF)

insert image description here

5. High-pass filtering

Since the details in the image correspond to its high-frequency components, high-pass filtering can sharpen the image. The high-pass filter is the opposite of the low-pass filter, which allows the high-frequency components to pass through smoothly, so that the low-frequency components are weakened. The high-pass filter is similar to the low-pass filter, and their transfer functions are:

(1) Ideal high-pass filter ( IHPF)

insert image description here

(2) Butterworth high-pass filter ( BLPF)

insert image description here

(3) Exponential high-pass filter ( ELPF)

insert image description here
After the image is processed by high-pass filtering, a lot of low-frequency information will be lost, so the smooth area of ​​the image will basically disappear. Therefore, high-frequency enhanced filtering can be used to make up for it. High-frequency enhanced filtering is to add a constant greater than 0 and less than 1 when designing the filter transfer function, namely:
insert image description here

3. Experiment content steps (record the main steps of the experiment, and after the debugging is successful, take a screenshot or take a photo to save the result)

MATLABThe use of or is required OpenCVto complete the following experiments.

1. Smooth spatial filtering

(1) Read out a grayscale image, add salt and pepper noise and Gaussian noise to this image and display it in the same image window as the previous image.
lena = imread('lena.jpg'); %读入原始图像
lena_gray = rgb2gray(lena); %原始图像灰度化

figure, subplot(1, 3, 1);
imshow(lena_gray);
title('原始图像');
lena_pepper = imnoise(lena_gray, 'salt & pepper', 0.02); %加入椒盐噪声

subplot(1, 3, 2);
imshow(lena_pepper);
title('椒盐噪声图像');

lena_gauss = imnoise(lena_gray, "gaussian", 0.02); %加入高斯噪声
subplot(1, 3, 3);
imshow(lena_gauss);
title('高斯噪声图像');

insert image description here

(2) Select different smoothing (low-pass) templates for the noise-added image, compare the effects of different templates, and display them in the same window.
lena = imread('lena.jpg'); %读入原始图像

lena_gray = rgb2gray(lena); %原始图像灰度化
figure, subplot(1, 3, 1);
imshow(lena_gray);
title('原始图像');

lena_pepper = imnoise(lena_gray, 'salt & pepper', 0.02); %加入椒盐噪声
subplot(1, 3, 2);
imshow(lena_pepper);
title('椒盐噪声图像');

lena_gauss = imnoise(lena_gray, "gaussian", 0.02); %加入高斯噪声
subplot(1, 3, 3);
imshow(lena_gauss);
title('高斯噪声图像');

%对椒盐噪声图像进行滤波处理
h = fspecial('average', 3);
I1 = filter2(h, lena_pepper) / 255; %二维数字滤波器
I2 = medfilt2(lena_pepper, [3 3]); %二维中位数滤波
figure, subplot(2, 2, 1);
imshow(lena);
title('原始图像');

subplot(2, 2, 2);
imshow(lena_pepper);
title('椒盐噪声图像');

subplot(2, 2, 3);
imshow(I1);
title('3*3 均值滤波图像');

subplot(2, 2, 4);
imshow(I2);
title('3*3 中值滤波图像');

%对高斯噪声图像进行滤波处理
G1 = filter2(h, lena_gauss) / 255; %二维数字滤波器
G2 = medfilt2(lena_gauss, [3 3]); %二维中位数滤波

figure, subplot(2, 2, 1);
imshow(lena);
title('原始图像');

subplot(2, 2, 2);
imshow(lena_gauss);
title('高斯噪声图像');

subplot(2, 2, 3);
imshow(G1);
title('3*3 均值滤波图像');

subplot(2, 2, 4);
imshow(G2);
title('3*3 中值滤波图像');

insert image description here
insert image description here
insert image description here

(3) If used MATLAB, when using the function imfilter, use different padding methods (or boundary options, such as zero padding, ' replicate', ' symmetric', ' circular') to perform low-pass filtering and display the processed image.
close all
lena=imread('lena.jpg');    %将原始图像读入
lena_gray=rgb2gray(lena);   %将图像灰度化
h=fspecial('motion',50,45);  %创建预定义的二维滤波器
filteredimg=imfilter(lena_gray,h);    % 对图像进行滤波,默认通过使用相关来完成
boundaryReplicate=imfilter(lena_gray,h,"replicate") ;    %图像大小通过复制外边界的值来扩展
boundary0=imfilter(lena_gray,h,0);   % 输入图像的边界通过用值X(无引号)来填充扩展,其默认值为0
boundarysymmetric=imfilter(lena_gray,h,"symmetric");  %图像大小通过镜像反射其边界来扩展
boundarycircular=imfilter(lena_gray,h,'circular');   %图像大小通过将图像看成是一个二维周期函数的一个周期来扩展

figure,subplot(3,2,1);%为各个分块创建分区
imshow(lena);   
title('原始图像');
subplot(3,2,2);
imshow(filteredimg);
title('Motion Blurred');
subplot(3,2,3);
imshow(boundaryReplicate);
title('Replicate');
subplot(3,2,4);
imshow(boundary0);
title('0-Padding');
subplot(3,2,5);
imshow(boundarysymmetric);
title('symmetric');
subplot(3,2,6);
imshow(boundarycircular);
title('cicular');

insert image description here

(4) Use fora loop to perform 10 and 20 average filtering on the image with salt and pepper noise, check its characteristics, and display the image after average processing (reminder: use the ' ' type of the function to generate the average fspecialfilter average).
close all
lena=imread('lena.jpg');    %将原始图像读入
lena_gray=rgb2gray(lena);   %将图像灰度化
lena_pepper=imnoise(lena_gray,'salt & pepper',0.02);    %加入椒盐噪声
h=fspecial('average');      %生成均值滤波器
for i=1:10  %对椒盐噪声图像进行10次均值滤波
    J1=imfilter(lena_pepper,h); end
for j=1:20  %对椒盐噪声图像进行20次均值滤波
    J2=imfilter(lena_pepper,h); end;
figure,subplot(1,3,1);
imshow(lena_pepper);
title('椒盐噪声图像');
subplot(1,3,2);
imshow(J1);
title('10次均值滤波');
subplot(1,3,3);
imshow(J2);
title('20次均值滤波');

insert image description here

(5) For the image with salt and pepper noise, the average filter method and the median filter method are used to process the noisy image respectively, and the results are required to be displayed in the same window.
close all
lena=imread('lena.jpg');    %读入原始图像
lena_gray=rgb2gray(lena);   %将图像灰度化
lena_pepper=imnoise(lena_gray,'salt & pepper',0.02);    %加入椒盐噪声
h=fspecial('average');  %生成均值滤波器
J1=imfilter(lena_pepper,h);
J2=medfilt2(lena_pepper);
figure,subplot(1,3,1);
imshow(lena_pepper);
title('椒盐噪声图像');
subplot(1,3,2);
imshow(J1);
title('均值滤波法');
subplot(1,3,3);
imshow(J2);
title('中值滤波法');

insert image description here

(6) Design a smoothing spatial filter by yourself, process it on the noise image, and display the processed image.
lena=imread('lena.jpg');    %读入原始图像
lena_gray=rgb2gray(lena);   %将图像灰度化
lena_pepper=imnoise(lena_gray,'salt & pepper',0.02);    %加入椒盐噪声
[m,n]=size(lena_pepper);
figure,subplot(1,2,1);
imshow(lena_pepper);
title('椒盐噪声图');
s=zeros(1,9);
for i=2:1:m-1
    for j=2:1:n-1
        h=1;
        for p=i-1:1:i+1
            for q=j-1:1:j+1
                s(h)=lena_pepper(p,q);
                h=h+1;
            end
        end
        s=sort(s);
       I(i,j)=s(3,3);
    end
end

subplot(1,2,2);
imshow(I);
title('噪声处理后的图像');

insert image description here

2. Sharpen spatial filtering

(1) Read out a grayscale image, and use the Laplacian operator to filter it.
close all
lena=imread('lena.jpg');    %读入原始图像
lena_gray=rgb2gray(lena);   %将图像灰度化
lena_double=im2double(lena_gray) %将图像转换为双精度值
w=[1,1,1;
    1,-8,1;
    1,1,1]
k=conv2(lena_double,w,"same");
figure,subplot(1,2,1);      
imshow(lena);
title('原始图像');
subplot(1,2,2);      
imshow(k);
title('滤波处理后的图像');

insert image description here

(2) Write a function w = genlaplacian(n)to automatically generate a Laplacian of any odd size, such as the Laplacian of

insert image description here

num=input('请输入数字n:');
n=num;
W=ones(n,n);
for i =1:n
    for j=1:n
        if(i==fix(n/2)+1&&j==fix(n/2)+1)
            W(i,j)=n*n-1;
        end
    end
end
display(W)

insert image description here

(3) Use 5×5, 9×9, 15×15 and 25×25 Laplacian operators to sharpen and filter the image, and use Eq. Different, require display in the same window.
function [w]=lapulasi(num)
n=num;
w=ones(n);
x=fix(n/2)+1;
w(x,x)=-(n*n-1);
end
function [w]=lapulasi(num)
n=num;
w=ones(n);
x=fix(n/2)+1;
w(x,x)=-(n*n-1);
end
lena=imread("lena.jpg");    %读入原始图像
lena_double=im2double(lena);    %将图像转换为双精度值
figure,subplot(2,3,1);
imshow(lena_double);
title('原始图像');
w0=lapulasi(3);
w1=lapulasi(5);
w2=lapulasi(9);
w3=lapulasi(15);
w4=lapulasi(25);
lena_0=lena_double-imfilter(lena_double,w0,"replicate");
subplot(2,3,2);
imshow(lena_0);
title('3*3 拉普拉斯');
lena_1=lena_double-imfilter(lena_double,w1,"replicate");
subplot(2,3,3);
imshow(lena_1);
title('5*5 拉普拉斯');
lena_2=lena_double-imfilter(lena_double,w2,"replicate");
subplot(2,3,2);
imshow(lena_2);
title('9*9 拉普拉斯');
lena_3=lena_double-imfilter(lena_double,w3,"replicate");
subplot(2,3,5);
imshow(lena_3);
title('15*15 拉普拉斯');
lena_4=lena_double-imfilter(lena_double,w4,"replicate");
subplot(2,3,6);
imshow(lena_4);
title('25*25 拉普拉斯');
(4) Use different gradient operators to sharpen and filter the image, and compare their effects.
[I,map]=imread('lena_gray.jpg'); 
I=double(I); 
subplot(2,3,1) 
imshow(I,map); 
title('原始图像'); 
[Gx,Gy]=gradient(I); % 梯度计算 
G=sqrt(Gx.*Gx+Gy.*Gy); % 矩阵 
J1=G; % 梯度1 
subplot(2,3,2)
imshow(J1,map); 
title(' 操作1图像'); 
J2=I; % 梯度2 
K=find(G>=7); 
J2(K)=G(K); 
subplot(2,3,3) 
imshow(J2,map); 
title('操作2图像'); 
J3=I; % 梯度3 
K=find(G>=7); 
J3(K)=255; 
subplot(2,3,4) 
imshow(J3,map); 
title('操作3图像'); 
J4=I; % 梯度4 
K=find(G<=7); 
J4(K)=255; 
subplot(2,3,5) 
imshow(J4,map); 
title('操作4图像'); 
J5=I; %梯度5 
K=find(G<=7); 
J5(K)=0; 
Q=find(G>=7); 
J5(Q)=255; 
subplot(2,3,6) 
imshow(J5,map); 
title('操作5图像');

insert image description here

(5) Design the sharpening spatial filter by yourself, and process it on the noise image, and display the processed image.
lena=imread("lena.jpg");
lena_gray=rgb2gray(lena);   %转换为灰度图像
h=fspecial("sobel");
h1=h'*0.5;
h2=h';
h3=h'*1.5;
z1=imfilter(lena_gray,h1);
z2=imfilter(lena_gray,h2);
z3=imfilter(lena_gray,h3);
figure,subplot(2,2,1);
imshow(lena_gray);
title('原始图像');
subplot(2,2,2);
imshow(z1);
title('梯度算子1');
subplot(2,2,3);
imshow(z2);
title('梯度算子2');
subplot(2,2,4);
imshow(z3);
title('梯度算子3');

insert image description here

3. Fourier transform

(1) Read out a grayscale image, perform fast Fourier transform on it, and display its amplitude image and phase image respectively.
lena=imread("lena.jpg");
lena_gray=rgb2gray(lena);
f1=fft2(lena_gray); %快速傅里叶变换
f2=log(1+abs(f1));  %振幅谱
f3=fftshift(f1);
f4=angle(f1);   %相位谱
figure,subplot(1,3,1);
imshow(lena_gray);
title('原始图像');
subplot(1,3,2);
imshow(log(1+abs(f3)),[]);
title('幅度图像');
subplot(1,3,3);
imshow(f4);
title('相位图像');

insert image description here

(2) View the resulting image after inverse Fourier transform of only the phase part.
lena=imread("lena.jpg");
lena_gray=rgb2gray(lena);
f1=fft2(lena_gray); %快速傅里叶变换
f=ifft(abs(f1));
figure,subplot(1,3,1);
imshow(lena_gray);
title('原始图像');
subplot(1,3,2);
imshow(log(1+abs(f3)),[]);
title('振幅图像');
subplot(1,3,3);
imshow(log(1+abs(f)),[]);
title('相位图像');

insert image description here

(3) Check the resulting image after inverse Fourier transform of only the amplitude part.
lena=imread("lena.jpg");
lena_gray=rgb2gray(lena);
f1=fft2(lena_gray); %快速傅里叶变换
f2=log(1+abs(f1));
f3=fftshift(f1);
f4=angle(f1);
f5=-f4;
f6=double(f3*exp(f4));   %傅里叶变换的复共轭
f7=ifft2(f6);   %反傅里叶变换
figure,subplot(1,2,1);
imshow(lena_gray);
title('原始图像');
subplot(1,2,2);
imshow(real(f7),[]);
title('逆傅里叶变换');

Here is the quote

(4) Set the Fourier transform of the image to its conjugate and perform an inverse transform to compare the difference between the newly generated image and the original image.
lena=imread("lena.jpg");
lena_gray=rgb2gray(lena);
f1=fft2(lena_gray); %快速傅里叶变换
f2=log(1+abs(f1));
f3=fftshift(f1);
f4=angle(f1);
f5=f4;
f6=double(f3*exp(f4));  %傅里叶变换的复共轭
figure,subplot(1,2,1);
imshow(lena_gray);
title('原始图像');
subplot(1,2,2);
imshow(real(f7),[]);
title('逆傅里叶变换');

insert image description here

4. Smooth frequency domain filtering

(1) Design an ideal low-pass filter, a Butterworth low-pass filter and a Gaussian low-pass filter, with optional cutoff frequencies.
%理想低通滤波器的透视图
a=100;
b=100;
U=0:a;
V=0:b;
M=length(U);
N=length(V);
D0=10;  %D0是频带的中心半径
x1=50;
y1=50;
x0=-50;
y0=-50;
m=fix(M/2);
n=fix(N/2);
H=zeros(M,N);
n=2;
for u=1:M
    for v=1:N
        a=sqrt((U(u)-50).*(U(u)-50)+(V(v)-50).*(V(v)-50));  %(u,v)的值
        if(a<=D0)   %理想滤波器
            H(u,v)=1;
        else
            H(u,v)=0;
        end
    end
end
figure,subplot(1,3,1);
surf(U,V,H);
title('理想低通滤波透视图');
%巴特沃斯低通滤波器透视图
a=100;    
b=100;    
U=0:a;    
V=0:b;    
M=length(U);N=length(V);    
D0=10;%W=200;%D0 是频带的中心半径;W 是频带的宽度
x1=50;y1=50;    
x0=-50;y0=-50;    
m=fix(M/2); 
n=fix(N/2);    
H=zeros(M,N);    
n=2;    
for u=1:M    
    for v=1:N    
        a=sqrt((U(u) - 50) .* (U(u)-50) + (V(v) - 50) .* (V(v) - 50));%D(u,v)的值
        b=1+(a/D0)^2*n;    
        H(u,v)=1/b;  
    end    
end    
subplot(1,3,2);
surf(U,V,H);  
title('巴特沃斯低通滤波器透视图');
%高斯低通滤波
a=100;    
b=100;    
U=0:a;    
V=0:b;    
M=length(U);
N=length(V);    
D0=10; %D0 是频带的中心半径
x1=50;
y1=50;    
x0=-50;
y0=-50;    
m=fix(M/2);
n=fix(N/2);    
H=zeros(M,N);          
for u=1:M    
    for v=1:N    
        D1=((u-m-x0)^2+(v-n-y0).^2)^0.5;    
        D2=((u-m+x0)^2+(v-n+y0).^2)^0.5;    
        D11=((u-m-x1)^2+(v-n-y1).^2)^0.5;    
        D21=((u-m+x1)^2+(v-n+y1).^2)^0.5;    
        H(u,v) = (U(u) - 50) .* (U(u)-50) + (V(v) - 50) .* (V(v)  - 50);   
    end    
end   
S=50;    
H = -H/(2*S);    
H = exp(H) / (sqrt(2*pi) * sqrt(S));    
subplot(1,3,3),surf(U,V,H),title('高斯低通滤波');

Here is the quote

(2) Read out a grayscale image, filter it with ideal low-pass filter, Butterworth low-pass filter and Gaussian low-pass filter respectively (cutoff frequency is optional), and then perform inverse transformation to observe different The difference between the image obtained by using different low-pass filters at the cutoff frequency and the original image, pay special attention to the ringing effect.
%d0=15 理想低通滤波
lena=imread("lena.jpg");
lena_gray=rgb2gray(lena);
f=double(lena_gray);
g=fft2(f);  %傅里叶变换
g=fftshift(g);
[M,N]=size(g);
d0=15;
m=fix(M/2);
n=fix(N/2);
for i=1:M
    for j=1:N
        d=sqrt((i-m)^2+(j-n)^2);
        if(d<d0)
            h=1;
        else
            h=0;
        end
        result(i,j)=h*g(i,j);
    end
end
result=ifftshift(result);
J1=ifft2(result); 
J2=uint8(real(J1));  
figure,subplot(2,2,1);
imshow(lena);
title('原始图像'); 
subplot(2,2,2);
imshow(J2);
title('d0=15 理想低通滤波器');
%d0=30 的理想低通滤波
d0=30;  
m=fix(M/2); 
n=fix(N/2); 
for i=1:M  
    for j=1:N  
        d=sqrt((i-m)^2+(j-n)^2);  
        if(d<=d0)  
            h=1;  
        else   
            h=0;  
        end  
        result(i,j)=h*g(i,j);  
    end 
end  
result=ifftshift(result); 
J1=ifft2(result); 
J2=uint8(real(J1));  
subplot(2,2,3);
imshow(J2);
title('d0=30 理想低通滤波器');
%d0=100 的理想低通滤波
d0=100;  
m=fix(M/2); 
n=fix(N/2); 
for i=1:M  
    for j=1:N  
        d=sqrt((i-m)^2+(j-n)^2);  
        if(d<=d0)  
            h=1;  
        else   
            h=0;  
        end  
        result(i,j)=h*g(i,j);  
    end 
end  
result=ifftshift(result); 
J1=ifft2(result); 
J2=uint8(real(J1));  
 subplot(2,2,4);
 imshow(J2);
 title('d0=100 理想低通滤波器');

Here is the quote
When the cut-off frequency d0=15is higher , the filtered image is blurred, and the ringing phenomenon is also obvious; at d0=30this time, the blurring degree of the image is weakened, but the ringing phenomenon still exists. At that d0=100time, the filtered image was relatively clear, but after the loss of high-frequency components, there was still a little ringing at the edge of the image.

5. Sharpen frequency domain filtering

(1) Design ideal high-pass filter, Butterworth high-pass filter and Gaussian high-pass filter, with optional cut-off frequency.
lena=imread("lena.jpg");
lena_gray=rgb2gray(lena);
f=double(lena_gray);
g=fft2(f);  %傅里叶变换
g=fftshift(g);
[M,N]=size(g);

%d0=15的巴特沃斯高通滤波器
nn=2;
d0=15;
m=fix(M/2);
n=fix(N/2); 
for i=1:M  
    for j=1:N  
        d=sqrt((i-m)^2+(j-n)^2);  
        h=1/(1+0.414*(d/d0)^(2*nn));  
        result(i,j)=h*g(i,j);  
    end 
end  
result=ifftshift(result); 
J1=ifft2(result); 
J2=uint8(real(J1));

figure,subplot(2,2,1);
imshow(lena);
title('原始图像'); 
subplot(2,2,2);
imshow(J2);
title('d0=15 巴特沃斯高通滤波器')
%d0=30的巴特沃斯高通滤波器
d0=30;  
m=fix(M/2);
n=fix(N/2); 
for i=1:M  
    for j=1:N  
        d=sqrt((i-m)^2+(j-n)^2);  
        h=1/(1+0.414*(d/d0)^(2*nn));  
        result(i,j)=h*g(i,j);  
    end 
end  
result=ifftshift(result); 
J1=ifft2(result);
J2=uint8(real(J1));   
subplot(2,2,3);
imshow(J2);
title('d0=30 巴特沃斯高通滤波器');
%d0=100的巴特沃斯高通滤波器
d0=100;  
m=fix(M/2);
n=fix(N/2); 
for i=1:M  
    for j=1:N  
        d=sqrt((i-m)^2+(j-n)^2);  
        h=1/(1+0.414*(d/d0)^(2*nn));  
        result(i,j)=h*g(i,j);  
    end 
end  
result=ifftshift(result); 
J1=ifft2(result);
J2=uint8(real(J1));   
subplot(2,2,4);
imshow(J2);
title('d0=100 巴特沃斯高通滤波器');
%d0=15的高斯低通滤波
d0=15;  
m=fix(M/2);
n=fix(N/2); 
for i=1:M  
    for j=1:N  
        d=sqrt((i-m)^2+(j-n)^2);  
        h=exp(-(d.^2)./(2*(d0^2)));  
        result(i,j)=h*g(i,j);  
    end 
end  
result=ifftshift(result); 
J1=ifft2(result);
J2=uint8(real(J1)); 
figure,subplot(2,2,1);
imshow(lena);
title('原始图像'); 
subplot(2,2,2);
imshow(J2);
title('d0=15 高斯高通滤');  
%d0=30的高斯低通滤波
d0=30;  
m=fix(M/2);
n=fix(N/2); 
for i=1:M  
    for j=1:N  
        d=sqrt((i-m)^2+(j-n)^2);  
        h=exp(-(d.^2)./(2*(d0^2)));  
        result(i,j)=h*g(i,j);  
    end 
end  
result=ifftshift(result);
J1=ifft2(result);
J2=uint8(real(J1)); 
subplot(2,2,3);
imshow(J2);
title('d0=30 高斯高通滤波');  
%d0=100的高斯低通滤波 
d0=100;  
m=fix(M/2);
n=fix(N/2); 
for i=1:M  
    for j=1:N  
        d=sqrt((i-m)^2+(j-n)^2);  
        h=exp(-(d.^2)./(2*(d0^2)));  
        result(i,j)=h*g(i,j);  
    end 
end  
result=ifftshift(result); 
J1=ifft2(result);
J2=uint8(real(J1));   
subplot(2,2,4);
imshow(J2);
title('d0=100 高斯高通滤波');

insert image description here

(2) Read out a grayscale image, filter it with ideal high-pass filter, Butterworth high-pass filter and Gaussian high-pass filter respectively (cutoff frequency is optional), and then perform inverse transformation to observe the different cutoff frequencies The difference between the image obtained by using different high-pass filters and the original image.
close all
lena=imread('lena.jpg');    %将原始图像读入
lena_gray=rgb2gray(lena);   %将图像灰度化
lena_double=double(lena_gray);
g=fft2(f);  %傅里叶变换
g=fftshift(g);
[M,N]=size(g);
%d0=15的理想高通滤波器
d0=15;
m=fix(M/2);
n=fix(N/2); 
for i=1:M  
    for j=1:N  
        d=sqrt((i-m)^2+(j-n)^2);
        if(d>=d0)
            h=1;
        else
            h=0;
        end
        result(i,j)=h*g(i,j);
    end
end
result=ifftshift(result);
J1=ifft2(result);
J2=uint8(real(J1));
figure,subplot(2,2,1);
imshow(lena);
title('原始图像');
subplot(2,2,2);
imshow(J2);
title('d0=15 理想高通滤波器');
%d0=30的理想高通滤波器
d0=30;
m=fix(M/2);
n=fix(N/2); 
for i=1:M  
    for j=1:N  
        d=sqrt((i-m)^2+(j-n)^2);
        if(d>=d0)
            h=1;
        else
            h=0;
        end
        result(i,j)=h*g(i,j);
    end
end
result=ifftshift(result);
J1=ifft2(result);
J2=uint8(real(J1));
subplot(2,2,3);
imshow(J2);
title('d0=30 理想高通滤波器');
%d0=80的理想高通滤波器
d0=80;
m=fix(M/2);
n=fix(N/2); 
for i=1:M  
    for j=1:N  
        d=sqrt((i-m)^2+(j-n)^2);
        if(d>=d0)
            h=1;
        else
            h=0;
        end
        result(i,j)=h*g(i,j);
    end
end
result=ifftshift(result);
J1=ifft2(result);
J2=uint8(real(J1));
subplot(2,2,4);
imshow(J2);
title('d0=80 理想高通滤波器');
%d0=15的巴特沃斯高通滤波器
nn=2;
d0=15;
m=fix(M/2);
n=fix(N/2);
for i=1:M
    for j=1:N
        d=sqrt((i-m)^2+(j-n)^2);
        if(d==0)
            h=0;
        else
            h=1/(1+0.414*(d0/d)^(2*nn));
        end
        result(i,j)=h*g(i,j);
    end
end
result=ifftshift(result);
J1=ifft2(result);
J2=uint8(real(J1));
figure,subplot(2,2,1);
imshow(lena);
title('原始图像');
subplot(2,2,2);
imshow(J2);
title('d0=15 巴特沃斯高通滤波')
%d0=30的巴特沃斯高通滤波器
d0=30;
m=fix(M/2);
n=fix(N/2);
for i=1:M
    for j=1:N
        d=sqrt((i-m)^2+(j-n)^2);
        if(d==0)
            h=0;
        else
            h=1/(1+0.414*(d0/d)^(2*nn));
        end
        result(i,j)=h*g(i,j);
    end
end
result=ifftshift(result);
J1=ifft2(result);
J2=uint8(real(J1));

subplot(2,2,3);
imshow(J2);
title('d0=30 巴特沃斯高通滤波')
%d0=80的巴特沃斯高通滤波器
d0=80;
m=fix(M/2);
n=fix(N/2);
for i=1:M
    for j=1:N
        d=sqrt((i-m)^2+(j-n)^2);
        if(d==0)
            h=0;
        else
            h=1/(1+0.414*(d0/d)^(2*nn));
        end
        result(i,j)=h*g(i,j);
    end
end
result=ifftshift(result);
J1=ifft2(result);
J2=uint8(real(J1));
subplot(2,2,4);
imshow(J2);
title('d0=80 巴特沃斯高通滤波')

Here is the quote

Guess you like

Origin blog.csdn.net/weixin_51571728/article/details/124713971
Recommended