均值滤波是指用当前像素点周围 N个像素值的均值来代替当前像素值,本质对领域内的矩阵求均值。它通常用于去除图像中高斯噪声。原理示意下图所示:
matlab代码实现:
clc;
clear all;
close all;
RGB_data = imread('G:\picture_deal\matlab_code\mangguo.bmp');%图像读入
[ROW,COL, DIM] = size(RGB_data); %提取图片的行列数
Y_data = zeros(ROW,COL);
Cb_data = zeros(ROW,COL);
Cr_data = zeros(ROW,COL);
Gray_data = RGB_data;
R_data = RGB_data(:,:,1);
G_data = RGB_data(:,:,2);
B_data = RGB_data(:,:,3);
for r = 1:ROW
for c = 1:COL
Y_data(r, c) = 0.299*R_data(r, c) + 0.587*G_data(r, c) + 0.114*B_data(r, c);
Cb_data(r, c) = -0.172*R_data(r, c) - 0.339*G_data(r, c) + 0.511*B_data(r, c) + 128;
Cr_data(r, c) = 0.511*R_data(r, c) - 0.428*G_data(r, c) - 0.083*B_data(r, c) + 128;
end
end
Gray_data(:,:,1)=Y_data;
Gray_data(:,:,2)=Y_data;
Gray_data(:,:,3)=Y_data;
figure(2);
imshow(Gray_data);
title('没有加高斯噪声的Y分量图像'); %显示滤波后的图象
%对原始图片加入高斯噪声
gaosi_data=imnoise(RGB_data,'gaussian');
R_data = gaosi_data(:,:,1);
G_data = gaosi_data(:,:,2);
B_data = gaosi_data(:,:,3);
for r = 1:ROW
for c = 1:COL
Y_data(r, c) = 0.299*R_data(r, c) + 0.587*G_data(r, c) + 0.114*B_data(r, c);
Cb_data(r, c) = -0.172*R_data(r, c) - 0.339*G_data(r, c) + 0.511*B_data(r, c) + 128;
Cr_data(r, c) = 0.511*R_data(r, c) - 0.428*G_data(r, c) - 0.083*B_data(r, c) + 128;
end
end
Gray_data(:,:,1)=Y_data;
Gray_data(:,:,2)=Y_data;
Gray_data(:,:,3)=Y_data;
figure(3);
imshow(Gray_data);
title('加高斯噪声的Y分量图像'); %显示滤波后的图象
%3*3均值滤波
A=fspecial('average',[3,3]);
Y=imfilter(Gray_data,A);
figure(4);
imshow(Y)
title('均值滤波后的Y分量图像'); %显示滤波后的图象
imwrite(gaosi_data,'mangguo_gaosi.bmp'); %保存图像为文件
与原图像对比,消除了部分高斯噪声,我们可以看到图像变得模糊了,缺失了很多的细节。这也是均值滤波的不足之处。