MATLAB實現中值濾波演算法
阿新 • • 發佈:2019-02-05
x = imread('C:\Users\Administrator\Desktop\im7.jpg');
x = rgb2gray(x);
[m, n] = size(x); %m表示行數(即高度);n表示列數(即寬度)
x = imnoise(x,'salt & pepper',0.02);
subplot(1,2,1)
imshow(x);
title('原圖');
x1 = double(x);
x2 = x1;
%% --------------------------------%
% 此處的演算法缺點是未對邊界值做出改變
for i = 1:m-3+1
for j = 1:n-3+1
mb = x1( i:(i+3-1), j:(j+3-1) ); %取出模板元素
mb = mb(:); %矩陣中的元素以一列輸出
mm = median(mb);%取向量的中值
x2( i+(3-1)/2, j+(3-1)/2 ) = mm; %對中心元素賦值
end
end
%% --------------------------------%
dstImage = uint8(x2);
subplot(1,2,2)
imshow(dstImage);
title('效果圖');
x = rgb2gray(x);
[m, n] = size(x); %m表示行數(即高度);n表示列數(即寬度)
x = imnoise(x,'salt & pepper',0.02);
subplot(1,2,1)
imshow(x);
title('原圖');
x1 = double(x);
x2 = x1;
%% --------------------------------%
% 此處的演算法缺點是未對邊界值做出改變
for i = 1:m-3+1
for j = 1:n-3+1
mb = x1( i:(i+3-1), j:(j+3-1) ); %取出模板元素
mb = mb(:); %矩陣中的元素以一列輸出
mm = median(mb);%取向量的中值
x2( i+(3-1)/2, j+(3-1)/2 ) = mm; %對中心元素賦值
end
end
%% --------------------------------%
dstImage = uint8(x2);
subplot(1,2,2)
imshow(dstImage);
title('效果圖');