function [validx,validy]=automate_image(grid_x,grid_y,filenamelist,validx,validy);

% Code to start actual image correlation
% Programmed by Chris and Rob
% Last revision: 09/10/08

% The automation function is the central function and processes all markers and 
% images by the use of the matlab function cpcorr.m. 
% Therefore the Current directory in matlab has to be the folder where 
%  automate_image.m finds the filenamelist.mat, grid_x.dat and grid_y.dat as well 
% as the images specified in filenamelist.mat. Just type automate_image; and 
% press ENTER at the command line of matlab. 
% At first, automate_image.m will open the first image in the filenamelist.mat and 
% plot the grid as green crosses on top. The next step will need some time since 
% all markers in that image have to be processed for the first image. After correlating 
% image one and two the new raster positions will be plotted as red crosses. On top 
% of the image and the green crosses. The next dialog will ask you if you want to 
% continue with this correlation or cancel. If you press continue, automate_image.m 
% will process all images in the filenamelist.mat. The time it will take to process 
% all images will be plotted on the figure but can easily be estimated by knowing the 
% raster point processing speed (see processing speed). 
% Depending on the number of images and markers you are tracking, this process 
% can take between seconds and days. For 100 images and 200 markers a decent 
% computer should need 200 seconds. To get a better resolution you can always 
% run jobs overnight (e.g. 6000 markers in 1000 images) with higher resolutions. 
% Keep in mind that CORRSIZE which you changed in cpcorr.m will limit your 
% resolution. If you chose to use the 15 pixel as suggested a marker distance of 
% 30 pixel will lead to a full cover of the strain field. Choosing smaller marker 
% distances will lead to an interpolation since two neighboring markers share 
% pixels. Nevertheless a higher marker density can reduce the noise of the strain field.
% When all images are processed, automate_image will write the files validx.mat, 
% validy.mat, validx.txt and validy.txt. The text files are meant to store the result in a 
% format which can be accessed by other programs also in the future.

