function [param_list, warped_points, warped_image, score] = optimise_all_warps_nmi(ref_image, ref_points, unwarped_image, unwarped_points, spline_type, placement_method, n_points, verbose, do_plot, subplot_fig, pane, column, image_width, number_of_images, verbose_score, number_of_bins, precision_required)
% OPTIMISE_ALL_WARPS_NMI: 
%
% Code written by Katherine Smith, 2003
%
%    GENERAL
%
%      [param_list, warped_points, warped_image, score] = 
%          optimise_all_warps_nmi(ref_image, ref_points, unwarped_image,
%          unwarped_points, spline_type, placement_method, n_points, verbose,
%          do_plot, subplot_fig, pane,
%          image_width, number_of_images, verbose_score)         
%
%    INPUT/S
%
%      -X:
%          X
%           
%    OUTPUT/S
%
%      -X:
%           X
%
%    PENDING WORK
%      
%      
%
%    KNOWN BUG/S
%
%      
%
%    COMMENT/S
%
%      
%
%    RELATED FUNCTION/S
%
%      
%
%    ABOUT
%
%      -Created:     November 23rd, 2003
%      -Last update: January 23rd, 2004
%      -Revision:    0.1.5
%      -Author:      R. S. Schestowitz, University of Manchester
% ==============================================================

[grid, steps] = setup_grid(2, unwarped_image, ref_points, placement_method, n_points);

if (do_plot),
  figure(subplot_fig);
  subplot(number_of_images, column, pane, 'replace');
  plot(unwarped_image);
  axis([0 image_width 0 1]);
  drawnow;
end

warp_params.green = 'biharmCPS';
param_list = [];
start_image = unwarped_image;
start_points = unwarped_points;
last_score = nmi(ref_image, unwarped_image, number_of_bins);
if(strcmp(spline_type,'single_point'))
  [params, score] = fminsearch(@eval_nmi_cps_warp,zeros(size(grid)),optimset('Display',verbose,'TolX', precision_required),grid,steps,start_points, start_image, ref_image, number_of_bins);
  param_list(size(param_list,1)+1).d = params;
  [dummy,start_points] = eval_nmi_cps_warp(params,grid,steps, start_points, start_image, ref_image, number_of_bins);
  start_image = interp1(ref_points,unwarped_image,start_points, 'linear',0);  
elseif(strcmp(spline_type,'multi_point'))
  warped_grid = grid;
  warp_params.green = 'biharmCPS';
  last_score = eval_nmi_multi_warp(zeros(size(grid)),grid, unwarped_image, unwarped_points, ref_image, ref_points, 20, warp_params, number_of_bins);
  [params, score] = fminsearch(@eval_nmi_multi_warp,zeros(size(grid)),optimset('Display',verbose,'TolX', precision_required),grid, unwarped_image, unwarped_points, ref_image, ref_points, 20, warp_params, number_of_bins);
  param_list(size(param_list,1)+1,:).d = params;
  warped_grid = grid + params;
  start_points = nrr_trans_1d(start_points, grid, warped_grid, warp_params,[]);
  start_image = interp1(unwarped_points,unwarped_image,start_points, 'linear',0);
else
  error('Unknown spline type');      
end

if (verbose_score == 1),
      disp(['Initial score: ',num2str(last_score),' Final score: ', num2str(score), ' Difference: ', num2str(last_score - score)]);
end
  
warped_points = start_points;
warped_image = start_image;

if (do_plot),
  figure(subplot_fig);
  subplot(number_of_images, column, pane, 'replace');
  plot(warped_image);
  axis([0 image_width 0 1]);
  title(num2str(score));
  drawnow;
end
