/normxcorr/trunk

To get this branch, use:
bzr branch http://suren.me/webbzr/normxcorr/trunk
1 by Suren A. Chilingaryan
Initial import
1
function [validx,validy]=automate_image(grid_x,grid_y,filenamelist,validx,validy);
2
3
% Code to start actual image correlation
4
% Programmed by Chris and Rob
5
% Last revision: 09/10/08
6
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.
34
2 by Suren A. Chilingaryan
Support for different optimization modes
35
% Changed by Suren A. Chilingaryan <csa@dside.dyndns.org> to optimize performance
36
% by tighter integration with Matlab images toolkit (few sources from the toolkit
37
% are moved to the current source tree and adjusted to benefit from knowledge of 
38
% tasks we are solving here. As well CUDA framework, if available, is used to
39
% speed-up core computations using parallel processors of latest NVidia video
40
% cards.
41
% OPTIMIZE parameter is controlling which optimizations should be used. 
42
%  0 - The original version, no optimizations
43
%  1 - The modified sources from images toolkit are used
44
%  2 - The CUDA matlab plugin is used to compute FFT's
45
%  3 - Most of computations are performed on NVidia card
46
% You can safely set an optimization level to 3. If NVidia card is not available
47
% the level will be automatically reduced to 1.
48
49
OPTIMIZE = 3;
50
CORRSIZE = 15;	
1 by Suren A. Chilingaryan
Initial import
51
PRECISION = 1000;
4 by Suren A. Chilingaryan
Instead of transfer compute local sums and denormals on board
52
SILENT = 1;
1 by Suren A. Chilingaryan
Initial import
53
2 by Suren A. Chilingaryan
Support for different optimization modes
54
1 by Suren A. Chilingaryan
Initial import
55
% exist('grid_x')
56
% exist('grid_y')
57
% exist('filenamelist')
58
% exist('validx')
59
% exist('validy')
60
61
if exist('data', 'dir')
62
    datadir = 'data/';
63
else
64
    datadir = '';
65
end
66
67
if exist('images', 'dir')
68
    imagedir = 'images/';
69
else
70
    imagedir = '';
71
end
72
73
% Load necessary files
74
if exist('grid_x')==0
75
    load([datadir, 'grid_x.dat'])              % file with x position, created by grid_generator.m
76
end
77
if exist('grid_y')==0
78
    load([datadir, 'grid_y.dat'])              % file with y position, created by grid_generator.m
79
end
80
if exist('filenamelist')==0
81
    load([datadir, 'filenamelist'])            % file with the list of filenames to be processed
82
end
83
84
85
resume=0;
86
if exist('validx')==1
87
    if exist('validy')==1
88
        resume=1;
89
        [Rasternum Imagenum]=size(validx);
90
    end
91
end
92
93
% Initialize variables
94
input_points_x=grid_x;
95
base_points_x=grid_x;
96
97
input_points_y=grid_y;
98
base_points_y=grid_y;
99
100
if resume==1
101
    input_points_x=validx(:,Imagenum);
102
    input_points_y=validy(:,Imagenum);
103
    inputpoints=1;
104
end
105
106
[row,col]=size(base_points_x);      % this will determine the number of rasterpoints we have to run through
107
[r,c]=size(filenamelist);           % this will determine the number of images we have to loop through
108
109
110
% Open new figure so previous ones (if open) are not overwritten
111
if (~SILENT)
112
    h=figure;
113
    imshow([imagedir, filenamelist(1,:)])           % show the first image
114
    title('Initial Grid For Image Correlation (Note green crosses)')        % put a title
115
    hold on
116
    plot(grid_x,grid_y,'g+')            % plot the grid onto the image
117
    hold off
118
119
    % Start image correlation using cpcorr.m
120
    g = waitbar(0,sprintf('Processing images'));        % initialize the waitbar
121
    set(g,'Position',[275,50,275,50])                   % set the position of the waitbar [left bottom width height]
122
end
123
124
firstimage=1;
125
126
if resume==1
127
    firstimage=Imagenum+1
128
end
129
130
%Initializing hardware code
131
132
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
133
base_points_for(:,1)=reshape(base_points_x,[],1);
134
base_points_for(:,2)=reshape(base_points_y,[],1);
135
136
%some checks (moved here from cpcorr)
137
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)
138
    msg = sprintf('In function %s, Control Points must be in pixel coordinates.',mfilename);
139
    eid = sprintf('Images:%s:cpPointsMustBeInPixCoord',mfilename);
140
    error(eid, msg);
