/normxcorr/trunk

To get this branch, use:
bzr branch http://suren.me/webbzr/normxcorr/trunk
31 by Suren A. Chilingaryan
CUDAfication of real-time module
1
function [validx, validy, displx, disply]=RTCorrCode(grid_x,grid_y,Firstimagename,ImageFolder)
2
3
% Real time Correlation Code
4
%
5
% Written by Chris
6
% OPTIMIZE parameter is controlling which optimizations should be used. 
7
% <3 - The original version, no optimizations
8
%  3 - Most of computations are performed on NVidia card
9
%  4 - Optimize FFT sizes for better performance (affects precision)
10
%  5 - Load images in C code
11
OPTIMIZE = 4;
12
CORRSIZE = 15;	
13
PRECISION = 1000;
14
15
warning off Images:initSize:adjustingMag
16
17
if OPTIMIZE > 2
18
    hwid = normxcorr_hw();
19
20
    if hwid > 0
21
    else
22
	OPTIMIZE = 0;
23
    end
24
else
25
    hwid = 0;
26
end
27
28
if OPTIMIZE > 2
29
    RTselection = menu(sprintf('End processing by end.txt or by last image?'),...
30
	'Stop with end.txt','Stop with image check','Exit');
31
32
    if RTselection==1
33
    end
34
35
    if RTselection==2
36
    end
37
38
    if RTselection==3
39
        if hwid > 0
40
	    normxcorr_hw(hwid);
41
	end
42
	return
43
    end
44
end
45
46
% Filename
47
48
if exist('Firstimagename')==0
49
    [Firstimagename ImageFolder]=uigetfile('*.tif','Open First Image');
50
end
51
52
53
if ~isempty(Firstimagename)
54
% Get the number of image name
55
letters=isletter(Firstimagename);
56
Pointposition=findstr(Firstimagename,'.');
57
Firstimagenamesize=size(Firstimagename);
58
counter=Pointposition-1;
59
counterpos=1;
60
letterstest=0;
61
62
while letterstest==0
63
    letterstest=letters(counter);
64
    if letterstest==1
65
        break
66
    end
67
    Numberpos(counterpos)=counter;
68
    counter=counter-1;
69
    counterpos=counterpos+1;
70
    if counter==0
71
        break
72
    end
73
end
74
75
Filename_first = Firstimagename(1:min(Numberpos)-1);
76
Firstfilenumber=Firstimagename(min(Numberpos):max(Numberpos));
77
Lastname_first = Firstimagename(max(Numberpos)+1:Firstimagenamesize(1,2));
78
Firstfilenumbersize=size(Firstfilenumber);
79
onemore=10^(Firstfilenumbersize(1,2));
80
filenamelist(1,:)=Firstimagename;
81
82
83
h=figure;
84
if exist('grid_x')==0
85
    fpstest=1;
86
    Filelist=[Firstimagename;Firstimagename];
87
    while fpstest==1
88
        [grid_x,grid_y]=grid_generator(Firstimagename,ImageFolder);
89
	if OPTIMIZE > 2
90
	    ncp = prod(size(grid_x));
91
	    err = normxcorr_hw(hwid, 1, ncp, CORRSIZE, PRECISION, OPTIMIZE);
92
	else
93
	    err = 0;
94
	end
95
	if err == 0
96
	    if OPTIMIZE > 2
97
		base_points_x=single(grid_x);
98
		base_points_y=single(grid_y);
99
		
100
		normxcorr_hw(hwid, 3, base_points_x, base_points_y);
101
102
		if OPTIMIZE > 4
103
		    normxcorr_hw(hwid, 4, strcat(imagedir, Firstimagename));
104
		else
105
		    base = uint8(mean(double(imread([ImageFolder, Firstimagename])),3));
106
		    normxcorr_hw(hwid, 4, base);
107
		end	
108
		
109
		normxcorr_hw(hwid, 12, base_points_x, base_points_y);
110
	    end
111
	    [processingtime]=fpstestfunc(hwid,OPTIMIZE,grid_x,grid_y,Filelist,ImageFolder);
112
    	    fpstest = menu(sprintf(['Processing the selected grid will allow ' , num2str(1/processingtime),' frames per second' ]),'Try again','Use the grid');
113
    	    if fpstest==1
114
        	clear grid_x; clear grid_y;
