function I2 = gray_medianfilter ( I1 )

  [ m, n ] = size ( I1 );
  p = double ( I1 );
  p2 = p;
  for i = 2 : m - 1 
    for j = 2 : n - 1 
      p2(i,j) = median ( ...
        [ p(i+1,j-1), p(i+1,j), p(i+1,j+1), ...
          p(i,  j-1), p(i,  j), p(i,  j+1), ...
          p(i-1,j-1), p(i-1,j), p(i-1,j+1) ] );
    end
  end
  I2 = uint8 ( p2 );

  return
end