141
end
142
2 by Suren A. Chilingaryan
Support for different optimization modes
143
%moved from normxcorr2
144
% We can precalculate it here, since near-edge images are dropped and, therefore, A,T always have the same size 
145
% n is width and height of T, but following computations are done for A
146
% outsize is sum if widths and heights of T and A - 1 (outsize = size(A) + size(T) - 1)
147
outsize = 6 * CORRSIZE + 1;
148
n = 2*CORRSIZE + 1;
149
mn = n*n;
1 by Suren A. Chilingaryan
Initial import
150
151
rects_base = dic_calc_rects(base_points_for,2*CORRSIZE,base);
152
153
154
data_base = struct;
155
156
ncp = size(base_points_for, 1);
157
2 by Suren A. Chilingaryan
Support for different optimization modes
158
if OPTIMIZE > 2
159
    hwid = normxcorr_hw();
4 by Suren A. Chilingaryan
Instead of transfer compute local sums and denormals on board
160
2 by Suren A. Chilingaryan
Support for different optimization modes
161
    if hwid > 0
3 by Suren A. Chilingaryan
Add error checking to initialization process
162
	err = normxcorr_hw(hwid, 1, ncp, CORRSIZE);
163
	if (err ~= 0)
164
	    normxcorr_hw(1);
165
	    OPTIMIZE = 2;
166
	end
2 by Suren A. Chilingaryan
Support for different optimization modes
167
    else
168
	if hwid < 0
169
	    OPTIMIZE = 1;
170
	else
171
	    OPTIMIZE = 2;
172
	end
173
    end
174
else
175
    hwid = 0;
176
end
1 by Suren A. Chilingaryan
Initial import
177
4 by Suren A. Chilingaryan
Instead of transfer compute local sums and denormals on board
178
2 by Suren A. Chilingaryan
Support for different optimization modes
179
if OPTIMIZE > 0
4 by Suren A. Chilingaryan
Instead of transfer compute local sums and denormals on board
180
181
%  if OPTIMIZE > 2
182
%    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]
183
%    crop_size = (crop_dim(3) - crop_dim(1)) * (crop_dim(4) - crop_dim(2))
184
%    mesh_size = ncp * 4 * CORRSIZE * CORRSIZE
185
%  end
186
2 by Suren A. Chilingaryan
Support for different optimization modes
187
  for icp = 1:ncp
1 by Suren A. Chilingaryan
Initial import
188
    if isequal(rects_base(icp,3:4),[0 0])
189
	%near edge
190
	data_base(icp).skip = 1;
191
	continue
192
    end
193
194
    sub_base = imcrop(base,rects_base(icp,:));
195
    
196
    if any(~isfinite(sub_base(:)))
197
	%NaN or Inf
198
	data_base(icp).skip = 1;
199
	continue
200
    end
201
202
    data_base(icp).skip = 0;
2 by Suren A. Chilingaryan
Support for different optimization modes
203
204
    if (OPTIMIZE > 2)
4 by Suren A. Chilingaryan
Instead of transfer compute local sums and denormals on board
205
206
	if sum(sub_base(:)) == sub_base(1)*numel(sub_base)
207
	    eid = sprintf('Images:%s:sameElementsInTemplate',mfilename);
208
	    msg = 'The values of TEMPLATE cannot all be the same.';
209
	    error(eid,'%s',msg);
210
	end
211
	normxcorr_hw(hwid, 10, icp, sub_base);
212
%{
213
	double_sub_base = double(sub_base);
214
	local_sum_A = dic_local_sum(double_sub_base,n,n);
215
	local_sum_A2 = dic_local_sum(double_sub_base.*double_sub_base,n,n);
216
	denom_A = sqrt( max(( local_sum_A2 - (local_sum_A.^2)/mn ) / (mn-1), 0) );
217
	i_nonzero = find(denom_A~=0);
2 by Suren A. Chilingaryan
Support for different optimization modes
218
	normxcorr_hw(hwid, 10, icp, sub_base, single(local_sum_A), single(denom_A), uint16(i_nonzero));
4 by Suren A. Chilingaryan
Instead of transfer compute local sums and denormals on board
219
%}
2 by Suren A. Chilingaryan
Support for different optimization modes
220
    else
4 by Suren A. Chilingaryan
Instead of transfer compute local sums and denormals on board
221
	double_sub_base = double(sub_base);
222
223
	if (numel(sub_base) < 2) ||  (std(double_sub_base(:)) == 0)
224
	    % This check is moved here for normxcorr2
225
	    eid = sprintf('Images:%s:sameElementsInTemplate',mfilename);
