数字图像处理-Matlab实现图像的滤波

目录

1.空域高斯滤波器

2.中值滤波

3.频率域高斯滤波

4.滤波器测试脚本

5.测试结果


1.空域高斯滤波器

function [OutImage,ConvAarray] = FilterGauss(InputImage,Size)
%================================================================
% 功能:单通道图像高斯滤波器
% 参数:InputImage为输入单通道图像,Size为模板尺寸
% 返回值:OutImage为InputImage同维数组
% 主要思路:利用卷积核,模板对称,利用相关等于卷积
% 备注:如果图像为多通道则需要重复调用
% 调用方法:OutImage = FilterGauss(InputImage)
% 日期:2019.12.02
% 作者:Leetion
[iLimit,jLimit] = size(InputImage);
OutImage = InputImage;
switch (Size)
    case 3
        ConvAarray =double((1/16)*[1 2 1;2 4 2;1 2 1]);
        for yIndex = 2:iLimit-1
            for xIndex = 2:jLimit-1
                NeighbourArray = [InputImage(yIndex-1,xIndex-1) InputImage(yIndex-1,xIndex) InputImage(yIndex-1,xIndex+1);...
                    InputImage(yIndex,xIndex-1) InputImage(yIndex,xIndex) InputImage(yIndex,xIndex+1);...
                    InputImage(yIndex+1,xIndex-1) InputImage(yIndex+1,xIndex) InputImage(yIndex+1,xIndex+1)];
                OutImage(yIndex,xIndex) = uint8(sum(sum(ConvAarray.*double(NeighbourArray))));
            end
        end
    case 5
        ConvAarray =double((1/90)*[1 2 4 2 1;2 4 8 4 2;4 8 16 8 4;2 4 8 4 2; 1 2 4 2 1]);
        for yIndex = 3:iLimit-2
            for xIndex = 3:jLimit-2
                NeighbourArray = [InputImage(yIndex-2,xIndex-2) InputImage(yIndex-2,xIndex-1) InputImage(yIndex-2,xIndex) InputImage(yIndex-2,xIndex+1) InputImage(yIndex-2,xIndex+2);...
                                  InputImage(yIndex-1,xIndex-2) InputImage(yIndex-1,xIndex-1) InputImage(yIndex-1,xIndex) InputImage(yIndex-1,xIndex+1) InputImage(yIndex-1,xIndex+2);...
                                  InputImage(yIndex,xIndex-2) InputImage(yIndex,xIndex-1) InputImage(yIndex,xIndex) InputImage(yIndex,xIndex+1) InputImage(yIndex,xIndex+2);...
                                  InputImage(yIndex+1,xIndex-2) InputImage(yIndex+1,xIndex-1) InputImage(yIndex+1,xIndex) InputImage(yIndex+1,xIndex+1) InputImage(yIndex+1,xIndex+2);...
                                  InputImage(yIndex+2,xIndex-2) InputImage(yIndex+2,xIndex-1) InputImage(yIndex+2,xIndex) InputImage(yIndex+2,xIndex+1) InputImage(yIndex+2,xIndex+2)];
                OutImage(yIndex,xIndex) = uint8(sum(sum(ConvAarray.*double(NeighbourArray))));
            end
        end
    otherwise
        warning("只支持三阶和五阶");
end
end

2.中值滤波

function OutImage = FilterMiddle(InputImage,Size)
%================================================================
% 功能:单通道图像中值滤波器
% 参数:InputImage为输入单通道图像,Size为邻域尺寸
% 返回值:OutImage为InputImage同维数组
% 主要思路:求解邻域中值
% 备注:如果图像为多通道则需要重复调用
% 调用方法:OutImage = FilterMiddle(InputImage)
% 日期:2019.12.02
% 作者:Leetion
[iLimit,jLimit] = size(InputImage);
OutImage = InputImage;
if Size(1)==1 && Size(2)==3
    flag = 1;
elseif Size(1)==1 && Size(2)==5
    flag = 2;
elseif Size(1)==3 && Size(2)==1
    flag = 3;
elseif Size(1)==5 && Size(2)==1
    flag = 4;
elseif Size(1)==3 && Size(2)==3
    flag = 5;
elseif Size(1)==5 && Size(2)==5
    flag = 6;
else
    warning("不支持的尺寸")
