/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-15 13:50:29 UTC
  • Revision ID: csa@dside.dyndns.org-20090115135029-wleapicg9a4593tp
Initial import

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
 
 
35
CORRSIZE = 15;
 
36
PRECISION = 1000;
 
37
SILENT = 1;
 
38
 
 
39
% exist('grid_x')
 
40
% exist('grid_y')
 
41
% exist('filenamelist')
 
42
% exist('validx')
 
43
% exist('validy')
 
44
 
 
45
if exist('data', 'dir')
 
46
    datadir = 'data/';
 
47
else
 
48
    datadir = '';
 
49
end
 
50
 
 
51
if exist('images', 'dir')
 
52
    imagedir = 'images/';
 
53
else
 
54
    imagedir = '';
 
55
end
 
56
 
 
57
% Load necessary files
 
58
if exist('grid_x')==0
 
59
    load([datadir, 'grid_x.dat'])              % file with x position, created by grid_generator.m
 
60
end
 
61
if exist('grid_y')==0
 
62
    load([datadir, 'grid_y.dat'])              % file with y position, created by grid_generator.m
 
63
end
 
64
if exist('filenamelist')==0
 
65
    load([datadir, 'filenamelist'])            % file with the list of filenames to be processed
 
66
end
 
67
 
 
68
 
 
69
resume=0;
 
70
if exist('validx')==1
 
71
    if exist('validy')==1
 
72
        resume=1;
 
73
        [Rasternum Imagenum]=size(validx);
 
74
    end
 
75
end
 
76
 
 
77
% Initialize variables
 
78
input_points_x=grid_x;
 
79
base_points_x=grid_x;
 
80
 
 
81
input_points_y=grid_y;
 
82
base_points_y=grid_y;
 
83
 
 
84
if resume==1
 
85
    input_points_x=validx(:,Imagenum);
 
86
    input_points_y=validy(:,Imagenum);
 
87
    inputpoints=1;
 
88
end
 
89
 
 
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
 
92
 
 
93
 
 
94
% Open new figure so previous ones (if open) are not overwritten
 
95
if (~SILENT)
 
96
    h=figure;
 
97
    imshow([imagedir, filenamelist(1,:)])           % show the first image
 
98
    title('Initial Grid For Image Correlation (Note green crosses)')        % put a title
 
99
    hold on
 
100
    plot(grid_x,grid_y,'g+')            % plot the grid onto the image
 
101
    hold off
 
102
 
 
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]
 
106
end
 
107
 
 
108
firstimage=1;
 
109
 
 
110
if resume==1
 
111
    firstimage=Imagenum+1
 
112
end
 
113
 
 
114
%Initializing hardware code
 
115
 
 
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);
 
119
 
 
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);
 
124
    error(eid, msg);
 
125
end
 
126
 
 
127
 
 
128
rects_base = dic_calc_rects(base_points_for,2*CORRSIZE,base);
 
129
 
 
130
 
 
131
data_base = struct;
 
132
 
 
133
%base=double(base);
 
134
 
 
135
ncp = size(base_points_for, 1);
 
136
 
 
137
hwid = normxcorr_hw();
 
138
normxcorr_hw(hwid, 1, ncp, CORRSIZE);
 
139
 
 
140
for icp = 1:ncp
 
141
    if isequal(rects_base(icp,3:4),[0 0])
 
142
        %near edge
 
143
        data_base(icp).skip = 1;
 
144
        continue
 
145
    end
 
146
 
 
147
    sub_base = imcrop(base,rects_base(icp,:));
 
148
    
 
149
    if any(~isfinite(sub_base(:)))
 
150
        %NaN or Inf
 
151
        data_base(icp).skip = 1;
 
152
        continue
 
153
    end
 
154
    
 
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.';
 
159
        error(eid,'%s',msg);
 
160
    end
 
161
 
 
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
    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);
 
171
 
 
172
 
 
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);
 
178
 
 
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
    data_base(icp).skip = 0;
 
188
    
 
189
    normxcorr_hw(hwid, 10, icp, sub_base, single(local_sum_A), single(denom_A), uint16(i_nonzero));
 
190
 
 
191
    data_base(icp).base_fractional_offset = base_points_for(icp,:) - round(base_points_for(icp,:)*PRECISION)/PRECISION;
 
192
 
 
193
end
 
194
 
 
195
%normxcorr_hw(hwid);
 
196
%return;
 
197
 
 
198
normxcorr_hw(hwid, 2);
 
199
 
 
200
for i=firstimage:(r-1)               % run through all images
 
201
    tic             % start the timer
 
202
 
 
203
    input = uint8(mean(double(imread([imagedir, filenamelist((i+1),:)])),3));       % read in the image which has to be correlated
 
204
    
 
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);
 
207
 
 
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
 
211
    
 
212
    
 
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 ;-)
 
216
    
 
217
    validy(:,i)=input_correl_y;
 
218
    saveliney=input_correl_y';
 
219
    dlmwrite([datadir, 'resultsimcorry.txt'], saveliney , 'delimiter', '\t', '-append');
 
220
    
 
221
    % Update base and input points for cpcorr.m
 
222
    input_points_x=input_correl_x;
 
223
    input_points_y=input_correl_y;
 
224
    
 
225
    if (~SILENT) 
 
226
        waitbar(i/(r-1))                                % update the waitbar
 
227
 
 
228
        imshow([imagedir, filenamelist(i+1,:)])                     % update image
 
229
        hold on
 
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
 
232
        hold off
 
233
        drawnow
 
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
 
237
        drawnow
 
238
    end
 
239
    
 
240
end    
 
241
 
 
242
normxcorr_hw(hwid);
 
243
 
 
244
if (~SILENT)
 
245
    close(g)
 
246
end
 
247
 
 
248
close all
 
249
 
 
250
% save
 
251
 
 
252
if (~SILENT)
 
253
    save ([datadir,'time.dat'], 'time', '-ascii', '-tabs');
 
254
end
 
255
 
 
256
save ([datadir,'validx.dat'], 'validx', '-ascii', '-tabs');
 
257
save ([datadir,'validy.dat'], 'validy', '-ascii', '-tabs');