/normxcorr/trunk

To get this branch, use:
bzr branch http://suren.me/webbzr/normxcorr/trunk

« back to all changes in this revision

Viewing changes to automate_image.m

  • Committer: Suren A. Chilingaryan
  • Date: 2009-12-09 13:00:50 UTC
  • Revision ID: csa@dside.dyndns.org-20091209130050-z27djqs8ed68fqnk
Complete elimination of cpcorr

Show diffs side-by-side

added added

removed removed

Lines of Context:
94
94
    end
95
95
end
96
96
 
97
 
% Initialize variables
98
 
base_points_x=grid_x;
99
 
base_points_y=grid_y;
100
 
 
101
 
if resume==1
102
 
    if OPTIMIZE>2
103
 
        input_points_x=single(validx(:,Imagenum));
104
 
        input_points_y=single(validy(:,Imagenum));
105
 
    else
106
 
        input_points_x=validx(:,Imagenum);
107
 
        input_points_y=validy(:,Imagenum);
108
 
    end
109
 
    inputpoints=1;
110
 
else
111
 
    if OPTIMIZE>2
112
 
        input_points_x=single(grid_x);
113
 
        input_points_y=single(grid_y);
114
 
    else
115
 
        input_points_x=grid_x;
116
 
        input_points_y=grid_y;
117
 
    end
118
 
end
119
 
 
120
 
[row,col]=size(base_points_x);      % this will determine the number of rasterpoints we have to run through
121
97
[r,c]=size(filenamelist);           % this will determine the number of images we have to loop through
122
98
 
123
 
 
124
99
% Open new figure so previous ones (if open) are not overwritten
125
100
if (~SILENT)
126
101
    h=figure;
143
118
 
144
119
%Initializing hardware code
145
120
 
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);
149
 
 
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);
154
 
    error(eid, msg);
155
 
end
156
 
 
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;
162
 
n = 2*CORRSIZE + 1;
163
 
mn = n*n;
164
 
 
165
 
rects_base = dic_calc_rects(base_points_for,2*CORRSIZE,base);
166
 
 
167
 
 
168
 
data_base = struct;
169
 
 
170
 
ncp = size(base_points_for, 1);
 
121
ncp = prod(size(grid_x));
171
122
 
172
123
if OPTIMIZE > 2
173
124
    hwid = normxcorr_hw();
174
125
 
175
126
    if hwid > 0
176
 
        err = normxcorr_hw(hwid, 1, ncp, CORRSIZE);
 
127
        err = normxcorr_hw(hwid, 1, ncp, CORRSIZE, PRECISION);
177
128
        if (err ~= 0)
178
129
            normxcorr_hw(1);
179
130
            OPTIMIZE = 2;
189
140
    hwid = 0;
190
141
end
191
142
 
192
 
 
193
 
if OPTIMIZE > 0
194
 
 
195
 
%  if OPTIMIZE > 2
 
143
% Initialize variables
 
144
if OPTIMIZE>2
 
145
    base_points_x=single(grid_x);
 
146
    base_points_y=single(grid_y);
 
147
else
 
148
    base_points_x=grid_x;
 
149
    base_points_y=grid_y;
 
150
end
 
151
 
 
152
if resume==1
 
153
    if OPTIMIZE>2
 
154
        input_points_x=single(validx(:,Imagenum));
 
155
        input_points_y=single(validy(:,Imagenum));
 
156
    else
 
157
        input_points_x=validx(:,Imagenum);
 
158
        input_points_y=validy(:,Imagenum);
 
159
    end
 
160
    inputpoints=1;
 
161
else
 
162
    if OPTIMIZE>2
 
163
        input_points_x=single(grid_x);
 
164
        input_points_y=single(grid_y);
 
165
    else
 
166
        input_points_x=grid_x;
 
167
        input_points_y=grid_y;
 
168
    end
 
169
end
 
170
 
 
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
 
172
 
 
173
if OPTIMIZE > 2
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
199
 
%  end
200
 
 
201
 
  for icp = 1:ncp
202
 
    if isequal(rects_base(icp,3:4),[0 0])
203
 
        %near edge
204
 
        data_base(icp).skip = 1;
205
 
        continue
 
