/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-01-16 23:37:17 UTC
  • Revision ID: csa@dside.dyndns.org-20090116233717-ycbj2qqk5bdyed6a
Support for different optimization modes

Show diffs side-by-side

added added

removed removed

Lines of Context:
32
32
% validy.mat, validx.txt and validy.txt. The text files are meant to store the result in a 
33
33
% format which can be accessed by other programs also in the future.
34
34
 
35
 
CORRSIZE = 15;
 
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;  
36
51
PRECISION = 1000;
37
52
SILENT = 1;
38
53
 
 
54
 
39
55
% exist('grid_x')
40
56
% exist('grid_y')
41
57
% exist('filenamelist')
124
140
    error(eid, msg);
125
141
end
126
142
 
 
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;
127
150
 
128
151
rects_base = dic_calc_rects(base_points_for,2*CORRSIZE,base);
129
152
 
130
153
 
131
154
data_base = struct;
132
155
 
133
 
%base=double(base);
134
 
 
135
156
ncp = size(base_points_for, 1);
136
157
 
137
 
hwid = normxcorr_hw();
138
 
normxcorr_hw(hwid, 1, ncp, CORRSIZE);
 
158
if OPTIMIZE > 2
 
159
    hwid = normxcorr_hw();
 
160
    if hwid > 0
 
161
        normxcorr_hw(hwid, 1, ncp, CORRSIZE);
 
162
    else
 
163
        if hwid < 0
 
164
            OPTIMIZE = 1;
 
165
        else
 
166
            OPTIMIZE = 2;
 
167
        end
 
168
    end
 
169
else
 
170
    hwid = 0;
 
171
end
139
172
 
140
 
for icp = 1:ncp
 
173
if OPTIMIZE > 0
 
174
  for icp = 1:ncp
141
175
    if isequal(rects_base(icp,3:4),[0 0])
142
176
        %near edge
143
177
        data_base(icp).skip = 1;
159
193
        error(eid,'%s',msg);
160
194
    end
161
195
 
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
165
 
    n = 2*CORRSIZE + 1;
166
 
    mn = n*n;
167
 
 
168
196
    double_sub_base = double(sub_base);
169
197
    local_sum_A = dic_local_sum(double_sub_base,n,n);
170
198
    local_sum_A2 = dic_local_sum(double_sub_base.*double_sub_base,n,n);
171
199
 
172
 
 
173
200
    % Note: diff_local_sums should be nonnegative, but may have negative
174
201
    % values due to round off errors. Below, we use max to ensure the
175
202
    % radicand is nonnegative.
176
203
    denom_A = sqrt( max(( local_sum_A2 - (local_sum_A.^2)/mn ) / (mn-1), 0) );
177
204
    i_nonzero = find(denom_A~=0);
178
205
 
179
 
%start comment    
180
 
%    data_base(icp).denom_A = denom_A;
181
 
%    data_base(icp).i_nonzero = i_nonzero;
182
 
    
183
 
%    data_base(icp).local_sum_A = local_sum_A;
184
 
%    data_base(icp).sub_base = sub_base;
185
 
%end comment
186
 
 
187
206
    data_base(icp).skip = 0;
188
 
    
189
 
    normxcorr_hw(hwid, 10, icp, sub_base, single(local_sum_A), single(denom_A), uint16(i_nonzero));
 
207
 
 
208
    if (OPTIMIZE > 2)
 
209
        normxcorr_hw(hwid, 10, icp, sub_base, single(local_sum_A), single(denom_A), uint16(i_nonzero));
 
210
    else
 
211
        data_base(icp).denom_A = denom_A;
 
212
        data_base(icp).i_nonzero = i_nonzero;
 
213
 
 
214
        data_base(icp).local_sum_A = local_sum_A;
 
215
        data_base(icp).sub_base = sub_base;
 
216
        
 
217
        if (OPTIMIZE > 1)
 
218
            data_base(icp).sub_base_fft = fft2_cuda(double_sub_base, outsize, outsize);
 
219
        else
 
220
            data_base(icp).sub_base_fft = fft2(double_sub_base, outsize, outsize);
 
221
        end
 
222
    end
190
223
 
191
224
    data_base(icp).base_fractional_offset = base_points_for(icp,:) - round(base_points_for(icp,:)*PRECISION)/PRECISION;
 
225
  end
192
226
 
 
227
  if (OPTIMIZE > 2)
 
228
     normxcorr_hw(hwid, 2);
 
229
  end
193
230
end
194
231
 
195
232
%normxcorr_hw(hwid);
196
233
%return;
197
234
 
198
 
normxcorr_hw(hwid, 2);
199
235
 
200
236
for i=firstimage:(r-1)               % run through all images
201
237
    tic             % start the timer
205
241
    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
242
    input_points_for(:,2)=reshape(input_points_y,[],1);
207
243
 
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
 
244
    if OPTIMIZE > 0
 
245
        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
 
246
    else
 
247
        input_correl(:,:)=cpcorr(input_points_for, base_points_for, input, base);
 
248
    end
209
249
    input_correl_x=input_correl(:,1);                                       % the results we get from cpcorr for the x-direction
210
250
    input_correl_y=input_correl(:,2);                                       % the results we get from cpcorr for the y-direction
211
251
    
239
279
    
240
280
end    
241
281
 
242
 
normxcorr_hw(hwid);
 
282
if OPTIMIZE > 2
 
283
    normxcorr_hw(hwid);
 
284
end
243
285
 
244
286
if (~SILENT)
245
287
    close(g)