%%-------------------------Matlab图像的噪声处理-------------------------
%-------------------头文件-----------------------------
clc ; %清屏幕
clear ; %删除所有的变量
close all ; %将所有打开的图片关掉
%-----------------------添加噪声 imnoise()---------------------------
A = imread('1.jpg') ;
A1 = rgb2gray( A ) ; %彩图转灰度图
B1 = imnoise( A1, 'gaussian' ) ; %添加高斯噪声
B2 = imnoise( A1, 'salt & pepper' ) ; %添加椒盐噪声
B3 = imnoise( A1, 'speckle' ) ; %添加乘性噪声
%----------------------------去除噪声--------------------------------
%-------------中值滤波 medfilt2()--------------------
C1 = medfilt2(B1) ; %高斯噪声中值滤波
C2 = medfilt2(B2) ; %椒盐噪声中值滤波
C3 = medfilt2(B3) ; %乘性噪声中值滤波
%-------------均值滤波 --------------------
%fspecial函数用于预定义滤波器,参数表示返回模板尺寸为3x3 的均值滤波器 h。
h = fspecial('average', 3);
% B=imfilter(A,h) 使用多维滤波器 h 对多维数组 A 进行滤波,并在 B 中返回结果。
% replicate表示数组边界之外的输入数组值假定为等于最近的数组边界值
D1 = imfilter(B1, h, 'replicate') ; %高斯噪声均值滤波
D2 = imfilter(B2, h, 'replicate') ; %椒盐噪声均值滤波
D3 = imfilter(B3, h, 'replicate') ; %乘性噪声均值滤波
%-------------维纳滤波 wiener2()--------------------
% wiener2(I,[m n]) , [m n] 指定用于估计局部图像均值和标准差的邻域的大小(m×n)
E1 = wiener2(B1, [5,5]) ; %高斯噪声维纳滤波
E2 = wiener2(B2, [5,5]) ; %椒盐噪声维纳滤波
E3 = wiener2(B3, [5,5]) ; %乘性噪声维纳滤波
%-------------------显示结果-----------------------
figure ;
subplot( 4 , 3 , 1 ) ; imshow( B1 ) ; title( '高斯噪声图像' ) ;
subplot( 4 , 3 , 2 ) ; imshow( B2 ) ; title( '椒盐噪声图像' ) ;
subplot( 4 , 3 , 3 ) ; imshow( B3 ) ; title( '乘性噪声图像' ) ;
subplot( 4 , 3 , 4 ) ; imshow( C1 ) ; title( '高斯噪声中值滤波' ) ;
subplot( 4 , 3 , 5 ) ; imshow( C2 ) ; title( '椒盐噪声中值滤波' ) ;
subplot( 4 , 3 , 6 ) ; imshow( C3 ) ; title( '乘性噪声中值滤波' ) ;
subplot( 4 , 3 , 7 ) ; imshow( D1 ) ; title( '高斯噪声均值滤波' ) ;
subplot( 4 , 3 , 8 ) ; imshow( D2 ) ; title( '椒盐噪声均值滤波' ) ;
subplot( 4 , 3 , 9 ) ; imshow( D3 ) ; title( '乘性噪声均值滤波' ) ;
subplot( 4 , 3 , 10 ) ; imshow( E1 ) ; title( '高斯噪声维纳滤波' ) ;
subplot( 4 , 3 , 11 ) ; imshow( E2 ) ; title( '椒盐噪声维纳滤波' ) ;
subplot( 4 , 3 , 12 ) ; imshow( E3 ) ; title( '乘性噪声维纳滤波' ) ;