function ret = medianfilter(img,Fsize)
% Performs a Median Filter on image
% Arguments: image, size of neighbourhood - odd eg 3,5,7...

dimens=size(img);
height=dimens(1);
width=dimens(2);
offset=floor(Fsize/2);
ret=zeros(height-2*offset,width-2*offset);

for x=1:(width-Fsize+1)
   for y=1:(height-Fsize+1)
      neighbourhood=img(y:y+Fsize-1,x:x+Fsize-1);
      list=neighbourhood(1,:);
      for n=2:Fsize
         list=[list neighbourhood(n,:)];
      end
      ret(y,x)=median(list);      
   end
end   