手寫圖像濾波與Matlab圖像濾波對比(作業向)

close all
clear
I=imread("noiseimg.bmp");%讀取圖片
P = rgb2gray(I);%灰度化

fprintf('處理中....')
w1=fspecial('average',[3 3]);
w2=fspecial('average',[5 5]);
w3=fspecial('gaussian',[3 3]);
w4=fspecial('gaussian',[3 3]);
w={w1,w2,w3,w4};
fprintf('處理中....')
for i=1:4
my(:,:,i)=my_filter(P,cell2mat(w(i)));
ma(:,:,i)=imadjust(imfilter(P,cell2mat(w(i))));
fprintf('處理中....')
end

my(:,:,5)=my_median(P,3);
fprintf('處理中....')
my(:,:,6)=my_median(P,5);
fprintf('處理中....')
ma(:,:,5)=imadjust(medfilt2(P,[3 2]));
fprintf('處理中....')
ma(:,:,6)=imadjust(medfilt2(P,[5 5]));
fprintf('處理中....\n')

fprintf('開始顯示')
figure 
imshow(I)
title('原圖')
pause(1)
% set(gcf,'position',[0 330 400 350]);
close

figure 
imshow(P)
title('灰度化後')
pause(1)
% set(gcf,'position',[400 330 400 350]);
close
a={'Matlab的3*3均值濾波後',
    '我的3*3均值濾波後',
   'Matlab的5*5均值濾波後',
     '我的5*5均值濾波後',
   'Matlab的3*3高斯濾波後',
  '我的3*3高斯濾波後',
   'Matlab的5*5高斯濾波後',
'我的5*5高斯濾波後',
   'Matlab的3*3中值濾波後',
'我的3*3中值濾波後',
   'Matlab的5*5中值濾波後',
'我的5*5中值濾波後'};
for i=1:6
figure 
set(gcf,'position',[200 150 400 350]);
imshow(ma(:,:,i))
title(char(a(2*(i-1)+1)))
figure 
set(gcf,'position',[900 150 400 350]);
imshow(my(:,:,i))
title(char(a(2*i)))
pause(2)
close all
end
subplot(351),imshow(I),title('原圖')
subplot(352),imshow(P),title('灰度化後')
for i=1:6
subplot(3,5,2+2*(i-1)+1),imshow(ma(:,:,i)),title(char(a(2*(i-1)+1)))
subplot(3,5,2+2*i),imshow(my(:,:,i)),title(char(a(2*i)))
end
function [image] = my_median(inputimage,n)
[height, width] = size(inputimage);
x1 = (inputimage);
x2 = x1;
    for i = 1: height-n+1
        for j = 1:width-n+1
            mb = x1( i:(i+n-1),  j:(j+n-1) );
            mb = mb(:);
            mm = median(mb);
            x2( i+(n-1)/2,  j+(n-1)/2 ) = mm;
        end
    end
    image=x2;
    image =imadjust( image);
end
function image = my_filter(inputimage,filter)
[n, ~] = size(filter);
template = filter;
[height, width] = size(inputimage);
x1 = double(inputimage);
x2 = x1;
for i = 1:height-n+1
    for j = 1:width-n+1
        c = x1(i:i+n-1,j:j+n-1).*template;
        s = sum(sum(c));
        x2(i+(n-1)/2,j+(n-1)/2) = s;
    end
end
image=uint8(x2);
 image =imadjust( image);
end

nosieimg.bmp
在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章