  g = imread ( 'snap.png' );

  [ m, n ] = size ( g );
%
%  FIGURE 1: The original image.
%
  figure ( 1 )
  imshow ( g );
  title ( 'Original image' )
%
%  Make a numerical vector out of the uint8 array.
%
  p = double ( g );
  p = reshape ( p, m * n, 1 );
%
%  FIGURE 2: Grayscale histogram.
%
  figure ( 2 )
  hist ( p, 256 )
  title ( 'Histogram for original image' )
%
%  Use KMEANS to choose 10 grayscales.
%
  k = 10;
  [ c, ptoc ] = kmeans ( p, k );
  p2 = ptoc(c);
  g2 = reshape ( p2, m, n );
  g2 = uint8 ( g2 );
%
%  FIGURE 3: Display the image.
%
  figure ( 3 )
  imshow ( g2 )
  title_string = sprintf ( 'Image using %d shades', k );
  title ( title_string )
%
%  FIGURE 4: Grayscale histogram.
%
  figure ( 4 )
  hist ( pv2, 256 )
  title_string = sprintf ( 'Histogram using %d shades', k );
  title ( title_string )
%
%  Use KMEANS to choose 3 grayscales.
%
  k = 3;
  [ c, ptoc ] = kmeans ( p, k );
  p2 = ptoc(c);
  g2 = reshape ( p2, m, n );
  g2 = uint8 ( g2 );
%
%  FIGURE 5: Display the image.
5
  figure ( 5 )
  imshow ( g2 )
  title_string = sprintf ( 'Image using %d shades', k );
  title ( title_string )
%
%  FIGURE 6: Grayscale histogram.
%
  figure ( 6 )
  hist ( pv2, 256 )
  title_string = sprintf ( 'Histogram using %d shades', k );
  title ( title_string )
