1
function [validx,validy]=automate_image(grid_x,grid_y,filenamelist,validx,validy);
3
% Code to start actual image correlation
4
% Programmed by Chris and Rob
5
% Last revision: 09/10/08
7
% The automation function is the central function and processes all markers and
8
% images by the use of the matlab function cpcorr.m.
9
% Therefore the Current directory in matlab has to be the folder where
10
% automate_image.m finds the filenamelist.mat, grid_x.dat and grid_y.dat as well
11
% as the images specified in filenamelist.mat. Just type automate_image; and
12
% press ENTER at the command line of matlab.
13
% At first, automate_image.m will open the first image in the filenamelist.mat and
14
% plot the grid as green crosses on top. The next step will need some time since
15
% all markers in that image have to be processed for the first image. After correlating
16
% image one and two the new raster positions will be plotted as red crosses. On top
17
% of the image and the green crosses. The next dialog will ask you if you want to
18
% continue with this correlation or cancel. If you press continue, automate_image.m
19
% will process all images in the filenamelist.mat. The time it will take to process
20
% all images will be plotted on the figure but can easily be estimated by knowing the
21
% raster point processing speed (see processing speed).
22
% Depending on the number of images and markers you are tracking, this process
23
% can take between seconds and days. For 100 images and 200 markers a decent
24
% computer should need 200 seconds. To get a better resolution you can always
25
% run jobs overnight (e.g. 6000 markers in 1000 images) with higher resolutions.
26
% Keep in mind that CORRSIZE which you changed in cpcorr.m will limit your
27
% resolution. If you chose to use the 15 pixel as suggested a marker distance of
28
% 30 pixel will lead to a full cover of the strain field. Choosing smaller marker
29
% distances will lead to an interpolation since two neighboring markers share
30
% pixels. Nevertheless a higher marker density can reduce the noise of the strain field.
31
% When all images are processed, automate_image will write the files validx.mat,
32
% validy.mat, validx.txt and validy.txt. The text files are meant to store the result in a
33
% format which can be accessed by other programs also in the future.
41
% exist('filenamelist')
45
if exist('data', 'dir')
51
if exist('images', 'dir')
57
% Load necessary files
59
load([datadir, 'grid_x.dat']) % file with x position, created by grid_generator.m
62
load([datadir, 'grid_y.dat']) % file with y position, created by grid_generator.m
64
if exist('filenamelist')==0
65
load([datadir, 'filenamelist']) % file with the list of filenames to be processed
73
[Rasternum Imagenum]=size(validx);
77
% Initialize variables
78
input_points_x=grid_x;
81
input_points_y=grid_y;
85
input_points_x=validx(:,Imagenum);
86
input_points_y=validy(:,Imagenum);
90
[row,col]=size(base_points_x); % this will determine the number of rasterpoints we have to run through
91
[r,c]=size(filenamelist); % this will determine the number of images we have to loop through
94
% Open new figure so previous ones (if open) are not overwritten
97
imshow([imagedir, filenamelist(1,:)]) % show the first image
98
title('Initial Grid For Image Correlation (Note green crosses)') % put a title
100
plot(grid_x,grid_y,'g+') % plot the grid onto the image
103
% Start image correlation using cpcorr.m
104
g = waitbar(0,sprintf('Processing images')); % initialize the waitbar
105
set(g,'Position',[275,50,275,50]) % set the position of the waitbar [left bottom width height]
111
firstimage=Imagenum+1
114
%Initializing hardware code
116
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
117
base_points_for(:,1)=reshape(base_points_x,[],1);
118
base_points_for(:,2)=reshape(base_points_y,[],1);
120
%some checks (moved here from cpcorr)
121
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)
122
msg = sprintf('In function %s, Control Points must be in pixel coordinates.',mfilename);
123
eid = sprintf('Images:%s:cpPointsMustBeInPixCoord',mfilename);
128
rects_base = dic_calc_rects(base_points_for,2*CORRSIZE,base);
135
ncp = size(base_points_for, 1);
137
hwid = normxcorr_hw();
138
normxcorr_hw(hwid, 1, ncp, CORRSIZE);
141
if isequal(rects_base(icp,3:4),[0 0])
143
data_base(icp).skip = 1;
147
sub_base = imcrop(base,rects_base(icp,:));
149
if any(~isfinite(sub_base(:)))
151
data_base(icp).skip = 1;
155
if (numel(sub_base) < 2) || (std(sub_base(:)) == 0)
156
% This check is moved here for normxcorr2
157
eid = sprintf('Images:%s:sameElementsInTemplate',mfilename);
158
msg = 'The values of TEMPLATE cannot all be the same.';
162
%moved from normxcorr2
163
% We can precalculate it here, since near-edge images are dropped and, therefore, A,T always have the same size
164
% n is width and height of T, but following computations are done for A
168
double_sub_base = double(sub_base);
169
local_sum_A = dic_local_sum(double_sub_base,n,n);
170
local_sum_A2 = dic_local_sum(double_sub_base.*double_sub_base,n,n);
173
% Note: diff_local_sums should be nonnegative, but may have negative
174
% values due to round off errors. Below, we use max to ensure the
175
% radicand is nonnegative.
176
denom_A = sqrt( max(( local_sum_A2 - (local_sum_A.^2)/mn ) / (mn-1), 0) );
177
i_nonzero = find(denom_A~=0);
180
% data_base(icp).denom_A = denom_A;
181
% data_base(icp).i_nonzero = i_nonzero;
183
% data_base(icp).local_sum_A = local_sum_A;
184
% data_base(icp).sub_base = sub_base;
187
data_base(icp).skip = 0;
189
normxcorr_hw(hwid, 10, icp, sub_base, single(local_sum_A), single(denom_A), uint16(i_nonzero));
191
data_base(icp).base_fractional_offset = base_points_for(icp,:) - round(base_points_for(icp,:)*PRECISION)/PRECISION;
198
normxcorr_hw(hwid, 2);
200
for i=firstimage:(r-1) % run through all images
201
tic % start the timer
203
input = uint8(mean(double(imread([imagedir, filenamelist((i+1),:)])),3)); % read in the image which has to be correlated
205
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
206
input_points_for(:,2)=reshape(input_points_y,[],1);
208
input_correl(:,:)=dic_cpcorr(CORRSIZE, PRECISION, hwid, data_base, input_points_for, input); % 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
209
input_correl_x=input_correl(:,1); % the results we get from cpcorr for the x-direction
210
input_correl_y=input_correl(:,2); % the results we get from cpcorr for the y-direction
213
validx(:,i)=input_correl_x; % lets save the data
214
savelinex=input_correl_x';
215
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 ;-)
217
validy(:,i)=input_correl_y;
218
saveliney=input_correl_y';
219
dlmwrite([datadir, 'resultsimcorry.txt'], saveliney , 'delimiter', '\t', '-append');
221
% Update base and input points for cpcorr.m
222
input_points_x=input_correl_x;
223
input_points_y=input_correl_y;
226
waitbar(i/(r-1)) % update the waitbar
228
imshow([imagedir, filenamelist(i+1,:)]) % update image
230
plot(grid_x,grid_y,'g+') % plot start position of raster
231
plot(input_correl_x,input_correl_y,'r+') % plot actual postition of raster
234
time(i)=toc; % take time
235
estimatedtime=sum(time)/i*(r-1); % estimate time to process
236
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
253
save ([datadir,'time.dat'], 'time', '-ascii', '-tabs');
256
save ([datadir,'validx.dat'], 'validx', '-ascii', '-tabs');
257
save ([datadir,'validy.dat'], 'validy', '-ascii', '-tabs');