Spatial Error Concealment

Marcelo Bel? (view profile)

16 Jan 2008 (Updated )

Implements video and static image spatial error concealment techniques with block error simulation i

DI (img, bs, sas)
```function [img2] = DI (img, bs, sas)

warning off all;
Sx=[-1,0,1;-2,0,2;-1,0,1];
Sy=[1,2,1;0,0,0;-1,-2,-1];
img2=img;

layer=1;
gx=imfilter(double(img(:,:,layer)), Sx);
gy=imfilter(double(img(:,:,layer)), Sy);
G=sqrt(gx.^2+gy.^2);
G(1:end,1)=0;
G(1:end,end)=0;
G(1,1:end)=0;
G(end,1:end)=0;
G(sas:sas+bs+1, sas:sas+bs+1)=0;
[indy, indx]=find(G);

%G=G(2:end-1, 2:end-1);
%Obtain the angle
thet=atan(gx./gy);
ind_thet=find(thet<0);
for(i=1:size(ind_thet,1))
thet(ind_thet(i))=thet(ind_thet(i))+pi;
end
%Round the angle to only 8 directions, with 22.5 degrees interval
k=mod(round(thet/(pi/8))+8,8)+1;

%Calculate the Dk, the direction of the edge, if there is one...

% Contrib: sdarticle.pdf - DI
Dk=zeros(1,8);

for(i=1:size(indy,1))
if(thet(indy(i),indx(i))==(pi/2))
if((indx(i)>=sas+1)&&((indx(i)<=sas+bs)))
Dk(k(indy(i),indx(i)))=Dk(k(indy(i),indx(i)))+G(indy(i),indx(i));
end
elseif(thet(indy(i),indx(i))==0)
if((indy(i)>=sas+1)&&((indy(i)<=sas+bs)))
Dk(k(indy(i),indx(i)))=Dk(k(indy(i),indx(i)))+G(indy(i),indx(i));
end
else
x1(indy(i),indx(i))=indx(i)+(sas+1-indy(i))/tan(thet(indy(i), indx(i)));
if((x1(indy(i),indx(i))>=sas+1)&&((x1(indy(i),indx(i))<=sas+bs)))
Dk(k(indy(i),indx(i)))=Dk(k(indy(i),indx(i)))+G(indy(i),indx(i));
else
x2(indy(i),indx(i))=indx(i)+(sas+bs-indy(i))/tan(thet(indy(i), indx(i)));
if((x2(indy(i),indx(i))>=sas+1)&&((x2(indy(i),indx(i))<=sas+bs)))
Dk(k(indy(i),indx(i)))=Dk(k(indy(i),indx(i)))+G(indy(i),indx(i));
else
y3(indy(i),indx(i))=indy(i)+(sas+1-indx(i))*tan(thet(indy(i), indx(i)));
if((y3(indy(i),indx(i))>sas+1)&&((y3(indy(i),indx(i))<sas+bs)))
Dk(k(indy(i),indx(i)))=Dk(k(indy(i),indx(i)))+G(indy(i),indx(i));
else
y4(indy(i),indx(i))=indy(i)+(sas+bs-indx(i))*tan(thet(indy(i), indx(i)));
if((y4(indy(i),indx(i))>sas+1)&&((y4(indy(i),indx(i))<sas+bs)))
Dk(k(indy(i),indx(i)))=Dk(k(indy(i),indx(i)))+G(indy(i),indx(i));
end
end
end
end
end
end

[maxDk,edgDir]=max(Dk);
edgDir=edgDir-1;
if(maxDk/(sum(sum(G))) < 0.1)
for(y=0:bs-1)
for(x=0:bs-1)
%all channels (gray or R,G,B)
pixel=double(img2(sas,sas+1+x,:))*0.5*(bs-y)/(bs+1); %up
pixel=pixel+double(img2(sas+1+bs,sas+1+x,:))*0.5*(y+1)/(bs+1); %down
pixel=pixel+double(img2(sas+1+y,sas,:))*0.5*(bs-x)/(bs+1); %left
pixel=pixel+double(img2(sas+1+y,sas+bs+1,:))*0.5*(x+1)/(bs+1); %right
img2(sas+1+y,sas+1+x,:)=uint8(pixel);
end
end
else
for(layer=1:size(img,3))
for(i=sas+1:sas+bs)
for(j=sas+1:sas+bs)
found=0;
if(edgDir==0)
X(1)=sas;
Y(1)=i;
X(2)=sas+bs+1;
Y(2)=i;
elseif (edgDir==4)
Y(1)=sas;
X(1)=j;
Y(2)=sas+bs+1;
X(2)=j;
else
x1=round(j+(sas-i)/tan(edgDir*0.3927));
if((x1>=sas)&&((x1<=sas+bs+1)))
found=found+1;
X(found)=x1;
Y(found)=sas;
end
x2=round(j+(sas+bs+1-i)/tan(edgDir*0.3927));
if((x2>=sas)&&((x2<=sas+bs+1)))
found=found+1;
X(found)=x2;
Y(found)=sas+bs+1;
end
y3=round(i+(sas-j)*tan(edgDir*0.3927));
if((y3>sas)&&((y3<sas+bs+1)))
found=found+1;
X(found)=sas;
Y(found)=y3;
end
y4=round(i+(sas+bs+1-j)*tan(edgDir*0.3927));
if((y4>sas)&&((y4<sas+bs+1)))
found=found+1;
X(found)=sas+bs+1;
Y(found)=y4;
end
end
Dsp1=sqrt((Y(1)-i)^2+(X(1)-j)^2);
Dsp2=sqrt((Y(2)-i)^2+(X(2)-j)^2);
img2(i,j,layer)=uint8((double(img2(Y(1), X(1),layer))*Dsp2+double(img2(Y(2), X(2),layer))*Dsp1)/(Dsp1+Dsp2));
end
end
end
end

```