% requires that the window size (3×3 first, then 5×5 gradually increase) be variable, and the window type (linear, cross, square) is optional)
% domain average method and median filtering method are filtering methods in spatial domain.
clear all;
I=imread('lena.bmp');
J 1=imnoise(I,'gaussian',0,0.02); % Gaussian noise has a mean value of 0 and a variance of 0.02.
J2=imnoise(I,'salt & pepper',0.02); The variance of% salt and pepper noise is 0.02.
J3=imnoise(I,'speckle',0.02); The mean value of% multiplicative noise is 0 and the variance is 0.02.
Subplot (2,2,1), im show (I), title ('original image');
Subplot (2,2,2), im show (j1), title ('Gaussian noise');
Subplot (2,2,3), im show (J2), title ('salt and pepper noise');
Subplot (2,2,4), im show (J3), title ('multiplicative noise');
% median filtering method
figure(2)
K 1=medfilt2(J 1,); Filter window of %3*3
subplot(3,2, 1),imshow(K 1,); Filter window of %5*5
Subplot (3,2,2), im show (k2, []), title ('median filtering of 5*5 Gaussian noise image');
K2=medfilt2(J2,); Filter window of %3*3
subplot(3,2,3),imshow(K2,); Filter window of %5*5
Subplot (3, 2, 4), im show (k2, []), title ('median filtering of 5*5 image with salt and pepper noise');
K3=medfilt2(J3,); Filter window of %3*3
subplot(3,2,5),imshow(K3,); Filter window of %5*5
Subplot (3, 2, 6), im show (k3, []), title ('median filtering for 5*5 multiplicative noise image');
% domain average method
figure(3)
L 1=filter2(fspecial('average',3),J 1); % 3*3 Template Smoothing Mean Filtering
L2=filter2(fspecial('average',5),J 1); %5×5 template smoothing mean filtering
Subplot (3,2,1), im show (l1,[]), title ('Smoothing images with 3*3 Gaussian noise');
Subplot (3, 2, 2), im show (L2, []), title ('Smoothing image with 5*5 Gaussian noise');
L3=filter2(fspecial('average',3),J2); % 3*3 Template Smoothing Mean Filtering
L4=filter2(fspecial('average',5),J2); %5×5 template smoothing mean filtering
Subplot (3, 2, 3), im show (L3, []), title ('Smoothing image with 3*3 salt and pepper noise');
Subplot (3, 2, 4), im show (l4, []), title ('Smoothing image with salt and pepper noise of 5*5');
L5=filter2(fspecial('average',3),J3); % 3*3 Template Smoothing Mean Filtering
L6=filter2(fspecial('average',5),J3); %5×5 template smoothing mean filtering
Subplot (3, 2, 5), im show (l5, []), title ('Smoothing images with 3*3 multiplicative noise');
Subplot (3, 2, 6), im show (l6, []), title ('Smoothing images with 5*5 multiplicative noise');
The% domain average method refers to replacing the gray value of a point with the average value of the gray value of that point. The algorithm is simple and the processing speed is fast, but it will also blur the image while attenuating the noise.
The% median filtering method can suppress salt and pepper noise very well, but it is not suitable for images with more details such as points, lines and spires.