function [status] = register_2d_images(number_of_images, image_width, image_height, position, width, height, width_variation, height_variation)
% REGISTER_2D_IMAGES: Genrates examples of 2-D images and warps them
%
%    GENERAL
%
%      [status] = register_2d_images(number_of_images, image_width,
%        image_height, position, width, height, width_variation, height_variation)
%
%    INPUT/S
%
%      -number_of_images:
%           The number of images to generate.
%
%      -image_width:
%           The width of the 2-D images to be created.
%
%      -image_height:
%           The width of the 2-D images to be created.
%
%      -width:
%            The width of the object in the image.
%
%      -width_variation:
%            The width variation of the object in the image.
%
%      -position:
%            The horizontal position of the object in the image.
%
%      -height:
%            The height of the object in the image.
%
%      -height_variation:
%            The height variation of the object in the image.
%           
%    OUTPUT/S
%
%      -status
%           Status on the success of the process.
%
%    PENDING WORK
%
%
%    KNOWN BUG/S
%
%      -
%
%    COMMENT/S
%
%
%    RELATED FUNCTION/S
%
%      MAKE_1D_IMAGES, MAKE_1D_BUMP, REGISTER_2D_IMAGES
%
%    ABOUT
%
%      -Created:     January 6th, 2003
%      -Last update: January 6th, 2003
%      -Revision:    0.0.1
%      -Author:      R. S. Schestowitz, University of Manchester
% ==============================================================


warp_flag = 1;
          % should the images be warped (else only displayed)
verbose_mode = 1;
          % internally controlled parameter
debugging_mode = 1;
          % does extra tests

subplot_fig = figure;
H = figure(subplot_fig);

for i=1:number_of_images,
   if (verbose_mode == 1) 
     disp(['generating image #',num2str(i),' of ',num2str(number_of_images)]);
   end
   [index,current_image_vectors,current_point_vectors]=make_2d_bump(1, image_width, image_height, position, width, height, width_variation, height_variation);
   if (debugging_mode == 1) % view it if debugging
       size(current_image_vectors)
       index
   end 
   subplot(number_of_images+4,2,i);
   hold on;
   title(['Generated image #',num2str(i)]);   
   imshow(current_image_vectors);
   image_vectors(:,:,i) = current_image_vectors;
   point_vectors(:,:,i) = current_point_vectors;
   hold off;
end

if( warp_flag == 1),    
    number_of_iterations = 1;
         % how many iterations of the optimisation/warping to run
    
    reference_points_vector = point_vectors(:,:,1);
    reference_image_vector = image_vectors(:,:,1);
                % set reference to be the first image
    if (debugging_mode == 1) % view it if debugging
      subplot(number_of_images+4,2,number_of_images+2);
      hold on;
      title('Reference image');
      imshow(reference_image_vector);
      hold off;
    end 
         
    warped_images = image_vectors;
    warped_points = point_vectors;   % copy these values as warped variables are dynamic  
         
    for n=1:number_of_iterations,
                            % images are now warped one by one with respect to reference image
         for i=1:number_of_images,
                if (verbose_mode == 1) 
                   disp(['Warping image #',num2str(i),' of ',num2str(number_of_images)]);
                end
                current_image_vector_to_warp = warped_images(:,:,i);
                current_point_vector_to_warp = warped_points(:,:,i);
                
                % warp image using one objective function or another - currently msd_opt_together
                

                scores_mi(i) = mi(reference_image_vector, warped_images(:,:,i),10);
                scores_smsd(i) = sum(msd(reference_image_vector, warped_images(:,:,i)));
                             % get similarity measures
                subplot(number_of_images+4,2,number_of_images+3);        
                hold on;
                title('MI scores');
                plot(scores_mi,'*');
                hold off;   
                
                subplot(number_of_images+4,2,number_of_images+4);
                hold on;
                title('MSD scores'); 
                plot(scores_smsd,'*');
                hold off;
         end
         
         warp_params.green = 'biharmCPS';
         param_list = [];
          %%%% The following are the ref and images: warped_images, reference_image_vector
        [grid, steps] = setup_grid(2, unwarped_image, ref_points, placement_method, n_points); 
                            % do this knowt-point selection in 2-D. see the brain equivalent which
                            % picks up points around the skull. in the above, steps are the points
                            % in 1-D - see GRID_TEST
            % now with knotpoints chosen in 2-D space....
         warped_grid = grid;
            % copy these knot-points. grid should be called knot-points or something similar. Maybe a 2-d grid
         [params, score] = fminsearch(@eval_msd_multi_warp,zeros(size(grid)),optimset('Display',verbose,'TolX',1e-10),grid, unwarped_image, unwarped_points, ref_image, ref_points, 20, warp_params);
            % find good paramters to achieve good similarity to the reference image         
         param_list(size(param_list,1)+1,:).d = params;
            % copy the parameters of the warp. Meaning of the notation above is still unknown.
         warped_grid = grid + params;
             % append parameters to the grip. Perhaps parameters are offests to the grid
         start_points = nrr_trans_1d(start_points, grid, warped_grid, warp_params,[]);
             % non-rigid transformation applied
         start_image = interp1(unwarped_points,unwarped_image,start_points, 'linear',0);
            % interpolate the image
         warped_points = start_points;
         warped_image = start_image;
           % copy some values
    end    
end    


status = 0;
     % report okay status
     % if problems have occurred earlier on in this function, set this flag to 1.
     
     
     
% The following is taken from the MATLAB logo function and can be used to
% show the 2D set as a plain with startling lighting effects.
%
% L = point_vectors(:,:,1);
% axes('CameraPosition', [-193.4013 -265.1546  220.4819],...
%     'CameraTarget',[26 26 10], ...
%     'CameraUpVector',[0 0 1], ...
%     'CameraViewAngle',9.5, ...
%     'DataAspectRatio', [1 1 .9],...
%     'Position',[0 0 1 1], ...
%     'Visible','off', ...
%     'XLim',[1 51], ...
%     'YLim',[1 51], ...
%     'ZLim',[-13 40]);
% s = surface(L, ...
%     'EdgeColor','none', ...
%     'FaceColor',[0.8 0.2 0.2], ...
%     'FaceLighting','phong', ...
%     'AmbientStrength',0.3, ...
%     'DiffuseStrength',0.6, ... 
%     'Clipping','off',...
%     'BackFaceLighting','lit', ...
%     'SpecularStrength',1.1, ...
%     'SpecularColorReflectance',1, ...
%     'SpecularExponent',7);
% l1 = light('Position',[40 100 20], ...
%     'Style','local', ...
%     'Color',[0 0.7 0.7]);
% l2 = light('Position',[.5 -1 .4], ...
%     'Color',[1 1 0]);