function Xm=med_filt2(X,w,c);
% function Xm=med_filt2(X,w,c);
%
% Inputs: 
%	X - image
%	w - block size (w=3 uses 3 x 3 median filter)
%	c - value to pad the edges with prior to computing median
%
% Outputs:
%	Xm - filtered image
%

% Mark Frei 12-13-94

[nr,nc] = size(X);
wk  = (w+1)/2 - 1;
% The next two lines pad the image with a boundary
   Xs= c*ones(nr+2*wk, nc+2*wk);
   Xs((wk+1):(wk+nr),(wk+1):(wk+nc))=X;

npts=nr*nc;
Xm=zeros(w^2,npts);
b1=1:nr; b2=1:nc;
for i=1:w, for j=1:w,
   Xm(w*(i-1)+j,:)=reshape(Xs((i-1)+b1, (j-1)+b2),1,npts);
end; end
Xm=reshape(median(Xm),nr,nc);
