function g11=expand2(g1)

[C,R]=size(g1);
g11=[];

%compute edges and corners
for I=1:C-1
   g11(2*I+1,2)=median([g1(I,1) g1(I,1) g1(I,1) g1(I+1,1) g1(I+1,1) g1(I+1,1) g1(I,2) g1(I+1,2)]);
   g11(2*I+1,1)=(g1(I,1)+g1(I+1,1))/2;
   g11(2*I+1,2*R)=median([g1(I,R) g1(I,R) g1(I,R) g1(I+1,R) g1(I+1,R) g1(I+1,R) g1(I,R-1) g1(I+1,R-1)]);
end
for J=1:R-1
   g11(2,2*J+1)=median([g1(1,J) g1(1,J) g1(1,J) g1(1,J+1) g1(1,J+1) g1(1,J+1) g1(2,J) g1(2,J+1)]);
   g11(1,2*J+1)=(g1(1,J)+g1(1,J+1))/2;
   g11(2*C,2*J+1)=median([g1(C,J) g1(C,J) g1(C,J) g1(C,J+1) g1(C,J+1) g1(C,J+1) g1(C-1,J) g1(C-1,J+1)]);
end

g11(2:2:2*C,2:2:2*R)=g1;
g11(2:2:2*C,1)=g1(:,1);
g11(1,2:2:2*R)=g1(1,:);
g11(1,1)=g1(1,1);

%use weighted median filter
for I=2:C-1
   for J=1:R-1
      g11(2*I,2*J+1)=median([g1(I-1,J) g1(I-1,J+1) g1(I,J) g1(I,J) g1(I,J) g1(I,J+1) g1(I,J+1) g1(I,J+1) g1(I+1,J) g1(I+1,J+1)]);
   end
end
for I=1:C-1
   for J=2:R-1
      g11(2*I+1,2*J)=median([g1(I,J-1) g1(I+1,J-1) g1(I,J) g1(I,J) g1(I,J) g1(I+1,J) g1(I+1,J) g1(I+1,J) g1(I,J+1) g1(I+1,J+1)]);
   end
end
for I=1:C-1
   for J=1:R-1
      g11(2*I+1,2*J+1)=median([g1(I,J) g1(I+1,J) g1(I,J+1) g1(I+1,J+1)]);
   end
end

