MATLAB中使用均值、中值、prewitt滤波器的几个例子
clc;clear; close all
%% 盒式滤波器
img = imread('lena256.bmp');%图像读取
img = rgb2gray(img);%转换为灰度图
h1 = fspecial('average',3); %3×3卷积核
img_filted1 = imfilter(img,h1);
h2 = fspecial('average',11); %11×11卷积核
img_filted2= imfilter(img,h2);
h3 = fspecial('average',21); %21×21卷积核
img_filted3 = imfilter(img,h3);
%显示原图及结果
figure,
subplot(221),
imshow(img,[]);
title('原图');
subplot(222),
imshow(img_filted1,[]);
title('3×3盒式滤波结果');
subplot(223),
imshow(img_filted2,[]);
title('11×11盒式滤波结果');
subplot(224),
imshow(img_filted3,[]);
title('21×21盒式滤波结果');
%% 中值滤波
img_medfilt = medfilt2(img);
figure,
subplot(121),
imshow(img,[]);
title('原图');
subplot(122);
imshow(img_medfilt,[]),
title('中值滤波结果');
%% Prewitt滤波
h_p1 = fspecial('prewitt');
h_p2 = h_p1';
img_prefilt1 = imfilter(img,h_p1);
img_prefilt2 = imfilter(img,h_p2);
figure,
subplot(131),
imshow(img,[]);
title('原图');
subplot(132),
imshow(img_prefilt1,[]);
title('prewitt滤波(水平)');
subplot(133),
imshow(img_prefilt2,[]);
title('prewitt滤波(垂直)');