/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('images', 'dir')
47
    imagedir = 'images/';
48
else
49
    imagedir = '';
50
end
51
31 by Suren A. Chilingaryan
CUDAfication of real-time module
52
if exist('data', 'dir')
53
    datadir = 'data/';
54
else
55
    if exist([imagedir, 'grid_x.dat'], 'file')
56
	datadir = imagedir;
57
    else
58
	datadir = '';
59
    end
60
end
61
62
2 by Suren A. Chilingaryan
Support for different optimization modes
63
% Load necessary files
64
if exist('grid_x')==0
65
    load([datadir, 'grid_x.dat'])   % file with x position, created by grid_generator.m
66
end
67
if exist('grid_y')==0
68
    load([datadir, 'grid_y.dat'])   % file with y position, created by grid_generator.m
69
end
70
if exist('filenamelist')==0
71
    load([datadir, 'filenamelist']) % file with the list of filenames to be processed
72
end
73
resume=0;
74
if exist('validx')==1
75
    if exist('validy')==1
76
        resume=1;
77
        [Rasternum Imagenum]=size(validx);
78
    end
79
end
80
81
82
% Initialize variables
83
input_points_x=grid_x;
84
base_points_x=grid_x;
85
86
input_points_y=grid_y;
87
base_points_y=grid_y;
88
89
if resume==1
90
    input_points_x=validx(:,Imagenum);
91
    input_points_y=validy(:,Imagenum);
92
    inputpoints=1;
93
end
94
95
[row,col]=size(base_points_x);      % this will determine the number of rasterpoints we have to run through
96
[r,c]=size(filenamelist);                   % this will determine the number of images we have to loop through
97
98
99
% Open new figure so previous ones (if open) are not overwritten
100
h=figure;
101
imshow([imagedir, filenamelist(1,:)])           % show the first image
102
title('Initial Grid For Image Correlation (Note green crosses)')        % put a title
103
hold on
104
plot(grid_x,grid_y,'g+')            % plot the grid onto the image
105
hold off
106
107
% Start image correlation using cpcorr.m
108
g = waitbar(0,sprintf('Processing images'));        % initialize the waitbar
109
set(g,'Position',[275,50,275,50])                               % set the position of the waitbar [left bottom width height]
110
firstimage=1;
111
112
if resume==1
113
    firstimage=Imagenum+1
114
end
115
116
for i=firstimage:(r-1)               % run through all images
117
    
118
    
119
    tic             % start the timer
120
    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
121
    input = uint8(mean(double(imread([imagedir, filenamelist((i+1),:)])),3));       % read in the image which has to be correlated
122
    
123
    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
124
    input_points_for(:,2)=reshape(input_points_y,[],1);
125
    base_points_for(:,1)=reshape(base_points_x,[],1);
126
    base_points_for(:,2)=reshape(base_points_y,[],1);
127
    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
128
    input_correl_x=input_correl(:,1);                                       % the results we get from cpcorr for the x-direction
129
    input_correl_y=input_correl(:,2);                                       % the results we get from cpcorr for the y-direction
130
    
131
    
132
    validx(:,i)=input_correl_x;                                                     % lets save the data
133
    savelinex=input_correl_x';
134
    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 ;-)
135
    
136
    validy(:,i)=input_correl_y;
137
    saveliney=input_correl_y';
138
    dlmwrite([datadir, 'resultsimcorry.txt'], saveliney , 'delimiter', '\t', '-append');
139
    
140
    waitbar(i/(r-1))                                                                        % update the waitbar
141
    
142
    % Update base and input points for cpcorr.m
143
    base_points_x=grid_x;
144
    base_points_y=grid_y;
145
    input_points_x=input_correl_x;
146
    input_points_y=input_correl_y;
147
    
148
    imshow([imagedir, filenamelist(i+1,:)])                 % update image
149
    hold on
150
    plot(grid_x,grid_y,'g+')                                % plot start position of raster
151
    plot(input_correl_x,input_correl_y,'r+')        % plot actual postition of raster
152
    hold off
153
    drawnow
154
    time(i)=toc;                                                 % take time
155
    estimatedtime=sum(time)/i*(r-1);            % estimate time to process
156
    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
157
    drawnow
158
    
159
end    
160
161
close(g)
162
close all
163
164
% save
165
166
save ([datadir, 'time.dat'], 'time', '-ascii', '-tabs')
167
save ([datadir, 'validx.dat'], 'validx', '-ascii', '-tabs')
168
save ([datadir, 'validy.dat'], 'validy', '-ascii', '-tabs')