226
	    msg = 'The values of TEMPLATE cannot all be the same.';
227
	    error(eid,'%s',msg);
228
	end
229
230
	local_sum_A = dic_local_sum(double_sub_base,n,n);
231
	local_sum_A2 = dic_local_sum(double_sub_base.*double_sub_base,n,n);
232
233
	% Note: diff_local_sums should be nonnegative, but may have negative
234
	% values due to round off errors. Below, we use max to ensure the
235
	% radicand is nonnegative.
236
	denom_A = sqrt( max(( local_sum_A2 - (local_sum_A.^2)/mn ) / (mn-1), 0) );
237
	i_nonzero = find(denom_A~=0);
238
    
2 by Suren A. Chilingaryan
Support for different optimization modes
239
	data_base(icp).denom_A = denom_A;
240
	data_base(icp).i_nonzero = i_nonzero;
241
242
	data_base(icp).local_sum_A = local_sum_A;
243
	data_base(icp).sub_base = sub_base;
244
	
245
	if (OPTIMIZE > 1)
246
	    data_base(icp).sub_base_fft = fft2_cuda(double_sub_base, outsize, outsize);
247
	else
248
	    data_base(icp).sub_base_fft = fft2(double_sub_base, outsize, outsize);
249
	end
250
    end
1 by Suren A. Chilingaryan
Initial import
251
252
    data_base(icp).base_fractional_offset = base_points_for(icp,:) - round(base_points_for(icp,:)*PRECISION)/PRECISION;
2 by Suren A. Chilingaryan
Support for different optimization modes
253
  end
1 by Suren A. Chilingaryan
Initial import
254
2 by Suren A. Chilingaryan
Support for different optimization modes
255
  if (OPTIMIZE > 2)
256
     normxcorr_hw(hwid, 2);
257
  end
1 by Suren A. Chilingaryan
Initial import
258
end
259
260
%normxcorr_hw(hwid);
261
%return;
262
263
264
for i=firstimage:(r-1)               % run through all images
265
    tic             % start the timer
266
267
    input = uint8(mean(double(imread([imagedir, filenamelist((i+1),:)])),3));       % read in the image which has to be correlated
268
    
269
    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
270
    input_points_for(:,2)=reshape(input_points_y,[],1);
271
2 by Suren A. Chilingaryan
Support for different optimization modes
272
    if OPTIMIZE > 0
273
	input_correl(:,:)=dic_cpcorr(CORRSIZE, PRECISION, OPTIMIZE, 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
274
    else
275
	input_correl(:,:)=cpcorr(input_points_for, base_points_for, input, base);
276
    end
1 by Suren A. Chilingaryan
Initial import
277
    input_correl_x=input_correl(:,1);                                       % the results we get from cpcorr for the x-direction
278
    input_correl_y=input_correl(:,2);                                       % the results we get from cpcorr for the y-direction
279
    
280
    
281
    validx(:,i)=input_correl_x;                                                     % lets save the data
282
    savelinex=input_correl_x';
283
    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 ;-)
284
    
285
    validy(:,i)=input_correl_y;
286
    saveliney=input_correl_y';
287
    dlmwrite([datadir, 'resultsimcorry.txt'], saveliney , 'delimiter', '\t', '-append');
288
    
289
    % Update base and input points for cpcorr.m
290
    input_points_x=input_correl_x;
291
    input_points_y=input_correl_y;
292
    
293
    if (~SILENT) 
294
	waitbar(i/(r-1))                                % update the waitbar
295
296
	imshow([imagedir, filenamelist(i+1,:)])                     % update image
297
	hold on
298
	plot(grid_x,grid_y,'g+')                                % plot start position of raster
299
	plot(input_correl_x,input_correl_y,'r+')        % plot actual postition of raster
300
	hold off
301
	drawnow
302
	time(i)=toc;                                                 % take time
303
	estimatedtime=sum(time)/i*(r-1);            % estimate time to process
304
	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
305
	drawnow
306
    end
307
    
308
end    
309
2 by Suren A. Chilingaryan
Support for different optimization modes
310
if OPTIMIZE > 2
311
    normxcorr_hw(hwid);
312
end
1 by Suren A. Chilingaryan
Initial import
313
314
if (~SILENT)
315
    close(g)
316
end
317
318
close all
319
320
% save
321
322
if (~SILENT)
323
    save ([datadir,'time.dat'], 'time', '-ascii', '-tabs');
324
end
325
326
save ([datadir,'validx.dat'], 'validx', '-ascii', '-tabs');
327
save ([datadir,'validy.dat'], 'validy', '-ascii', '-tabs');