Current location - Recipe Complete Network - Dinner recipes - Matlab plus salt and pepper noise
Matlab plus salt and pepper noise
Just finished. If it works well, I will release it to you. 7×7,1/kloc-0 /×11,change it yourself!

Haha, it's the first time I know that Baidu answers questions. ...

Clear;

clc

I=imread ('carrot [gray]. JPG’);

[a, b]= size (i);

I guessed.

Subplots (1, 2,1); Imshow (1); Title ('gray original');

K=imnoise(I,' salt & pepper', 0.02);

[a, b]= size (k);

Subplots (1, 2, 2); im show(K); Title ('image after adding salt and pepper noise');

%3*3 median filtering

For i= 1:a

For j= 1:b

if(I = = 1 | I = = a | j = = 1 | j = = b)

G(i,j)=K(i,j);

other

temp( 1)=K(i- 1,j- 1);

temp(2)=K(i- 1,j);

temp(3)=K(i- 1,j+ 1);

temp(4)=K(i,j- 1);

temp(5)=K(i,j);

temp(6)=K(i,j+ 1);

temp(7)=K(i+ 1,j- 1);

temp(8)=K(i+ 1,j);

temp(9)=K(i+ 1,j+ 1);

temp = sort(temp);

G(i,j)= temp(5);

end

end

end

I guessed.

Subplots (1, 2,1); imshow(G); Title ('3×3 median filtered image');

%5*5 median filtering

For i= 1:a

For j= 1:b

if(I = = 1 | I = = 2 | I = = a- 1 | I = = a | j = = 1 | j = = 2 | j = = b- 1 | j = = b)

G5(i,j)=K(i,j);

other

temp( 1)=K(i-2,j-2);

temp(2)=K(i-2,j- 1);

temp(3)=K(i-2,j);

temp(4)=K(i-2,j+ 1);

temp(5)=K(i-2,j+2);

temp(6)=K(i- 1,j-2);

temp(7)=K(i- 1,j- 1);

temp(8)=K(i- 1,j);

temp(9)=K(i- 1,j+ 1);

temp( 10)=K(i- 1,j+2);

temp( 1 1)=K(i,j-2);

temp( 12)=K(i,j- 1);

temp( 13)=K(i,j);

temp( 14)=K(i,j+ 1);

temp( 15)=K(i,j+2);

temp( 16)=K(i+ 1,j-2);

temp( 17)=K(i+ 1,j- 1);

temp( 18)=K(i+ 1,j);

temp( 19)=K(i+ 1,j+ 1);

temp(20)=K(i+ 1,j+2);

temp(2 1)=K(i+2,j-2);

temp(22)=K(i+2,j- 1);

temp(23)=K(i+2,j);

temp(24)=K(i+2,j+ 1);

temp(25)=K(i+2,j+2);

temp = sort(temp);

G5(i,j)= temp( 13);

end

end

end

Subplots (1, 2, 2); im show(G5); Title ('5×5 median filtered image');