/normxcorr/trunk

To get this branch, use:
bzr branch http://suren.me/webbzr/normxcorr/trunk
41 by Suren A. Chilingaryan
Synchronize with Chris version, set precision to 1, initial implementation of labview wrapper
1
function [validx,validy]=automate_image_mp_2009b(grid_x,grid_y,filenamelist,validx,validy);
2
3
%% Coarse grained multi core code to start actual image correlation
4
% Be aware that you will need the Parallel Computing Toolbox and at least
5
% Matlab 2009b, all earlier version do not work. Experience shows that
6
% logical processor cores as available within the Core i7 series of Intel
7
% do not accelerate the code much. this seems to be due to the highly
8
% optimized fft code of matlab and the extensive memory bandwidth needed.
9
%
10
% Last change by Chris
11
%
12
% Last revision: 9/8/10
13
14
15
% The automation function is the central function and processes all markers and
16
% images by the use of the matlab function cpcorr.m.
17
% Therefore the Current directory in matlab has to be the folder where
18
%  automate_image.m finds the filenamelist.mat, grid_x.dat and grid_y.dat as well
19
% as the images specified in filenamelist.mat. Just type automate_image; and
20
% press ENTER at the command line of matlab.
21
% At first, automate_image.m will open the first image in the filenamelist.mat and
22
% plot the grid as green crosses on top. The next step will need some time since
23
% all markers in that image have to be processed for the first image. After correlating
24
% image one and two the new raster positions will be plotted as red crosses. On top
25
% of the image and the green crosses. The next dialog will ask you if you want to
26
% continue with this correlation or cancel. If you press continue, automate_image.m
27
% will process all images in the filenamelist.mat. The time it will take to process
28
% all images will be plotted on the figure but can easily be estimated by knowing the
29
% raster point processing speed (see processing speed).
30
% Depending on the number of images and markers you are tracking, this process
31
% can take between seconds and days. For 100 images and 200 markers a decent
32
% computer should need 200 seconds. To get a better resolution you can always
33
% run jobs overnight (e.g. 6000 markers in 1000 images) with higher resolutions.
34
% Keep in mind that CORRSIZE which you changed in cpcorr.m will limit your
35
% resolution. If you chose to use the 15 pixel as suggested a marker distance of
36
% 30 pixel will lead to a full cover of the strain field. Choosing smaller marker
37
% distances will lead to an interpolation since two neighboring markers share
38
% pixels. Nevertheless a higher marker density can reduce the noise of the strain field.
39
% When all images are processed, automate_image will write the files validx.mat,
40
% validy.mat, validx.txt and validy.txt. The text files are meant to store the result in a
41
% format which can be accessed by other programs also in the future.
42
43
44
% exist('grid_x')
45
% exist('grid_y')
46
% exist('filenamelist')
47
% exist('validx')
48
% exist('validy')
49
50
% Load necessary files
51
if exist('grid_x')==0
52
    load('grid_x.dat')              % file with x position, created by grid_generator.m
53
end
54
if exist('grid_y')==0
55
    load('grid_y.dat')              % file with y position, created by grid_generator.m
56
end
57
if exist('filenamelist')==0
58
    load('filenamelist')            % file with the list of filenames to be processed
59
end
60
resume=0;
61
if exist('validx')==1
62
    if exist('validy')==1
63
        resume=1;
64
        [Rasternum Imagenum]=size(validx);
65
    end
