天天看點

數字圖像處理實驗(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'); 

           

實驗效果

數字圖像處理實驗(2)——中值濾波/均值濾波

原圖

數字圖像處理實驗(2)——中值濾波/均值濾波

第一排是中值濾波,第二排是均值濾波,

從左到右是3X3,5X5,7X7,9X9模闆大小

數字圖像處理實驗(2)——中值濾波/均值濾波
數字圖像處理實驗(2)——中值濾波/均值濾波

從左到右分别是Sobel,Prewitt,Roberts算子

數字圖像處理實驗(2)——中值濾波/均值濾波

繼續閱讀