144
119
%Initializing hardware code
146
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
147
base_points_for(:,1)=reshape(base_points_x,[],1);
148
base_points_for(:,2)=reshape(base_points_y,[],1);
150
%some checks (moved here from cpcorr)
151
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)
152
msg = sprintf('In function %s, Control Points must be in pixel coordinates.',mfilename);
153
eid = sprintf('Images:%s:cpPointsMustBeInPixCoord',mfilename);
157
%moved from normxcorr2
158
% We can precalculate it here, since near-edge images are dropped and, therefore, A,T always have the same size
159
% n is width and height of T, but following computations are done for A
160
% outsize is sum if widths and heights of T and A - 1 (outsize = size(A) + size(T) - 1)
161
outsize = 6 * CORRSIZE + 1;
165
rects_base = dic_calc_rects(base_points_for,2*CORRSIZE,base);
170
ncp = size(base_points_for, 1);
121
ncp = prod(size(grid_x));
173
124
hwid = normxcorr_hw();
176
err = normxcorr_hw(hwid, 1, ncp, CORRSIZE);
127
err = normxcorr_hw(hwid, 1, ncp, CORRSIZE, PRECISION);
143
% Initialize variables
145
base_points_x=single(grid_x);
146
base_points_y=single(grid_y);
148
base_points_x=grid_x;
149
base_points_y=grid_y;
154
input_points_x=single(validx(:,Imagenum));
155
input_points_y=single(validy(:,Imagenum));
157
input_points_x=validx(:,Imagenum);
158
input_points_y=validy(:,Imagenum);
163
input_points_x=single(grid_x);
164
input_points_y=single(grid_y);
166
input_points_x=grid_x;
167
input_points_y=grid_y;
171
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
196
174
% 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]
197
175
% crop_size = (crop_dim(3) - crop_dim(1)) * (crop_dim(4) - crop_dim(2))
198
176
% mesh_size = ncp * 4 * CORRSIZE * CORRSIZE
202
if isequal(rects_base(icp,3:4),[0 0])
204
data_base(icp).skip = 1;
178
% Verification of GPU local sum and denom computations
179
% dic_basecorr3(CORRSIZE, PRECISION, OPTIMIZE, hwid, base_points_x, base_points_y, base);
182
base_points_for(:,1)=reshape(base_points_x,[],1);
183
base_points_for(:,2)=reshape(base_points_y,[],1);
185
data_base(icp).base_fractional_offset = base_points_for(icp,:) - round(base_points_for(icp,:)*PRECISION)/PRECISION;
208
sub_base = imcrop(base,rects_base(icp,:));
188
normxcorr_hw(hwid, 3, base_points_x, base_points_y);
189
normxcorr_hw(hwid, 4, base);
191
normxcorr_hw(hwid, 5);
193
base_points_for(:,1)=reshape(base_points_x,[],1);
194
base_points_for(:,2)=reshape(base_points_y,[],1);
196
%some checks (moved here from cpcorr)
197
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)
198
msg = sprintf('In function %s, Control Points must be in pixel coordinates.',mfilename);
199
eid = sprintf('Images:%s:cpPointsMustBeInPixCoord',mfilename);
210
if any(~isfinite(sub_base(:)))
212
data_base(icp).skip = 1;
216
data_base(icp).skip = 0;
220
if sum(sub_base(:)) == sub_base(1)*numel(sub_base)
221
eid = sprintf('Images:%s:sameElementsInTemplate',mfilename);
222
msg = 'The values of TEMPLATE cannot all be the same.';
206
%moved from normxcorr2
207
% We can precalculate it here, since near-edge images are dropped and, therefore, A,T always have the same size
208
% n is width and height of T, but following computations are done for A
209
% outsize is sum if widths and heights of T and A - 1 (outsize = size(A) + size(T) - 1)
211
outsize = 6 * CORRSIZE + 1;
215
rects_base = dic_calc_rects(base_points_for,2*CORRSIZE,base);
218
if isequal(rects_base(icp,3:4),[0 0])
220
data_base(icp).skip = 1;
225
normxcorr_hw(hwid, 10, icp, sub_base);
227
double_sub_base = double(sub_base);
228
local_sum_A = dic_local_sum(double_sub_base,n,n);
229
local_sum_A2 = dic_local_sum(double_sub_base.*double_sub_base,n,n);
230
denom_A = sqrt( max(( local_sum_A2 - (local_sum_A.^2)/mn ) / (mn-1), 0) );
231
i_nonzero = find(denom_A~=0);
232
normxcorr_hw(hwid, 10, icp, sub_base, single(local_sum_A), single(denom_A), uint16(i_nonzero));
224
sub_base = imcrop(base,rects_base(icp,:));
226
data_base(icp).skip = 0;
235
228
double_sub_base = double(sub_base);
237
230
if (numel(sub_base) < 2) || (std(double_sub_base(:)) == 0)
262
255
data_base(icp).sub_base_fft = fft2(double_sub_base, outsize, outsize);
258
data_base(icp).base_fractional_offset = base_points_for(icp,:) - round(base_points_for(icp,:)*PRECISION)/PRECISION;
266
data_base(icp).base_fractional_offset = base_points_for(icp,:) - round(base_points_for(icp,:)*PRECISION)/PRECISION;
270
normxcorr_hw(hwid, 2);
274
263
%normxcorr_hw(hwid);
279
normxcorr_hw(hwid, 3, input_points_x, input_points_y);
267
normxcorr_hw(hwid, 12, input_points_x, input_points_y);
281
269
input_correl(:,1)=reshape(input_points_x,[],1); % we reshape the input points to one row of values since this is the shape cpcorr will accept
282
270
input_correl(:,2)=reshape(input_points_y,[],1);
285
273
for i=firstimage:(r-1) % run through all images
286
274
tic % start the timer
288
276
input = uint8(mean(double(imread([imagedir, filenamelist((i+1),:)])),3)); % read in the image which has to be correlated
291
input_correl(:,:)=dic_cpcorr3(CORRSIZE, PRECISION, OPTIMIZE, hwid, data_base, input_correl, input);
280
%dic_cpcorr3(CORRSIZE, PRECISION, OPTIMIZE, hwid, ncp, input);
282
normxcorr_hw(hwid, 13, input)
283
input_correl = normxcorr_hw(hwid, 14);
292
284
elseif OPTIMIZE > 0
293
285
input_correl(:,:)=dic_cpcorr(CORRSIZE, PRECISION, OPTIMIZE, hwid, data_base, input_correl, input);
295
287
input_correl(:,:)=cpcorr(input_correl, base_points_for, input, base);
297
291
validx(:,i)=double(input_correl(:,1)); % the results we get from cpcorr for the x-direction
298
292
validy(:,i)=double(input_correl(:,2)); % the results we get from cpcorr for the y-direction