function image_sample ( filename )

%*****************************************************************************80
%
%% image_sample ( ) allows the user to select points on a displayed image.
%
%  Discussion:
%
%    As an example, the image of an aneurysm can be displayed, and the user
%    can select points that define the boundary of the aneurysm.
%
%    The coordinates of the selected points will be saved to a file.
%
%  Licensing:
%
%    This code is distributed under the MIT license.
%
%  Modified:
%
%    28 February 2024
%
%  Author:
%
%    John Burkardt
%
%  Input:
%
%    string filename: the image filename, minus the file extension.
%
  timestamp ( );

  fprintf ( 1, '\n' );
  fprintf ( 1, 'image_sample():\n' );
  fprintf ( 1, '  An image will be displayed.\n' );
  fprintf ( 1, '  Each time you click on a point, the (x,y) coordinates\n' );
  fprintf ( 1, '  of that point will be recorded.\n' );
  fprintf ( 1, '\n' );
  fprintf ( 1, '  To end the list, double click.\n' );
%
%  If filename not supplied, get it.
%
  if ( nargin < 1 )
    filename = input ( '  Enter the name of the image file to be sampled.' );
  end
%
%  Break the filename up into pieces.
%
  [ path, name, extension ] = fileparts ( filename );
%
%  Display the image.
%
  figure ( );
  imshow ( filename );
%
%  Allow the user to select points.
%
  [ x, y ] = getpts ( );
%
%  For some reason, getpts() numbers the vertical axis starting at the top!
%
  ymax = max ( y );
  y = ymax - y;
%
%  Save the data to a file.
%
  n = length ( x );
  filename = strcat ( name, '_boundary.txt' );
  r8vec2_write ( filename, n, x, y );
  fprintf ( 1, '\n' );
  fprintf ( 1, '  Selected point coordinates saved to "%s"\n', filename );
%
%  Plot the data.
%
  plot ( x, y, 'bo-' );
  grid ( 'on' );
  axis ( 'equal' );
  filename = strcat ( name, '_boundary.png' );
  print ( '-dpng', filename )
  fprintf ( 1, '  Graphics saved as "%s"\n', filename );
%
%  Terminate.
%
  fprintf ( 1, '\n' );
  fprintf ( 1, 'image_sample():\n' );
  fprintf ( 1, '  Normal end of execution.\n' );

  timestamp ( );

  return
end
function r8vec2_write ( output_filename, n, x, y )

%*****************************************************************************80
%
%% r8vec2_write() writes an R8VEC2 file.
%
%  Discussion:
%
%    An R8VEC2 is a pair of vectors of R8's.
%
%  Licensing:
%
%    This code is distributed under the MIT license.
%
%  Modified:
%
%    10 July 2011
%
%  Author:
%
%    John Burkardt
%
%  Input:
%
%    string OUTPUT_FILENAME, the output filename.
%
%    integer N, the number of points.
%
%    real X(N), Y(N), the vectors.
%

%
%  Open the file.
%
  output_unit = fopen ( output_filename, 'wt' );

  if ( output_unit < 0 ) 
    fprintf ( 1, '\n' );
    fprintf ( 1, 'R8VEC2_WRITE - Error!\n' );
    fprintf ( 1, '  Could not open the output file.\n' );
    error ( 'R8VEC2_WRITE - Error!' );
  end

  for j = 1 : n
    fprintf ( output_unit, '  %24.16g  %24.16g\n', x(j), y(j) );
  end
%
%  Close the file.
%
  fclose ( output_unit );

  return
end
function timestamp ( )

%*****************************************************************************80
%
%% timestamp() prints the YMDHMS date as a timestamp.
%
%  Licensing:
%
%    This code is distributed under the MIT license.
%
%  Modified:
%
%    14 November 2022
%
%  Author:
%
%    John Burkardt
%
  t = now ( );
  c = datevec ( t );
  s = datestr ( c, 0 );
  fprintf ( 1, '%s\n', s );

  return
end
