Tag Archives: Asprince sorting

Asprince Sorting Code in MATLAB

tic
clc
clear all
a=imread(‘lena_gray.tif’);
imshow(a)
nf=.5;
ws=7;
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;
%figure,imshow(b3)
for i=r+1:m-r
for j=r+1:n-r
t1=b1(i-r:i+r,j-r:j+r);
t2=sortrows(t1);
t2=sort(t1(1:end,1:end));
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)
[psnr,mse]=psnr_mse_maxerr(a,b2);
display(psnr);display(mse);
toc

Advertisements