end
switch(flag)
    case 1
        for yIndex = 1:iLimit
            for xIndex = 2:jLimit-1
                NeighbourArray = [InputImage(yIndex,xIndex-1) InputImage(yIndex,xIndex) InputImage(yIndex,xIndex+1)];
                OutImage(yIndex,xIndex) = median(NeighbourArray);
            end
        end
    case 2
        for yIndex = 1:iLimit
            for xIndex = 3:jLimit-2
                NeighbourArray = [InputImage(yIndex,xIndex-2) InputImage(yIndex,xIndex-1) InputImage(yIndex,xIndex) InputImage(yIndex,xIndex+1) InputImage(yIndex,xIndex+2)];
                OutImage(yIndex,xIndex) = median(NeighbourArray);
            end
        end
    case 3
        for yIndex = 2:iLimit-1
            for xIndex = 1:jLimit
                NeighbourArray = [InputImage(yIndex-1,xIndex),InputImage(yIndex,xIndex),InputImage(yIndex+1,xIndex)];
                OutImage(yIndex,xIndex) = median(NeighbourArray);
            end
        end
    case 4
        for yIndex = 3:iLimit-2
            for xIndex = 1:jLimit
                NeighbourArray = [InputImage(yIndex-2,xIndex) InputImage(yIndex-1,xIndex),InputImage(yIndex,xIndex),InputImage(yIndex+1,xIndex) InputImage(yIndex+2,xIndex)];
                OutImage(yIndex,xIndex) = median(NeighbourArray);
            end
        end
    case 5
        for yIndex = 2:iLimit-1
            for xIndex = 2:jLimit-1
                NeighbourArray = [InputImage(yIndex-1,xIndex-1) InputImage(yIndex-1,xIndex) InputImage(yIndex-1,xIndex+1)...
                    InputImage(yIndex,xIndex-1) InputImage(yIndex,xIndex) InputImage(yIndex,xIndex+1)...
                    InputImage(yIndex+1,xIndex-1) InputImage(yIndex+1,xIndex) InputImage(yIndex+1,xIndex+1)];
                OutImage(yIndex,xIndex) = median(NeighbourArray);
            end
        end
    case 6
        for yIndex = 3:iLimit-2
            for xIndex = 3:jLimit-2
                NeighbourArray = [InputImage(yIndex-2,xIndex-2) InputImage(yIndex-2,xIndex-1) InputImage(yIndex-2,xIndex) InputImage(yIndex-2,xIndex+1) InputImage(yIndex-2,xIndex+2)...
                    InputImage(yIndex-1,xIndex-2) InputImage(yIndex-1,xIndex-1) InputImage(yIndex-1,xIndex) InputImage(yIndex-1,xIndex+1) InputImage(yIndex-1,xIndex+2)...
                    InputImage(yIndex,xIndex-2) InputImage(yIndex,xIndex-1) InputImage(yIndex,xIndex) InputImage(yIndex,xIndex+1) InputImage(yIndex,xIndex+2)...
                    InputImage(yIndex+1,xIndex-2) InputImage(yIndex+1,xIndex-1) InputImage(yIndex+1,xIndex) InputImage(yIndex+1,xIndex+1) InputImage(yIndex+1,xIndex+2)...
                    InputImage(yIndex+2,xIndex-2) InputImage(yIndex+2,xIndex-1) InputImage(yIndex+2,xIndex) InputImage(yIndex+2,xIndex+1) InputImage(yIndex+2,xIndex+2)];
                OutImage(yIndex,xIndex) = median(NeighbourArray);
            end
        end
end
end

3.频率域高斯滤波

function OutImage = FreFilterGauss(InputImage,D0)
%================================================================
% 功能:频域高斯滤波
% 参数:InputImage为输入单通道图像,D0为截止频率
% 返回值:OutImage为输出图像
% 主要思路:FFT转换到频率域,频率域乘积高斯传函,再IFFT2到时域
% 备注:使用库函数计算FFT和IFFT
% 调用方法:OutImage = FreFilterGauss(InputImage,D0)
% 日期:2019.12.02
% 作者:Leetion
[iLimit,jLimit] = size(InputImage);
FreInputImage = fft2(InputImage);
for vIndex = 1:iLimit
    for uIndex = 1:jLimit
        Distance = sqrt((vIndex-(1+iLimit)/2)^2+(uIndex-(1+jLimit)/2)^2);
        H = exp(-(Distance^2/(2*D0^2)));
        FreOutImage(vIndex,uIndex) = H*FreInputImage(vIndex,uIndex);
    end
end
OutImage = uint8(ifft2(FreOutImage));
end

4.滤波器测试脚本

%================================================================
% 功能:滤波器测试
% 主要思路:
% 备注:
% 日期:2019.11.29
% 作者:leetion
clc,clear
close all
InputImage = imread('eight.tif'); 
figure(1)
subplot(2,3,1);
imshow(InputImage);
title("原图");
InputImage = imnoise(InputImage,'gaussian',0.001,0.001);
% InputImage = imnoise(InputImage,'salt & pepper');
subplot(2,3,2);
imshow(InputImage);
title("Gaussian Noise");
OutImage = FilterGauss(InputImage,3);
subplot(2,3,3);
imshow(OutImage);
title("Gaussian Filter");
subplot(2,3,4);
OutImage = FilterMiddle(InputImage,[5,5]);
imshow(OutImage);
title("Middle Filter");
subplot(2,3,5);
OutImage = FreFilterGauss(InputImage,1500);
imshow(OutImage);
title("FreGauss Filter");
figure(2)
subplot(2,3,1);
imshow(InputImage);
title("原图");
InputImage = imnoise(InputImage,'salt & pepper');
subplot(2,3,2);
imshow(InputImage);
title("Gaussian Noise");
OutImage = FilterGauss(InputImage,3);
subplot(2,3,3);
imshow(OutImage);
title("Gaussian Filter");
subplot(2,3,4);
OutImage = FilterMiddle(InputImage,[5,5]);
imshow(OutImage);
title("Middle Filter");
subplot(2,3,5);
OutImage = FreFilterGauss(InputImage,1500);
imshow(OutImage);
title("FreGauss Filter");

5.测试结果

高斯白噪声
椒盐噪声

  • 1
    点赞
  • 45
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值