115
    	    end
116
	else
117
	    ASelection = menu(sprintf('CUDA initialization failed?'),...
118
	'Retry another grid','Continue in software mode','Exit');
119
	    if Aselection==1
120
		fptest = 1;
121
	    end
122
123
	    if Aselection==2
124
		fptest = 0;
125
		normxcorr_hw(hwid);
126
		OPTIMIZE = 0;
127
	    end
128
129
	    if Aselection==3
130
		return
131
	    end
132
	end
133
    end
134
end
135
136
if OPTIMIZE < 3
137
    [validx, validy, displx, disply] = RTCorrCode_orig(grid_x, grid_y, Firstimagename, ImageFolder)
138
    return
139
end
140
141
142
Firstfilenumber=str2num(Firstfilenumber);
143
u=1+onemore+Firstfilenumber;
144
ustr=num2str(u);
145
filenamelist(2,:)=[Filename_first ustr(2:Firstfilenumbersize(1,2)+1) Lastname_first];
146
numberofimages=2;
147
148
counter=1;
149
150
input_points_x=single(grid_x);
151
input_points_y=single(grid_y);
152
normxcorr_hw(hwid, 12, input_points_x, input_points_y);
153
154
fit_options = optimset('Display', 'off');
155
156
numberofmarkers=max(size(grid_x))*min(size(grid_x));
157
validx(:,1)=reshape(grid_x,[],1);
158
displx=zeros(numberofmarkers,1);
159
validy(:,1)=reshape(grid_y,[],1);
160
disply=zeros(numberofmarkers,1);
161
tic;
162
163
while exist([ImageFolder, 'end.txt'],'file') ==0;
164
    pause(0.01);
165
166
    if exist([ImageFolder, filenamelist((counter+1),:)],'file') ==2;
167
%        warning(['# Processed Images: ', num2str(numberofimages-1),'; # markers:',num2str(numberofmarkers), '; Processing Image: ',filenamelist(counter+1,:)]);
168
169
	if OPTIMIZE > 4
170
	    normxcorr_hw(hwid, 13, strcat(ImageFolder, filenamelist((counter+1),:)));
171
	    input_correl = normxcorr_hw(hwid, 14);
172
	else
173
	    input = uint8(mean(double(imread([ImageFolder, filenamelist((counter+1),:)])),3));
174
	    normxcorr_hw(hwid, 13, input);
175
	    input_correl = normxcorr_hw(hwid, 14);
176
	end
177
178
	input_correl_x=double(input_correl(:,1));
179
        input_correl_y=double(input_correl(:,2));
180
	
181
        validx(:,counter+1)=input_correl_x;                                                     % lets save the data
182
        savelinex=input_correl_x';