66
end
67
68
69
70
% Initialize variables
71
input_points_x=grid_x;
72
base_points_x=grid_x;
73
74
input_points_y=grid_y;
75
base_points_y=grid_y;
76
77
78
[row,col]=size(base_points_x);      % this will determine the number of rasterpoints we have to run through
79
[r,c]=size(filenamelist);                   % this will determine the number of images we have to loop through
80
81
%% multicore part, here we start the matlabpool and the matlab workers
82
% after checking if there is a matlabpool available it starts a local one.
83
% If you want to limit the number of workers to the real core count (not
84
% the logical ones, you can add the number in line 94 (e.g. matlabpool 4)
85
clear ans
86
matlabpool size;
87
poolsize=ans;
88
if poolsize==0;
89
    matlabpool %start a new matlabpool
90
end
91
matlabpool size;
92
poolsize=ans;
93
94
grid_x=reshape(grid_x,[],1);
95
grid_y=reshape(grid_y,[],1);
96
97
numberperlab=length(grid_x)/poolsize;
98
99
% single program multiple data -  here we distribute the work between the
100
% available workers
101
spmd
102
if labindex<poolsize
103
    input_points_x=grid_x(1+(labindex-1)*fix(numberperlab):(labindex)*fix(numberperlab),1);
104
else
105
    input_points_x=grid_x(1+(labindex-1)*fix(numberperlab):length(grid_x),1);
106
end
107
    base_points_x=input_points_x;
108
end
109
spmd
110
if labindex<poolsize
111
    input_points_y=grid_y(1+(labindex-1)*fix(numberperlab):(labindex)*fix(numberperlab),1);
112
else
113
    input_points_y=grid_y(1+(labindex-1)*fix(numberperlab):length(grid_x),1);
114
end
115
    base_points_y=input_points_y;
116
end
117
118
%% Open new figure so previous ones (if open) are not overwritten
119
h=figure;
120
imshow(filenamelist(1,:))           % show the first image
121
title('Initial Grid For Image Correlation (Note green crosses)')        % put a title
122
hold on
123
plot(grid_x,grid_y,'g+')            % plot the grid onto the image
124
hold off
125
126
% Start image correlation using cpcorr.m
127
g = waitbar(0,sprintf('Processing images'));        % initialize the waitbar
128
set(g,'Position',[275,50,275,50])                               % set the position of the waitbar [left bottom width height]
129
firstimage=1;
130
131
base = uint8(mean(double(imread(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
132
133
for i=firstimage:(r-1)               % run through all images
134
    
135
    
136
    tic;             % start the timer
137
    input = uint8(mean(double(imread(filenamelist((i+1),:))),3));       % read in the image which has to be correlated
138
    
139
    % here we send of the image to the individual workers which will work
140
    % on their specific markers
141
    spmd
142
        input_points_for=[input_points_x input_points_y];
143
        base_points_for=[base_points_x base_points_y];
144
        % rounding the input and base points gives much better resolution -
145
        % seems to be a imcrop problem but I'm still investigating
146
        input_correl(:,:)=cpcorr(round(input_points_for),round(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
147
        input_correl_x=input_correl(:,1);                                       % the results we get from cpcorr for the x-direction
148
        input_points_x=input_correl_x;
149
        input_correl_y=input_correl(:,2);                                       % the results we get from cpcorr for the y-direction
150
        input_points_y=input_correl_y;
151
    end
152
    
153
    % we need to collect all the data from our workers
154
    for j=1:numel(input_correl_x)
155
        lengthx(j,:)=size(input_correl_x{j});
156
    end
157
    bis=0;
158
    for j=1:numel(input_correl_x)
159
        von=bis+1;
160
        bis=bis+lengthx(j,1);
161
        validx_temp(von:bis,:)=input_correl_x{j};
162
        validy_temp(von:bis,:)=input_correl_y{j};
163
    end
164
    
165
    validx(:,i)=validx_temp;                                                     % lets save the data
166
    savelinex=validx_temp';
167
    dlmwrite('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 ;-)
168
    
169
    validy(:,i)=validy_temp;
170
    saveliney=validy_temp';
171
    dlmwrite('resultsimcorry.txt', saveliney , 'delimiter', '\t', '-append');
172
    
173
    waitbar(i/(r-1))   ;                                                                     % update the waitbar
174
    
175
    imshow(filenamelist(i+1,:))     ;                % update image
176
    hold on
177
    plot(grid_x,grid_y,'g+');                               % plot start position of raster
178
    plot(validx_temp,validy_temp,'r+');        % plot actual postition of raster
179
    hold off
180
    drawnow
181
    time(i)=toc;                                                 % take time
182
    estimatedtime=sum(time)/i*(r-1);           % estimate time to process
183
    title(['# Im.: ', num2str((r-1)),'; Proc. Im. #: ', num2str((i)),'; # Rasterp.:',num2str(row*col), '; Est. Time [s] ', num2str(estimatedtime), ';  Elapsed Time [s] ', num2str(sum(time))]);    % plot a title onto the image
184
    drawnow
185
    
186
end
187
188
close(g)
189
close all
190
191
% save
192
matlabpool close
193
194
save time.dat time -ascii -tabs
195
save validx.dat validx -ascii -tabs
196
save validy.dat validy -ascii -tabs