function eval_obj_fn_to_mean
% EVAL_OBJ_FN_TO_MEAN: Evaluate objective function for the mean.
%
% Code written by Katherine Smith, 2003
%
%    GENERAL
%
%      eval_obj_fn_to_mean
%
%    INPUT/S
%
%      -
%           
%    OUTPUT/S
%
%      -
%
%    PENDING WORK
%
%      -
%
%    KNOWN BUG/S
%
%      -
%
%    COMMENT/S
%
%      
%
%    RELATED FUNCTION/S
%
%      
%
%    ABOUT
%
%      -Created:     November 23rd, 2003
%      -Last update: Novermber 30th, 2003
%      -Revision:    0.0.1
%      -Author:      R. S. Schestowitz, University of Manchester
% ==============================================================

% make images and points

do_plot = 0;
obj_fn_type = 'msd';
n_images = 10;
n_iterations = 10;
n_sets = 10;
image_width = 50;
ref_shift = 0.2;
max_shift = 0.2;
step = 0.1;
los = zeros(n_images,floor((max_shift-ref_shift)/step));
his = zeros(n_images,floor((max_shift-ref_shift)/step));
blurred = 0;
spec_iters = 25;
gen_iters = 25;
min_error = 0;
max_error = 1;
error_step = 0.1;
n_error_steps = (max_error-min_error)/error_step;

for this_set = 1:n_sets
	[imagelist images points his los] = make_1d_images(n_images, image_width, 0.2);
	% smooth images
	window = 9;
	%    images = average_smooth(images, window); blurred = 1;
	%    images = gaussian_smooth(images, window); blurred = 1;
	
	% normalise points from -1 to 1
	points = -1 + 2*(points-1)/(image_width-1);
	los = -1 + 2*(los-1)/(image_width-1);
	his = -1 + 2*(his-1)/(image_width-1);
		
	keep = 0.999999;
	ref_points_vec = points(:,1);
	ref_image_vec = images(:,1);
	if(strcmp(obj_fn_type,'model'))
        ref_hi = mean(his);
        ref_lo = mean(los);
	elseif(strcmp(obj_fn_type,'msd'))
        ref_hi = his(1);
        ref_lo = los(1);
	end
	
	%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
	% Warping images, then modelling warped images
	% attempt to register images by warping.
  if(do_plot)
  	subplot_fig = figure;
      figure(subplot_fig),for i=1:n_images,subplot(n_images,2,(2*i)-1),plot(images(:,i)),title('Unwarped images'); end;
  end
  warped_images = images;
  warped_points = points;
	
	% move points iteratively towards mean
  for n=1:n_iterations*2-1
    disp(['iter ',num2str(n)]);
    % correct registration
    % attempt to register
    for i=1:n_images
      disp(['Warping image ',num2str(i),' of ',num2str(n_images)]);
      image_vec = images(:,i);
      points_vec = points(:,i);
      hi_step = (his(i)-ref_hi)/n_iterations;
      lo_step = (los(i)-ref_lo)/n_iterations;
      params.green = 'biharmCPS';
      [warped_points,E,data] = nrr_trans_1d(points_vec,[his(i),los(i)],[his(i)+hi_step*n, los(i)+lo_step*n],params,[]);
      warped_image = interp1(points_vec,image_vec,warped_points, 'linear',0);
%      [param_list, warped_point, warped_image, score] = optimise_warps(ref_image_vec, ref_points_vec, image_vec, points_vec, 'multi_point', 'edge',subplot_fig,i*2);
          % Warps are being applied.
      warped_images(:,i) = warped_image;
      warped_points(:,i) = warped_points;
      if(do_plot)
        figure(subplot_fig),subplot(10,2,2*i,'replace'),plot(warped_image); drawnow;
        pause(0.05);
      end
    end
      
	if(strcmp(obj_fn_type,'model'))
        norm_type = 'edge';
        keep = 0.9999;
        c_model = build_model(warped_images, warped_points, keep, '', norm_type);
        obj_fn(n,this_set) = measure_model(c_model.variances,20);
	elseif(strcmp(obj_fn_type,'msd'))
          for i = 1:n_images
            temp(i) = msd(warped_images(:,i),ref_image_vec);
          end
        obj_fn(n,this_set) = mean(temp);
	    % rename these variabled e.g. temp
	end
  end % n_iterations
end % n_sets

% logged
figure,errorbar(log(mean(obj_fn,2)),std(obj_fn,0,2)./mean(obj_fn,2)),title([obj_fn_type, '(log)']);

% not logged
%figure,errorbar(mean(obj_fn,2)),std(obj_fn,0,2),title(obj_fn_type);
