matlab—中值濾波
matlab自帶函數:
%%matlab中自帶的函數
clc
clear
I=im2double(imread('17.jpg'));
figure;imshow(I);title("原圖");
[height,width,channel]=size(I);
I1=imnoise(I,'salt & pepper');
figure;imshow(I1);title("添加椒鹽噪聲圖像圖");
R=medfilt2(I1(:,:,1),[5,5]);
G=medfilt2(I1(:,:,2),[10,10]);
B=medfilt2(I1(:,:,3),[10,10]);
medfout=cat(3,R,G,B);
figure;imshow(medfout);title("中值濾波圖像");
原圖
matlab自己編寫程序
%%
%中值濾波圖像
clc
clear
I=im2double(imread('17.jpg'));
figure;imshow(I);title("原圖");
n=3;
[height,width,channel]=size(I);
I1=imnoise(I,'salt & pepper');
figure;imshow(I1);title("添加椒鹽噪聲圖像圖");
for i=1:height-n+1
for j=1:width-n+1
for k=1:3
c=I1(i:i+(n-1),j:j+(n-1),k);
e=c(1,:);
for u=2:n
e=[e,c(u,:)];
end
Med=median(e);
I1(i+(n-1)/2,j+(n-1)/2,k)=Med;
end
end
end
I1= imresize(I1,[height,width]);
figure;imshow(I1);title("中值濾波圖像");