% Changed by Suren A. Chilingaryan <csa@dside.dyndns.org> to optimize performance
% by tighter integration with Matlab images toolkit (few sources from the toolkit
% are moved to the current source tree and adjusted to benefit from knowledge of 
% tasks we are solving here. As well CUDA framework, if available, is used to
% speed-up core computations using parallel processors of latest NVidia video
% cards.
% OPTIMIZE parameter is controlling which optimizations should be used. 
%  0 - The original version, no optimizations
%  1 - The modified sources from images toolkit are used
%  2 - The CUDA matlab plugin is used to compute FFT's
%  3 - Most of computations are performed on NVidia card
%  4 - Optimize FFT sizes for better performance (affects precision)
% You can safely set an optimization level to 3. If NVidia card is not available
% the level will be automatically reduced to 1.
%
% SAVEDATA controls if data is saved per iteration or at the end
% 0 - in the end
% 1 - per iteration

OPTIMIZE = 4;
CORRSIZE = 15;	
PRECISION = 1000;
SILENT = 1;
SAVEDATA = 0;

% exist('grid_x')
% exist('grid_y')
% exist('filenamelist')
% exist('validx')
% exist('validy')

if exist('data', 'dir')
    datadir = 'data/';
else
    datadir = '';
end

if exist('images', 'dir')
    imagedir = 'images/';
else
    imagedir = '';
end

% Load necessary files
if exist('grid_x')==0
    load([datadir, 'grid_x.dat'])              % file with x position, created by grid_generator.m
end
if exist('grid_y')==0
    load([datadir, 'grid_y.dat'])              % file with y position, created by grid_generator.m
end
if exist('filenamelist')==0
    load([datadir, 'filenamelist'])            % file with the list of filenames to be processed
end


resume=0;
if exist('validx')==1
    if exist('validy')==1
        resume=1;
        [Rasternum Imagenum]=size(validx);
    end
end

[r,c]=size(filenamelist);           % this will determine the number of images we have to loop through

% Open new figure so previous ones (if open) are not overwritten
if (~SILENT)
    h=figure;
    imshow([imagedir, filenamelist(1,:)])           % show the first image
    title('Initial Grid For Image Correlation (Note green crosses)')        % put a title
    hold on
    plot(grid_x,grid_y,'g+')            % plot the grid onto the image
    hold off

    % Start image correlation using cpcorr.m
    g = waitbar(0,sprintf('Processing images'));        % initialize the waitbar
    set(g,'Position',[275,50,275,50])                   % set the position of the waitbar [left bottom width height]
end

firstimage=1;

if resume==1
    firstimage=Imagenum+1
end

%Initializing hardware code

ncp = prod(size(grid_x));

if OPTIMIZE > 2
    hwid = normxcorr_hw();

    if hwid > 0
	err = normxcorr_hw(hwid, 1, ncp, CORRSIZE, PRECISION, OPTIMIZE);
	if (err ~= 0)
	    normxcorr_hw(1);
	    OPTIMIZE = 2;
	end
    else
	if hwid < 0
	    OPTIMIZE = 1;
	else
	    OPTIMIZE = 2;
	end
    end
else
    hwid = 0;
end

% Initialize variables
if OPTIMIZE>2
    base_points_x=single(grid_x);
    base_points_y=single(grid_y);
else
    base_points_x=grid_x;
    base_points_y=grid_y;
end

if resume==1
    if OPTIMIZE>2
	input_points_x=single(validx(:,Imagenum));
	input_points_y=single(validy(:,Imagenum));
    else
	input_points_x=validx(:,Imagenum);
	input_points_y=validy(:,Imagenum);
    end
    inputpoints=1;
else
    if OPTIMIZE>2
	input_points_x=single(grid_x);
	input_points_y=single(grid_y);
    else
	input_points_x=grid_x;
	input_points_y=grid_y;
    end
end

base = uint8(mean(double(imread([imagedir, filenamelist(1,:)])),3));            % read in the base image ( which is always  image number one. You might want to change that to improve correlation results in case the light conditions are changing during the experiment

if OPTIMIZE > 2
%    crop_dim = [min(base_points_x(:)) - 2*CORRSIZE, min(base_points_y(:)) - 2 * CORRSIZE, max(base_points_x(:)) + 2*CORRSIZE, max(base_points_y(:)) + 2 * CORRSIZE]
%    crop_size = (crop_dim(3) - crop_dim(1)) * (crop_dim(4) - crop_dim(2))
%    mesh_size = ncp * 4 * CORRSIZE * CORRSIZE

%    Verification of GPU local sum and denom computations
%    dic_basecorr3(CORRSIZE, PRECISION, OPTIMIZE, hwid, base_points_x, base_points_y, base);

    data_base = struct;
    base_points_for(:,1)=reshape(base_points_x,[],1);
    base_points_for(:,2)=reshape(base_points_y,[],1);
    for icp = 1:ncp
	data_base(icp).base_fractional_offset = base_points_for(icp,:) - round(base_points_for(icp,:)*PRECISION)/PRECISION;
    end

    normxcorr_hw(hwid, 3, base_points_x, base_points_y);
    normxcorr_hw(hwid, 4, base);
else
  base_points_for(:,1)=reshape(base_points_x,[],1);
  base_points_for(:,2)=reshape(base_points_y,[],1);

  %some checks (moved here from cpcorr)
  if any(base_points_for(:)<0.5) || any(base_points_for(:,1)>size(base,2)+0.5) || any(base_points_for(:,2)>size(base,1)+0.5)
    msg = sprintf('In function %s, Control Points must be in pixel coordinates.',mfilename);
    eid = sprintf('Images:%s:cpPointsMustBeInPixCoord',mfilename);
    error(eid, msg);
  end

  if OPTIMIZE > 0
     data_base = struct;
    
     %moved from normxcorr2
     % We can precalculate it here, since near-edge images are dropped and, therefore, A,T always have the same size 
     % n is width and height of T, but following computations are done for A
     % outsize is sum if widths and heights of T and A - 1 (outsize = size(A) + size(T) - 1)

     outsize = 6 * CORRSIZE + 1;
     n = 2*CORRSIZE + 1;
     mn = n*n;

     rects_base = dic_calc_rects(base_points_for,2*CORRSIZE,base);

     for icp = 1:ncp
	if isequal(rects_base(icp,3:4),[0 0])
	    %near edge
	    data_base(icp).skip = 1;
	    continue
	end

	sub_base = imcrop(base,rects_base(icp,:));
    
	data_base(icp).skip = 0;

	double_sub_base = double(sub_base);

	if (numel(sub_base) < 2) ||  (std(double_sub_base(:)) == 0)
	    % This check is moved here for normxcorr2
	    eid = sprintf('Images:%s:sameElementsInTemplate',mfilename);
	    msg = 'The values of TEMPLATE cannot all be the same.';
	    error(eid,'%s',msg);
	end

	local_sum_A = dic_local_sum(double_sub_base,n,n);
	local_sum_A2 = dic_local_sum(double_sub_base.*double_sub_base,n,n);

	% Note: diff_local_sums should be nonnegative, but may have negative
	% values due to round off errors. Below, we use max to ensure the
	% radicand is nonnegative.
	denom_A = sqrt( max(( local_sum_A2 - (local_sum_A.^2)/mn ) / (mn-1), 0) );
	i_nonzero = find(denom_A~=0);
    
	data_base(icp).denom_A = denom_A;
	data_base(icp).i_nonzero = i_nonzero;

	data_base(icp).local_sum_A = local_sum_A;
	data_base(icp).sub_base = sub_base;
	
	if (OPTIMIZE > 1)
	    data_base(icp).sub_base_fft = fft2_cuda(double_sub_base, outsize, outsize);
	else
	    data_base(icp).sub_base_fft = fft2(double_sub_base, outsize, outsize);
	end

	data_base(icp).base_fractional_offset = base_points_for(icp,:) - round(base_points_for(icp,:)*PRECISION)/PRECISION;
    end
  end
end

%normxcorr_hw(hwid);
%return;

if OPTIMIZE > 2
    normxcorr_hw(hwid, 12, input_points_x, input_points_y);
else
    input_correl(:,1)=reshape(input_points_x,[],1);         % we reshape the input points to one row of values since this is the shape cpcorr will accept
    input_correl(:,2)=reshape(input_points_y,[],1);
end

input = uint8(mean(double(imread([imagedir, filenamelist((firstimage+1),:)])),3));       % read in the image which has to be correlated
for i=firstimage:(r-1)               % run through all images
    tic             % start the timer

%    input = uint8(mean(double(imread([imagedir, filenamelist((i+1),:)])),3));       % read in the image which has to be correlated

    if OPTIMIZE > 2
	%Validate findpeak
	%dic_cpcorr3(CORRSIZE, PRECISION, OPTIMIZE, hwid, ncp, input);
	normxcorr_hw(hwid, 13, input)
	if i < (r-1)
	    input = uint8(mean(double(imread([imagedir, filenamelist((i+2),:)])),3));       % read in the image which has to be correlated
	end
	input_correl = normxcorr_hw(hwid, 14);
    elseif OPTIMIZE > 0
	input_correl(:,:)=dic_cpcorr(CORRSIZE, PRECISION, OPTIMIZE, hwid, data_base, input_correl, input);
    else
	input_correl(:,:)=cpcorr(input_correl, base_points_for, input, base);
    end
    
    %We need to copy
    validx(:,i)=double(input_correl(:,1));                                       % the results we get from cpcorr for the x-direction
    validy(:,i)=double(input_correl(:,2));                                       % the results we get from cpcorr for the y-direction

    if (SAVEDATA)    
	savelinex=validx(:,i)';
	dlmwrite([datadir, 'resultsimcorrx.txt'], savelinex , 'delimiter', '\t', '-append');       % Here we save the result from each image; if you are desperately want to run this function with e.g. matlab 6.5 then you should comment this line out. If you do that the data will be saved at the end of the correlation step - good luck ;-)
    
	saveliney=validy(:,i)';
	dlmwrite([datadir, 'resultsimcorry.txt'], saveliney , 'delimiter', '\t', '-append');
    end
    
    if (~SILENT) 
	waitbar(i/(r-1))                                % update the waitbar

	imshow([imagedir, filenamelist(i+1,:)])                     % update image
	hold on
	plot(grid_x,grid_y,'g+')                                % plot start position of raster
	plot(input_correl(:,1),input_correl(:,2),'r+')        % plot actual postition of raster
	hold off
	drawnow
	time(i)=toc;                                                 % take time
	estimatedtime=sum(time)/i*(r-1);            % estimate time to process
	title(['# Im.: ', num2str((r-1)),'; Proc. Im. #: ', num2str((i)),'; # Rasterp.:',num2str(row*col), '; Est. Time [s] ', num2str(round(estimatedtime)), ';  Elapsed Time [s] ', num2str(round(sum(time)))]);    % plot a title onto the image
	drawnow
    end
end    

if OPTIMIZE > 2
    normxcorr_hw(hwid);
end

if (~SILENT)
    close(g)
end

close all

% save

if (~SILENT)
    save ([datadir,'time.dat'], 'time', '-ascii', '-tabs');
end

save ([datadir,'validx.dat'], 'validx', '-ascii', '-tabs');
save ([datadir,'validy.dat'], 'validy', '-ascii', '-tabs');
