function [validx2,validy2]=automate_image_session2(grid_x2,grid_y2,filenamelist)
% Code to start actual image correlation
% Programmed by Chris and Rob
% Last revision: 9/19/06
% 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.
% automate_image_session1 and automate_image_session2 are called by the
% master function automate_image_twothreads and open grid_x1.dat or
% grid_x2.dat as well as filenamelist.m and processes the job. Since both
% threads are running independent from each other, computers with dual core
% processors or two processor computers can run jobs twice as fast as
% single core/singleprocessor computers.
clear
if exist('workdirectory.dat','file')==2
load('workdirectory.dat');
cd(char(workdirectory));
end
% Load necessary files
if exist('grid_x2')==0
load('grid_x2.dat') % file with x position, created by grid_generator.m
end
if exist('grid_y2')==0
load('grid_y2.dat') % file with y position, created by grid_generator.m
end
if exist('filenamelist')==0
load('filenamelist') % file with the list of filenames to be processed
end
grid_x=grid_x2;
grid_y=grid_y2;
% Initialize variables
input_points_x=grid_x;
base_points_x=grid_x;
input_points_y=grid_y;
base_points_y=grid_y;
[row,col]=size(base_points_x); % this will determine the number of rasterpoints we have to run through
[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
h=figure;
imshow(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 teh waitbar [left bottom width height]
for i=1:(r-1) % run through all images
tic % start the timer
base = uint8(mean(double(imread(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
input = uint8(mean(double(imread(filenamelist((i+1),:))),3)); % read in the image which has to be correlated
input_points_for(:,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_points_for(:,2)=reshape(input_points_y,[],1);
base_points_for(:,1)=reshape(base_points_x,[],1);
base_points_for(:,2)=reshape(base_points_y,[],1);
input_correl(:,:)=cpcorr(input_points_for, base_points_for, input, base); % here we go and give all the markers and images to process to cpcorr.m which ic a function provided by the matlab image processing toolbox
input_correl_x=input_correl(:,1); % the results we get from cpcorr for the x-direction
input_correl_y=input_correl(:,2); % the results we get from cpcorr for the y-direction
validx(:,i)=input_correl_x; % lets save the data
savelinex=input_correl_x';
dlmwrite('resultsimcorrx2.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 ;-)
validy(:,i)=input_correl_y;
saveliney=input_correl_y';
dlmwrite('resultsimcorry2.txt', saveliney , 'delimiter', '\t', '-append');
waitbar(i/(r-1)) % update the waitbar
% Update base and input points for cpcorr.m
base_points_x=grid_x;
base_points_y=grid_y;
input_points_x=input_correl_x;
input_points_y=input_correl_y;
imshow(filenamelist(i+1,:)) % update image
hold on
plot(grid_x,grid_y,'g+') % plot start position of raster
plot(input_correl_x,input_correl_y,'r+') % plot actual postition of raster
hold off
drawnow
time(i)=toc; % take time
estimatedtime=sum(time)/i*(r-1); % estimate time to process
[h,m,s] = hms2mat(timedim(estimatedtime,'seconds','hms'));
title(['# Images: ', num2str((r-1)),'; # rasterpoints:',num2str(row*col), '; Est. Time [h:m:s] ', num2str(h),':',num2str(m),':',num2str(s)]) % plot a title onto the image
drawnow
end
close(g)
close all
validx2=validx;
validy2=validy;
end2='done';
% save
save time2.dat time -ascii -tabs
save validx2.dat validx2 -ascii -tabs
save validy2.dat validy2 -ascii -tabs
save end2.txt end2 -ascii
exit