数字图像处理实验(2)——中值滤波/均值滤波

实验题目:

编程实现灰度图像的中值滤波平滑处理。滤波模板的大小自定(可为3×3、5×5、7×7、15×15等)。实验图像可从提供的实验图像集中的噪声图像中选取。
思考题:(选做)
编程实现灰度图像的均值滤波平滑处理;也可尝试实现灰度图像的锐化处理,包括Sobel、Prewitt、Roberts、Laplace、Canny边缘检测等。

源码:

均值滤波

function ava(n)
imNew=im2double(imread('noise.jpg'));
len=floor(n/2);
a(1:n,1:n)=1;   %a即n×n模板,元素全是1
%对原始图像进行扩展,复制边界的值
imNew_pad=padarray(imNew,[len,len],'symmetric');
[M,N]=size(imNew_pad);
New=zeros(size(imNew));
for i=1+len:M-len
    for j=1+len:N-len
        c=imNew_pad(i-(n-1)/2:i+(n-1)/2,j-(n-1)/2:j+(n-1)/2).*a; %取出x1中从(i,j)开始的n行n列元素与模板相乘  
        s=sum(sum(c));                 %求c矩阵中各元素之和  
        New(i-(n-1)/2,j-(n-1)/2)=s/(n*n); %将与模板运算后的各元素的均值赋给模板中心位
    end
end
figure('toolbar','none','menubar','none');
set (gca,'position',[0.05,0.03,0.90,0.90] );
imshow(New);
title(['灰度图像的均值滤波平滑处理',num2str(n),'X',num2str(n),'图像']);
end

中值滤波

// 中值函数
function mid=mid(A)
len=length(A);
for i = 1:len
    for j = 1:len-i
        if A(j) > A(j+1)
            temp = A(j);       %核心代码
            A(j) = A(j+1);
            A(j+1) = temp;
        end
    end
end
mid=A(floor(len/2)+1);
end

//中值滤波 
function mido(bianSize)
imNew=im2double(imread('noise.jpg'));
%扩展区域的行列数,floor取整
len=floor(bianSize/2);
%对原始图像进行扩展,没有参数默认0填充
imNew_pad=padarray(imNew,[len,len]);
[M,N]=size(imNew_pad);
New=zeros(size(imNew));
for i=1+len:M-len
    for j=1+len:N-len
        %从扩展图像中,取出局部图像
        Block=imNew_pad(i-len:i+len,j-len:j+len);
        %将多维矩阵转换为一维数组
        Block=Block(:);
        %取这组数的中值,赋值给输出图像        
        New(i-len,j-len)=mid(Block);
    end
end
figure('toolbar','none','menubar','none');
set (gca,'position',[0.05,0.03,0.90,0.90] );
imshow(New);
title(['灰度图像的中值滤波平滑处理',num2str(bianSize),'X',num2str(bianSize),'图像']);
end

Prewitt算子

function Prewitt
imNew=im2double(imread('锐化及边缘检测用途.jpg'));
[M , N, R]=size(imNew);
for i=2:M-1
    for j=2:N-1
        Dx=[imNew(i+1,j-1)-imNew(i-1,j-1)]+[imNew(i+1,j)-imNew(i-1,j)]+[imNew(i+1,j+1)-imNew(i-1,j+1)];
        Dy=[imNew(i-1,j+1)-imNew(i-1,j-1)]+[imNew(i,j+1)-imNew(i,j-1)]+[imNew(i+1,j+1)-imNew(i+1,j-1)];
       P(i,j)=sqrt(Dx^2+Dy^2);
     
    end
end
for i=1:M-1
    for j=1:N-1
        if (P(i,j)<0.5)
            P(i,j)=1;
        else P(i,j)=0;
        end
    end
end
figure('toolbar','none','menubar','none');
set (gca,'position',[0.05,0.03,0.90,0.90] );
imshow(P,[]);
title('Prewitt边缘检测');  %画出边缘检测后的图像
end

Roberts算子

function Roberts
imNew=im2double(imread('锐化及边缘检测用途.jpg'));
[M,N]=size(imNew);
newimNew=imNew;%为保留图像的边缘一个像素
for j=1:M-1 %进行边界提取
    for k=1:N-1
        robertsNum = abs(imNew(j,k)-imNew(j+1,k+1)) + abs(imNew(j+1,k)-imNew(j,k+1));
        newimNew(j,k)=robertsNum;
    end
end
figure('toolbar','none','menubar','none');
set (gca,'position',[0.05,0.03,0.90,0.90] );
imshow(newimNew);
title('Roberts边缘检测');  %画出边缘检测后的图像
end


Sobel算子

function Sobel
imNew=im2double(imread('锐化及边缘检测用途.jpg'));
[M,N] = size(imNew);   % 获得图像的高度和宽度
F2 = double(imNew);        
U = double(imNew);       
uSobel = imNew;
for i = 2:M - 1   %sobel边缘检测
    for j = 2:N - 1
        Gx = (U(i+1,j-1) + 2*U(i+1,j) + F2(i+1,j+1)) - (U(i-1,j-1) + 2*U(i-1,j) + F2(i-1,j+1));
        Gy = (U(i-1,j+1) + 2*U(i,j+1) + F2(i+1,j+1)) - (U(i-1,j-1) + 2*U(i,j-1) + F2(i+1,j-1));
        uSobel(i,j) = sqrt(Gx^2 + Gy^2); 
    end
