% Code to start actual image correlation
% Programmed by Rob
% Last revision: 4/14/2007
clear all;
clc;
close(gcf);
% Load necessary files
load grid_points.txt;
load filenamelist;
% Initialize variables
input_points(:,1) = grid_points(:,1);
input_points(:,2) = grid_points(:,2);
validx(:,1) = grid_points(:,1);
validy(:,1) = grid_points(:,2);
[r,c] = size(filenamelist);
% Ask user what type of correlation analysis should be performed
corr_selection = menu(sprintf('Should Matlab perform the correlation analysis between consecutive images\n or should correlation always be done with respect to the first image?'),'Consecutive Correlation','First Image Correlation');
% Open waitbar and new figure so previous ones (if open) are not overwritten
h = figure;
hold on;
set(h,'Position',[150,50,800,600]); % [left bottom width height]
set(h,'PaperPositionMode','auto');
image(imread(filenamelist(1,:)));
axis image;
plot(input_points(:,1),input_points(:,2),'.r');
xlabel('x-Location on Image [Pixels]');
ylabel('y-Location on Image [Pixels]');
box on;
g = waitbar(0,sprintf('Overall image correlation progress'));
% Warn user that Matlab may respond slowly once processing is started
caution_selection = menu(sprintf('Caution: If a large corr size is being used for the correlation analysis (note cpcorr.m), Matlab may respond slowly and unusually.\n Please check your Task Manager, verify the processor load, and be patient. If no results are shown after 3 or 4 minutes, use Control-C to restart the analysis.'),'OK');
% Start image correlation using cpcorr.m (located in Image Processing Toolbox)
tic; % Start time counter
% Correlate between consecutive images
if corr_selection == 1;
for i = 2:r;
base = uint8(mean(double(imread(filenamelist((i-1),:))),3));
input = uint8(mean(double(imread(filenamelist(i,:))),3));
adjusted_corr_output = cpcorr(input_points,input_points,input,base);
% Save (backup) results after each iteration in case of system crashes
validx(:,i) = adjusted_corr_output(:,1);
savelinex = (adjusted_corr_output(:,1))';
dlmwrite('validx_backup.txt',savelinex,'delimiter','\t','-append');
validy(:,i) = adjusted_corr_output(:,2);
saveliney = (adjusted_corr_output(:,2))';
dlmwrite('validy_backup.txt',saveliney,'delimiter','\t','-append');
% Plot the current iteration results
plot(adjusted_corr_output(:,1),adjusted_corr_output(:,2),'.r');
drawnow;
elapsed_time = round(toc*10/60)/10; % Time in minutes
title([sprintf('Processing Complete for Image: %10s',filenamelist(i,:)),' Elapsed Time [min]: ',num2str(elapsed_time)]);
drawnow;
% Update correlation points and waitbar for next iteration
input_points = adjusted_corr_output;
waitbar(i/r);
end;
end;
% Correlate with respect to first image
if corr_selection == 2;
base_points(:,1) = validx(:,1);
base_points(:,2) = validy(:,1);
for i = 2:r;
base = uint8(mean(double(imread(filenamelist(1,:))),3));
input = uint8(mean(double(imread(filenamelist(i,:))),3));
adjusted_corr_output = cpcorr(input_points,base_points,input,base);
% Save (backup) results after each iteration in case of system crashes
validx(:,i) = adjusted_corr_output(:,1);
savelinex = (adjusted_corr_output(:,1))';
dlmwrite('validx_backup.txt',savelinex,'delimiter','\t','-append');
validy(:,i) = adjusted_corr_output(:,2);
saveliney = (adjusted_corr_output(:,2))';
dlmwrite('validy_backup.txt',saveliney,'delimiter','\t','-append');
% Plot the current iteration results
plot(adjusted_corr_output(:,1),adjusted_corr_output(:,2),'.r');
drawnow;
elapsed_time = round(toc*10/60)/10; % Time in minutes
title([sprintf('Processing Complete for Image: %10s',filenamelist(i,:)),' Elapsed Time [min]: ',num2str(elapsed_time)]);
drawnow;
% Update correlation points and waitbar for next iteration
input_points = adjusted_corr_output;
waitbar(i/r);
end;
end;
% Save the completed correlation data as well as a plot of the results
save validx.txt validx -ascii -tabs;
save validy.txt validy -ascii -tabs;
saveas(h,'correlation_results.fig');
print -djpeg -r300 correlation_results;
close(g);
% Prompt user to either proceed or exit Matlab
selection4 = menu(sprintf('Processing of all the images is complete and the correlation results have been saved.\n Would you like to begin analyzing and plotting the raw correlation results now or later?'),'Create Plots','Exit');
if selection4 == 1;
close(gcf);
RJT_process_results;
elseif selection4 == 2;
exit;
end;