/normxcorr/trunk

To get this branch, use:
bzr branch http://suren.me/webbzr/normxcorr/trunk
2 by Suren A. Chilingaryan
Support for different optimization modes
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
% Minor changes by Suren A. Chilingaryan <csa@dside.dyndns.org> to look for data
36
% files in sub directories.
37
38
 
39
% exist('grid_x')
40
% exist('grid_y')
41
% exist('filenamelist')
42
% exist('validx')
43
% exist('validy')
44
45
46
if exist('data', 'dir')
47
    datadir = 'data/';
48
else
49
    datadir = '';
50
end
51
52
if exist('images', 'dir')
53
    imagedir = 'images/';
54
else
55
    imagedir = '';
56
end
57
58
% Load necessary files
59
if exist('grid_x')==0
60
    load([datadir, 'grid_x.dat'])   % file with x position, created by grid_generator.m
61
end
62
if exist('grid_y')==0
63
    load([datadir, 'grid_y.dat'])   % file with y position, created by grid_generator.m
64
end
65
if exist('filenamelist')==0
66
    load([datadir, 'filenamelist']) % file with the list of filenames to be processed
67
end
68
resume=0;
69
if exist('validx')==1
70
    if exist('validy')==1
71
        resume=1;
72
        [Rasternum Imagenum]=size(validx);
73
    end
74
end
75
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
h=figure;
96
imshow([imagedir, filenamelist(1,:)])           % show the first image
97
title('Initial Grid For Image Correlation (Note green crosses)')        % put a title
98
hold on
99
plot(grid_x,grid_y,'g+')            % plot the grid onto the image
100
hold off
101
102
% Start image correlation using cpcorr.m
103
g = waitbar(0,sprintf('Processing images'));        % initialize the waitbar
104
set(g,'Position',[275,50,275,50])                               % set the position of the waitbar [left bottom width height]
105
firstimage=1;
106
107
if resume==1
108
    firstimage=Imagenum+1
109
end
110
111
for i=firstimage:(r-1)               % run through all images
112
    
113
    
114
    tic             % start the timer
115
    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
116
    input = uint8(mean(double(imread([imagedir, filenamelist((i+1),:)])),3));       % read in the image which has to be correlated
117
    
118
    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
119
    input_points_for(:,2)=reshape(input_points_y,[],1);
120
    base_points_for(:,1)=reshape(base_points_x,[],1);
121
    base_points_for(:,2)=reshape(base_points_y,[],1);
122
    input_correl(:,:)=cpcorr(input_points_for, base_points_for, input, base);           % 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
123
    input_correl_x=input_correl(:,1);                                       % the results we get from cpcorr for the x-direction
124
    input_correl_y=input_correl(:,2);                                       % the results we get from cpcorr for the y-direction
125
    
126
    
127
    validx(:,i)=input_correl_x;                                                     % lets save the data
128
    savelinex=input_correl_x';
129
    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 ;-)
130
    
131
    validy(:,i)=input_correl_y;
132
    saveliney=input_correl_y';
133
    dlmwrite([datadir, 'resultsimcorry.txt'], saveliney , 'delimiter', '\t', '-append');
134
    
135
    waitbar(i/(r-1))                                                                        % update the waitbar
136
    
137
    % Update base and input points for cpcorr.m
138
    base_points_x=grid_x;
139
    base_points_y=grid_y;
140
    input_points_x=input_correl_x;
141
    input_points_y=input_correl_y;
142
    
143
    imshow([imagedir, filenamelist(i+1,:)])                 % update image
144
    hold on
145
    plot(grid_x,grid_y,'g+')                                % plot start position of raster
146
    plot(input_correl_x,input_correl_y,'r+')        % plot actual postition of raster
147
    hold off
148
    drawnow
149
    time(i)=toc;                                                 % take time
150
    estimatedtime=sum(time)/i*(r-1);            % estimate time to process
151
    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
152
    drawnow
153
    
154
end    
155
156
close(g)
157
close all
158
159
% save
160
161
save ([datadir, 'time.dat'], 'time', '-ascii', '-tabs')
162
save ([datadir, 'validx.dat'], 'validx', '-ascii', '-tabs')
163
save ([datadir, 'validy.dat'], 'validy', '-ascii', '-tabs')