end 
figure('toolbar','none','menubar','none');
set (gca,'position',[0.05,0.03,0.90,0.90] );
imshow(im2uint8(uSobel));
title('Sobel边缘检测');  %画出边缘检测后的图像
end

交互面板

function varargout = mmm2(varargin)
% MMM2 MATLAB code for mmm2.fig
%      MMM2, by itself, creates a new MMM2 or raises the existing
%      singleton*.
%
%      H = MMM2 returns the handle to a new MMM2 or the handle to
%      the existing singleton*.
%
%      MMM2('CALLBACK',hObject,eventData,handles,...) calls the local
%      function named CALLBACK in MMM2.M with the given input arguments.
%
%      MMM2('Property','Value',...) creates a new MMM2 or raises the
%      existing singleton*.  Starting from the left, property value pairs are
%      applied to the GUI before mmm2_OpeningFcn gets called.  An
%      unrecognized property name or invalid value makes property application
%      stop.  All inputs are passed to mmm2_OpeningFcn via varargin.
%
%      *See GUI Options on GUIDE's Tools menu.  Choose "GUI allows only one
%      instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES

% Edit the above text to modify the response to help mmm2

% Last Modified by GUIDE v2.5 23-Mar-2020 23:58:07

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @mmm2_OpeningFcn, ...
                   'gui_OutputFcn',  @mmm2_OutputFcn, ...
                   'gui_LayoutFcn',  [] , ...
                   'gui_Callback',   []);
if nargin && ischar(varargin{1})
    gui_State.gui_Callback = str2func(varargin{1});
end

if nargout
    [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
    gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT


% --- Executes just before mmm2 is made visible.
function mmm2_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
% varargin   command line arguments to mmm2 (see VARARGIN)

% Choose default command line output for mmm2
handles.output = hObject;

% Update handles structure
guidata(hObject, handles);

% UIWAIT makes mmm2 wait for user response (see UIRESUME)
% uiwait(handles.figure1);


% --- Outputs from this function are returned to the command line.
function varargout = mmm2_OutputFcn(hObject, eventdata, handles) 
% varargout  cell array for returning output args (see VARARGOUT);
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Get default command line output from handles structure
varargout{1} = handles.output;


% --- Executes on button press in pushbutton1.
function pushbutton1_Callback(hObject, eventdata, handles)
clear;
mido(3)
% --- Executes on button press in pushbutton2.
function pushbutton2_Callback(hObject, eventdata, handles)
clear;
mido(5)
% --- Executes on button press in pushbutton3.
function pushbutton3_Callback(hObject, eventdata, handles)
clear;
mido(7);
% --- Executes on button press in pushbutton4.
function pushbutton4_Callback(hObject, eventdata, handles)
clear;
mido(9)
% --- Executes on button press in pushbutton7.
function pushbutton7_Callback(hObject, eventdata, handles)
clear;
ava(3)
% --- Executes on button press in pushbutton8.
function pushbutton8_Callback(hObject, eventdata, handles)
clear;
ava(5)
% --- Executes on button press in pushbutton9.
function pushbutton9_Callback(hObject, eventdata, handles)
clear;
ava(7)
% --- Executes on button press in pushbutton10.
function pushbutton10_Callback(hObject, eventdata, handles)
clear;
ava(9)
% --- Executes on button press in pushbutton12.
function pushbutton12_Callback(hObject, eventdata, handles)
clear;
Sobel();
% --- Executes on button press in pushbutton13.
function pushbutton13_Callback(hObject, eventdata, handles)
clear;
Prewitt();
% --- Executes on button press in pushbutton14.
function pushbutton14_Callback(hObject, eventdata, handles)
clear;
Roberts();
% --- Executes on button press in pushbutton16.
function pushbutton16_Callback(hObject, eventdata, handles)
clear;
imNew=im2double(imread('锐化及边缘检测用途.jpg'));
figure('toolbar','none','menubar','none');
set (gca,'position',[0.05,0.03,0.90,0.90] );
imshow(imNew);
title('原图2'); 
% --- Executes on button press in pushbutton17.
function pushbutton17_Callback(hObject, eventdata, handles)
clear;
imNew=im2double(imread('noise.jpg'));
figure('toolbar','none','menubar','none');
set (gca,'position',[0.05,0.03,0.90,0.90] );
imshow(imNew);
title('原图1'); 

实验效果

在这里插入图片描述
原图
椒盐噪声

第一排是中值滤波,第二排是均值滤波,
从左到右是3X3,5X5,7X7,9X9模板大小
在这里插入图片描述

在这里插入图片描述
从左到右分别是Sobel,Prewitt,Roberts算子
在这里插入图片描述

发布了8 篇原创文章 · 获赞 5 · 访问量 152

猜你喜欢

转载自blog.csdn.net/qq_41650371/article/details/105362182