matalb image processing low-pass filtering and high-pass filtering (ideal, Butterworth, Gaussian including code)

Main types and formulas

1. Low-pass filtering
is mainly divided into ideal low-pass filtering, Butterworth low-pass filtering, and Gaussian low-pass filtering
Ideal low-pass filtering Filtering:
![Insert image description here](https://img-blog.csdnimg.cn/897ae174b49b4f24a1c1aaa85cf414fe.png
Among them: for an image of size M*N, the distance between the frequency point (u, v) and the center of the frequency domain is D (u, v), and its expression is:

The following D(u, v) are all the same

Butterworth low pass filter:
Insert image description here

Gaussian low-pass filter:
Insert image description here

2. High-pass filtering
Ideal high-pass filtering:
Insert image description here

Butterworth high-pass filter:
Insert image description here

Gaussian high-pass filter:
Insert image description here

Main renderings

With Fourier spectrum:
Insert image description here
Without Fourier spectrum:
Insert image description here

Various types of function codes

Ideal low pass filter:

function out = imidealflpf(I, freq)
% imidealflpf函数	构造理想的频域低通滤波器
% 参数:I			输入的灰度图像
% 参数:freq			低通滤波器的截止频率
% 返回值:out        指定的理想低通滤波器

[M,N] = size(I);
out = ones(M,N);
for i=1:M
    for j=1:N
        if (sqrt(((i-M/2)^2+(j-N/2)^2))>freq)
            out(i,j)=0;
        end
    end
end

Ideal high pass filter:

function out = imidealflpf1(I, freq)
% imidealflpf函数	构造理想的频域低通滤波器
% 参数:I			输入的灰度图像
% 参数:freq			低通滤波器的截止频率
% 返回值:out        指定的理想低通滤波器

[M,N] = size(I);
out = ones(M,N);
for i=1:M
    for j=1:N
        if (sqrt(((i-M/2)^2+(j-N/2)^2))<freq)
            out(i,j)=0;
        end
    end
end

Butterwall low pass filter:

function out = imgaussflpf4(I,sigma,x)
% imgaussflpf函数   	构造频域高斯低通滤波器
% 参数:I				输入的灰度图像
% 参数:sigma			限制图像D0参数
% 参数:x				巴特沃尔的阶数

[M,N] = size(I);
out = ones(M,N);
for i=1:M
    for j=1:N
        distance=sqrt((i-M/2)^2+(j-N/2)^2);
        out(i,j) = 1/(1+(distance/sigma).^(2.*x));
    end
end

Butterwall high-pass filter:

function out = imgaussflpf5(I, sigma,n)
% imgaussflpf函数   	构造频域高斯低通滤波器
% 参数:I				输入的灰度图像
% 参数:sigma			高斯函数的Sigma参数

[M,N] = size(I);
out = ones(M,N);
for i=1:M
    for j=1:N
        out(i,j) = 1/(1+((sigma/((i-M/2).^2+(j-N/2).^2))).^(2.*n));
    end
end

Gaussian low-pass filter:

function out = imgaussflpf2(I, sigma)
% imgaussflpf函数   	构造频域高斯低通滤波器
% 参数:I				输入的灰度图像
% 参数:sigma			高斯函数的Sigma参数

[M,N] = size(I);
out = ones(M,N);
for i=1:M
    for j=1:N
        out(i,j) = exp(-((i-M/2)^2+(j-N/2)^2)/2/sigma^2);
    end
end

Gaussian high-pass filter:

function out = imgaussflpf3(I, sigma)
% imgaussflpf函数   	构造频域高斯低通滤波器
% 参数:I				输入的灰度图像
% 参数:sigma			高斯函数的Sigma参数

[M,N] = size(I);
out = ones(M,N);
for i=1:M
    for j=1:N
        out(i,j) = 1-exp(-((i-M/2)^2+(j-N/2)^2)/2/sigma^2);
    end
end

Function that corresponds to images and outputs:

function out = imfreqfilt(I, ff)
% imfreqfilt函数		对灰度图像进行频域滤波
% 参数:I				输入的空域图像
% 参数:ff				应用的与原图像等大的频域滤波
% 返回值:out           反变换回空域后的图像

if (ndims(I)==3) && (size(I,3)==3)   % RGB图像
    I = rgb2gray(I);
    I = double(I);
end

if (size(I) ~= size(ff))
    msg1 = sprintf('%s: 滤镜与原图像不等大,检查输入', mfilename);
    msg2 = sprintf('%s: 滤波操作已经取消', mfilename);
    eid = sprintf('Images:%s:ImageSizeNotEqual',mfilename);
    error(eid,'%s %s',msg1,msg2);
end

% 快速傅立叶变换
f = fft2(double(I));

% 移动原点
s = fftshift(f);

% 应用滤镜及反变换
out = s .* ff; %对应元素相乘实现频域滤波
out = ifftshift(out);
out = ifft2(out);

% 求模值
out = abs(out);

% 归一化以便显示
out = out/max(out(:));

Final comparison code

%低通|高通频率域滤波
f=imread('8.jpeg');%原图
f=im2double(f);
f1=rgb2gray(f);
%低通理想滤波
dt_L=imidealflpf(f1,40);%低通 理想40滤波器
dtL=imfreqfilt(f1,dt_L);%带入滤波器输出
dtL1=fft2(dtL);%傅里叶变换
sdtL=fftshift(log(1+abs(dtL1)));% 绘制傅里叶变换对数谱并将零点移到中心
%高通理想滤波
gt_L=imidealflpf1(f1,60);%高通 理想60滤波器
gtL=imfreqfilt(f1,gt_L);%带入滤波器输出
gtL1=fft2(gtL);%傅里叶变换
sgtL=fftshift(log(1+abs(gtL1)));% 绘制傅里叶变换对数谱并将零点移到中心
%低通巴特沃斯
dt_B=imgaussflpf4(f1,40,2);%巴特沃斯
dtB=imfreqfilt(f1,dt_B);%带入滤波器输出
dtB1=fft2(dtB);%傅里叶变换
sdtB=fftshift(log(1+abs(dtB1)));% 绘制傅里叶变换对数谱并将零点移到中心
%高通巴特沃斯
gt_B=imgaussflpf5(f1,60,2);%巴特沃斯
gtB=imfreqfilt(f1,gt_B);%带入滤波器输出
gtB1=fft2(gtB);%傅里叶变换
sgtB=fftshift(log(1+abs(gtB1)));% 绘制傅里叶变换对数谱并将零点移到中心
%低通高斯滤波
dt_G=imgaussflpf2(f1,40);
dtG=imfreqfilt(f1,dt_G);%带入滤波器输出
dtG1=fft2(dtG);%傅里叶变换
sdtG=fftshift(log(1+abs(dtG1)));% 绘制傅里叶变换对数谱并将零点移到中心
%高通高斯滤波
gt_G=imgaussflpf3(f1,60);
gtG=imfreqfilt(f1,gt_G);%带入滤波器输出
gtG1=fft2(gtG);%傅里叶变换
sgtG=fftshift(log(1+abs(gtG1)));% 绘制傅里叶变换对数谱并将零点移到中心
%输出不带傅里叶谱
% subplot(3,3,1);imshow(f1);title('原图');
% subplot(3,3,4);imshow(dtL);title('低通理想滤波处理后 D0=40');
% subplot(3,3,5);imshow(dtB);title('低通巴特沃斯滤波处理后 D0=40 二阶');
% subplot(3,3,6);imshow(dtG);title('低通高斯滤波处理后 D0=40 ');
% subplot(3,3,7);imshow(gtL);title('高通理想滤波处理后 D0=60');
% subplot(3,3,8);imshow(gtB);title('高通巴特沃斯滤波处理后 D0=60 二阶');
% subplot(3,3,9);imshow(gtG);title('高通高斯滤波处理后 D0=60');
%输出带傅里叶谱
subplot(5,3,1);imshow(f1);title('原图');
subplot(5,3,4);imshow(dtL);title('低通理想滤波处理后 D0=40');
subplot(5,3,5);imshow(dtB);title('低通巴特沃斯滤波处理后 D0=40 二阶');
subplot(5,3,6);imshow(dtG);title('低通高斯滤波处理后 D0=40 ');
subplot(5,3,7);imshow(sdtL,[]);title('低通理想滤波处理后 D0=40 傅里叶谱');
subplot(5,3,8);imshow(sdtB,[]);title('低通巴特沃斯滤波处理后 D0=40 二阶 傅里叶谱');
subplot(5,3,9);imshow(sdtG,[]);title('低通高斯滤波处理后 D0=40 傅里叶谱');
subplot(5,3,10);imshow(gtL);title('高通理想滤波处理后 D0=60');
subplot(5,3,11);imshow(gtB);title('高通巴特沃斯滤波处理后 D0=60 二阶');
subplot(5,3,12);imshow(gtG);title('高通高斯滤波处理后 D0=60');
subplot(5,3,13);imshow(sgtL,[]);title('高通理想滤波处理后 D0=60 傅里叶谱');
subplot(5,3,14);imshow(sgtB,[]);title('高通巴特沃斯滤波处理后 D0=60 二阶 傅里叶谱');
subplot(5,3,15);imshow(sgtG,[]);title('高通高斯滤波处理后 D0=60 傅里叶谱');

Guess you like

Origin blog.csdn.net/joreng/article/details/128024235