手写图像滤波与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
手写图像滤波与Matlab图像滤波对比(作业向)_第1张图片

你可能感兴趣的:(计算机视觉)