?中值滤波是一种典型的非线性滤波,是基于排序统计理论的一种能够有效抑制噪声的非线性信号处理技术,基本思想是用像素点邻域灰度值的中值来代替该像素点的灰度值,让周围的像素值接近真实的值从而消除孤立的噪声点。该方法在去除脉冲噪声、椒盐噪声的同时能保留图像的边缘细节
中值滤波示意如下图所示
?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(1);
imshow(Gray_data);
title('没有加椒盐噪声的Y分量原始图像');
%对原始图片加入椒盐噪声
salt_data=imnoise(RGB_data,'salt & pepper',0.01);
R_data = salt_data(:,:,1);
G_data = salt_data(:,:,2);
B_data = salt_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分量图像');
%中值滤波
Y=medfilt2(Y_data);
Gray_data(:,:,1)=Y;
Gray_data(:,:,2)=Y;
Gray_data(:,:,3)=Y;
figure(3);
imshow(Gray_data);
title('中值滤波后的Y分量图像');
imwrite(salt_data,'mangguo_salt.bmp'); %保存图像为文件
?
?
|