function glassware_3x3 ( )

%*****************************************************************************80
%
%% glassware_3x3() filters out noise from a gray scale image.
%
%  Discussion:
%
%    Each pixel is replaced by the median of the pixels in the 3x3 neighborhood.
%
%  Licensing:
%
%    This code is distributed under the MIT license.
%
%  Modified:
%
%    24 April 2018
%
%  Author:
%
%    John Burkardt
%
  fprintf ( 1, '\n' );
  fprintf ( 1, 'GLASSWARE_3X3:\n' );
  fprintf ( 1, '  MATLAB version\n' );
  fprintf ( 1, '  Read a noisy black-and-white image.\n' );
  fprintf ( 1, '  Clean it up by replacing each pixel by the median\n' );
  fprintf ( 1, '  of its 3x3 neighborhood.\n' );

  gray = imread ( 'glassware_noisy.png' );

  [ m, n ] = size ( gray );
  gray3d = zeros ( 9, m, n );
%
%  For pixel (I,J), GRAY3D(*,I,J) contains the current pixel value
%  and the 8 neighboring values.
%
  gray3d(1,:,:) = circshift ( gray, [ -1, -1 ] );
  gray3d(2,:,:) = circshift ( gray, [ -1,  0 ] );
  gray3d(3,:,:) = circshift ( gray, [ -1,  1 ] );
  gray3d(4,:,:) = circshift ( gray, [  0, -1 ] );
  gray3d(5,:,:) = circshift ( gray, [  0,  0 ] );
  gray3d(6,:,:) = circshift ( gray, [  0,  1 ] );
  gray3d(7,:,:) = circshift ( gray, [  1, -1 ] );
  gray3d(8,:,:) = circshift ( gray, [  1,  0 ] );
  gray3d(9,:,:) = circshift ( gray, [  1,  1 ] );
%
%  By taking the median of the 9 values, we hope to drop any noisy pixels.
%
  gray3d = median ( gray3d );
%
%  The MEDIAN command returned a 3D array with a first dimension of 1.
%  For convenience, we suppress the first dimension.
%
  gray_3x3(:,:) = gray3d(1,:,:);
%
%  GRAY is a UINT8 array.  The MEDIAN operation returned a DOUBLE value.
%  So we have to convert the array back to UINT8.
%
  gray_3x3 = uint8 ( gray_3x3 );

  filename = 'glassware_3x3.png';
  imwrite ( gray_3x3, filename, 'png' );
  fprintf ( 1, '  Graphics saved as "%s"\n', filename );
%
%  Terminate.
%
  fprintf ( 1, '\n' );
  fprintf ( 1, 'GLASSWARE_3X3:\n' );
  fprintf ( 1, '  Normal end of execution.\n' );

  return
end
