tic

clc;

a=imread(‘lena_gray.tif’);

imshow(a)

nf=.9;

ws=9;

b=imnoise(a,’salt & pepper’,nf);

figure,imshow(b)

[m1 n1]=size(a);

r=floor(ws/2);

b1=zeros(m1+ws-1);

b1=uint8(b1);

[m n]=size(b1);

b1(r+1:m-r,r+1:n-r)=b;

for i=r+1:m-r

for j=r+1:n-r

t1=b1(i-r:i+r,j-r:j+r);

t2=sort(t1,1);

t2=sort(t2,2);

t2=t2′;

t2=t2(:);

t2=t2′;

le=length(t2);

len=ceil(le/2);

if (0<t1(r+1,r+1)&&t1(r+1,r+1)<255)

else

if t1(r+1,r+1)==255

for i1=len:-1:1

if (0<t2(i1)&&t2(i1)<255)

b1(i,j)=t2(i1);

break;

end

if (i1==1)

b1(i,j)=t2(len);

end

end

elseif t1(r+1,r+1)==0

for i1=len:le

if (0<t2(i1)&&t2(i1)<255)

b1(i,j)=t2(i1);

break;

end

if (i1==le)

b1(i,j)=t2(len);

end

end

end

end

end

end

b2=b1(r+1:m-r,r+1:n-r);

figure,imshow(b2)

toc

# Tag Archives: Matlab codes

# Bubble Sort in MATLAB

clc, clear, close all;

tic

a=imread(‘lena_gray.tif’);

nf=.5;

ws=3;

b=imnoise(a,’salt & pepper’,nf);

figure,imshow(b);

[m1 n1]=size(b);

ll=floor(ws/2);

b1=zeros(m1+ws-1); % b1 is zero matrix of 514*514

b1=uint8(b1); % b1 is a unsign 8 bit

[m n]=size(b1);

b1(ll+1:m-ll,ll+1:n-ll)=b;

for i=ll+1:m1-ll

for j=ll+1:n1-ll

f1=b1(i-ll:i+ll,j-ll:j+ll);

f=f1;

n=length(f);

g=size(f);

for k=1:g

for l=g-1:-1:k+1

if(f(l)<f(l+1))

tmp=f(l);

f(l)=f(l+1);

f(l+1)=tmp;

end

end

end

if (0<b1(i,j)&&b1(i,j)<255) %(0<t1(2,2)&&t1(2,2)<255)

else

for i1=9:-1:1 % for i1= (start= 5 : step= -1 : end=1)

if (0<f(i1)&&f(i1)<255) % if

b1(i,j)=f(i1);

break;

end

if (i1==1)

b1(i,j)=f(5);

end

end

end

end

end

[m n]=size(b1);

b2=b1(ll+1:m-ll,ll+1:n-ll); % Size of b2 is 512*512

figure,imshow(b2);

toc