177
 
 
178
%    Verification of GPU local sum and denom computations
 
179
%    dic_basecorr3(CORRSIZE, PRECISION, OPTIMIZE, hwid, base_points_x, base_points_y, base);
 
180
 
 
181
    data_base = struct;
 
182
    base_points_for(:,1)=reshape(base_points_x,[],1);
 
183
    base_points_for(:,2)=reshape(base_points_y,[],1);
 
184
    for icp = 1:ncp
 
185
        data_base(icp).base_fractional_offset = base_points_for(icp,:) - round(base_points_for(icp,:)*PRECISION)/PRECISION;
206
186
    end
207
187
 
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);
 
190
 
 
191
    normxcorr_hw(hwid, 5);
 
192
else
 
193
  base_points_for(:,1)=reshape(base_points_x,[],1);
 
194
  base_points_for(:,2)=reshape(base_points_y,[],1);
 
195
 
 
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);
 
200
    error(eid, msg);
 
201
  end
 
202
 
 
203
  if OPTIMIZE > 0
 
204
     data_base = struct;
209
205
    
210
 
    if any(~isfinite(sub_base(:)))
211
 
        %NaN or Inf
212
 
        data_base(icp).skip = 1;
213
 
        continue
214
 
    end
215
 
 
216
 
    data_base(icp).skip = 0;
217
 
 
218
 
    if (OPTIMIZE > 2)
219
 
 
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.';
223
 
            error(eid,'%s',msg);
 
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)
 
210
 
 
211
     outsize = 6 * CORRSIZE + 1;
 
212
     n = 2*CORRSIZE + 1;
 
213
     mn = n*n;
 
214
 
 
215
     rects_base = dic_calc_rects(base_points_for,2*CORRSIZE,base);
 
216
 
 
217
     for icp = 1:ncp
 
218
        if isequal(rects_base(icp,3:4),[0 0])
 
219
            %near edge
 
220
            data_base(icp).skip = 1;
 
221
            continue
224
222
        end
225
 
        normxcorr_hw(hwid, 10, icp, sub_base);
226
 
%{
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));
233
 
%}
234
 
    else
 
223
 
 
224
        sub_base = imcrop(base,rects_base(icp,:));
 
225
    
 
226
        data_base(icp).skip = 0;
 
227
 
235
228
        double_sub_base = double(sub_base);
236
229
 
237
230
        if (numel(sub_base) < 2) ||  (std(double_sub_base(:)) == 0)
261
254
        else
262
255
            data_base(icp).sub_base_fft = fft2(double_sub_base, outsize, outsize);
263
256
        end
 
257
 
 
258
        data_base(icp).base_fractional_offset = base_points_for(icp,:) - round(base_points_for(icp,:)*PRECISION)/PRECISION;
264
259
    end
265
 
 
266
 
    data_base(icp).base_fractional_offset = base_points_for(icp,:) - round(base_points_for(icp,:)*PRECISION)/PRECISION;
267
 
  end
268
 
 
269
 
  if (OPTIMIZE > 2)
270
 
     normxcorr_hw(hwid, 2);
271
260
  end
272
261
end
273
262
 
274
263
%normxcorr_hw(hwid);
275
264
%return;
276
265
 
277
 
 
278
 
%if OPTIMIZE > 2
279
 
    normxcorr_hw(hwid, 3, input_points_x, input_points_y);
280
 
%else
 
266
if OPTIMIZE > 2
 
267
    normxcorr_hw(hwid, 12, input_points_x, input_points_y);
 
268
else
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);
283
 
%end
 
271
end
284
272
 
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
289
277
 
290
278
    if OPTIMIZE > 2
291
 
        input_correl(:,:)=dic_cpcorr3(CORRSIZE, PRECISION, OPTIMIZE, hwid, data_base, input_correl, input);
 
279
        %Validate findpeak
 
280
        %dic_cpcorr3(CORRSIZE, PRECISION, OPTIMIZE, hwid, ncp, input);
 
281
 
 
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);
294
286
    else
295
287
        input_correl(:,:)=cpcorr(input_correl, base_points_for, input, base);
296
288
    end
 
289
 
 
290
    %We need to copy
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
299
293