% Code to generate the DIC analysis grid
% Programmed by Rob
% Last revision: 4/10/2007
clear all;
clc;
close(gcf);
% Load filenamelist; show the user the first image
load filenamelist;
p = figure;
hold on;
set(p,'Position',[150,50,800,600]); % [left bottom width height]
set(p,'PaperPositionMode','auto');
image(imread(filenamelist(1,:)));
axis image;
xlabel('x-Location on Image [Pixels]');
ylabel('y-Location on Image [Pixels]');
title(sprintf('Define the area of interest. Pick (single click) a point in the UPPER LEFT region\n of the sample first. Then, do the same for a point in the LOWER RIGHT region.'));
[xgrid,ygrid] = ginput(2);
xmin = xgrid(1);
xmax = xgrid(2);
ymin = ygrid(2);
ymax = ygrid(1);
closereq;
close(gcf);
% Prompt user for grid spacing/resolution
prompt = {'Enter the horizontal (x) spacing between correlation points [Pixels]:','Enter the vertical (y) spacing between correlation points [Pixels]:'};
dlg_title = 'Input correlation grid spacings';
num_lines = 1;
def = {'30','30'};
answer = inputdlg(prompt,dlg_title,num_lines,def);
xspacing = str2num(cell2mat(answer(1,1)));
yspacing = str2num(cell2mat(answer(2,1)));
% Warn user that Matlab may respond slowly
caut_selection = menu(sprintf('Caution: After this prompt, Matlab may appear unresponsive for a few seconds while the grid is being created.\n Please be patient. If no results are shown after 30 seconds, then use Control-C to restart the analysis.'),'OK');
% Round xmin, xmax and ymin, ymax "up" based on selected spacing
numXelem = ceil((xmax-xmin)/xspacing);
numYelem = ceil((ymax-ymin)/yspacing);
xmin_new = (xmax+xmin)/2-((numXelem/2)*xspacing);
xmax_new = (xmax+xmin)/2+((numXelem/2)*xspacing);
ymin_new = (ymax+ymin)/2-((numYelem/2)*yspacing);
ymax_new = (ymax+ymin)/2+((numYelem/2)*yspacing);
% Create the analysis grid and show user
[x,y] = meshgrid(xmin_new:xspacing:xmax_new,ymin_new:yspacing:ymax_new);
[grid_x_dim grid_y_dim] = size(x);
num_analysis_pts = grid_x_dim*grid_y_dim;
q = figure;
hold on;
set(q,'Position',[150,50,800,600]); % [left bottom width height]
set(q,'PaperPositionMode','auto');
image(imread(filenamelist(1,:)));
axis image;
xlabel('x-Location on Image [Pixels]');
ylabel('y-Location on Image [Pixels]');
title(['Number of Correlation Points: ',num2str(num_analysis_pts)]);
plot(x,y,'.r');
box on;
% Save settings, grid files, and plots in the image directory for visualization and plotting later
grid_pts = [reshape(x,[],1) reshape(y,[],1)];
save grid_settings.txt xspacing yspacing xmin_new xmax_new ymin_new ymax_new grid_x_dim grid_y_dim num_analysis_pts -ascii -tabs;
save grid_points.txt grid_pts -ascii -tabs;
saveas(q,'initial_grid.fig');
print -djpeg -r300 initial_grid;
% Check if grid generation results are acceptable
selection3 = menu(sprintf('Grid generation is complete and the results have been saved. Would you like to begin correlating the images?\n This may take a while depending on the speed of your computer, the number of images, and the grid resolution.\n If the grid shown is unsatisfactory and needs to be modified before proceeding, then redo grid generation.'),'Process Images','Redo Grid');
if selection3 == 1;
close(gcf);
RJT_process_images;
elseif selection3 == 2;
clear all;
clc;
RJT_grid_generator;
end;