183
    	dlmwrite([ImageFolder, '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 ;-)
184
185
    	validy(:,counter+1)=input_correl_y;
186
	saveliney=input_correl_y';
187
    	dlmwrite([ImageFolder, 'resultsimcorry.txt'], saveliney , 'delimiter', '\t', '-append');
188
189
        subplot(2,2,1);
190
        imshow([ImageFolder, filenamelist(counter+1,:)]);
191
        hold on;
192
        plot(grid_x,grid_y,'g+');
193
        plot(input_correl_x,input_correl_y,'r+');
194
        hold off;
195
        drawnow;
196
197
        displx(:,counter+1)=validx(:,counter+1)-validx(:,1);
198
        disply(:,counter+1)=validy(:,counter+1)-validy(:,1);
199
200
        subplot(2,2,2);
201
        xdata=validx(:,counter+1);
202
        ydata=displx(:,counter+1);
203
        if counter==1
204
            x(1)=0;
205
            x(2)=0;
206
        end
207
        [x,resnormx,residual,exitflagx,output]  = lsqcurvefit(@linearfit, [x(1) x(2)], xdata, ydata, [], [], fit_options);
208
        plot(xdata,ydata,'.');
209
        hold on;
210
        ydatafit=x(1)*xdata+x(2);
211
        plot(xdata,ydatafit,'r');
212
        hold off;
213
        xlabel('x-pos [pixel]');
214
        ylabel('x-displ [pixel]');
215
        title('x displ. versus x pos. in [pixel]');
216
217
        slopex(counter,:)=[i x(1) x(2)];
218
219
        subplot(2,2,4);
220
        xdata=validy(:,counter+1);
221
        ydata=disply(:,counter+1);
222
        if counter==1
223
            y(1)=0;
224
            y(2)=0;
225
        end
226
        [y,resnormx,residual,exitflagx,output]  = lsqcurvefit(@linearfit, [y(1) y(2)], xdata, ydata, [], [], fit_options);
227
        plot(xdata,ydata,'.g');
228
        hold on;
229
        ydatafit=y(1)*xdata+y(2);
230
        plot(xdata,ydatafit,'r');
231
        hold off
232
        xlabel('y-pos [pixel]')
233
        ylabel('y-displ [pixel]')
234
        title('y displ. versus y pos. in [pixel]');
235
236
        slopey(counter,:)=[i y(1) y(2)];
237
238
        subplot(2,2,3);
239
        plot(slopex(:,2),'-b');
240
        hold on;
241
        plot(slopey(:,2),'-g');
242
        hold off;
243
        xlabel('Image # [ ]');
244
        ylabel('x- and y-strain [ ]');
245
        title('Strain in x and y direction versus Image #');
246
247
        counter=counter+1;
248
249
        u=1+u;
250
        ustr=num2str(u);
251
        filenamelist(counter+1,:)=[Filename_first ustr(2:Firstfilenumbersize(1,2)+1) Lastname_first];
252
        [numberofmarkers numberofimages]=size(validx);
253
        
254
        if RTselection==2
255
            if exist([ImageFolder, filenamelist((counter+1),:)],'file') ==0;
256
                save ([ImageFolder, 'validx.dat'], 'validx', '-ascii', '-tabs');
257
                save ([ImageFolder, 'validy.dat'], 'validy', '-ascii', '-tabs');
258
                %warning('Last image detected, RTCorrCode stopped');
259
		normxcorr_hw(hwid);
260
                return
261
            end
262
        end
263
        
264
        
265
        subplot(2,2,1),title(['# Processed Images: ', num2str(numberofimages-1),'; fps: ', num2str((numberofimages-1)/toc),'; # markers:',num2str(numberofmarkers), '; Waiting for Image: ',filenamelist(counter+1,:)]);
266
267
    end
268
end
269
270
normxcorr_hw(hwid);
271
272
save ([ImageFolder, 'validx.dat'], 'validx', '-ascii', '-tabs');
273
save ([ImageFolder, 'validy.dat'], 'validy', '-ascii', '-tabs');
274
msgboxwicon=msgbox('end.txt file detected, RTCorrCode stopped','Processing stopped!');
275
%warning('end.txt file detected, RTCorrCode stopped');
276
end
277
278
%----------------------------------
279
%
280
281
function [processingtime]=fpstestfunc(hwid, OPTIMIZE, grid_x, grid_y, filenamelist, ImageFolder)
282
283
tic;
284
285
if hwid > 0
286
    if OPTIMIZE > 4
287
	normxcorr_hw(hwid, 13, strcat(ImageFolder, filenamelist(2,:)));
288
	input_correl = normxcorr_hw(hwid, 14);
289
    else
290
	input = uint8(mean(double(imread([ImageFolder, filenamelist(2,:)])),3));
291
	normxcorr_hw(hwid, 13, input)
292
	input_correl = normxcorr_hw(hwid, 14);
293
    end
294
    input_correl_x=double(input_correl(:,1));
295
    input_correl_y=double(input_correl(:,2));
296
else
297
    input_points_x=grid_x;
298
    base_points_x=grid_x;
299
300
    input_points_y=grid_y;
301
    base_points_y=grid_y;
302
303
    base = uint8(mean(double(imread([ImageFolder, 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
304
    input = uint8(mean(double(imread([ImageFolder, filenamelist(2,:)])),3));       % read in the image which has to be correlated
305
306
    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
307
    input_points_for(:,2)=reshape(input_points_y,[],1);
308
    base_points_for(:,1)=reshape(base_points_x,[],1);
309
    base_points_for(:,2)=reshape(base_points_y,[],1);
310
    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
311
    input_correl_x=input_correl(:,1);                                       % the results we get from cpcorr for the x-direction
312
    input_correl_y=input_correl(:,2);                                       % the results we get from cpcorr for the y-direction
313
end
314
315
processingtime=toc;