1
function [validx, validy, displx, disply]=RTCorrCode(grid_x,grid_y,Firstimagename,ImageFolder)
3
% Real time Correlation Code
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
15
warning off Images:initSize:adjustingMag
18
hwid = normxcorr_hw();
29
RTselection = menu(sprintf('End processing by end.txt or by last image?'),...
30
'Stop with end.txt','Stop with image check','Exit');
48
if exist('Firstimagename')==0
49
[Firstimagename ImageFolder]=uigetfile('*.tif','Open First Image');
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;
63
letterstest=letters(counter);
67
Numberpos(counterpos)=counter;
69
counterpos=counterpos+1;
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;
86
Filelist=[Firstimagename;Firstimagename];
88
[grid_x,grid_y]=grid_generator(Firstimagename,ImageFolder);
90
ncp = prod(size(grid_x));
91
err = normxcorr_hw(hwid, 1, ncp, CORRSIZE, PRECISION, OPTIMIZE);
97
base_points_x=single(grid_x);
98
base_points_y=single(grid_y);
100
normxcorr_hw(hwid, 3, base_points_x, base_points_y);
103
normxcorr_hw(hwid, 4, strcat(imagedir, Firstimagename));
105
base = uint8(mean(double(imread([ImageFolder, Firstimagename])),3));
106
normxcorr_hw(hwid, 4, base);
109
normxcorr_hw(hwid, 12, base_points_x, base_points_y);
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');
114
clear grid_x; clear grid_y;
117
ASelection = menu(sprintf('CUDA initialization failed?'),...
118
'Retry another grid','Continue in software mode','Exit');
137
[validx, validy, displx, disply] = RTCorrCode_orig(grid_x, grid_y, Firstimagename, ImageFolder)
142
Firstfilenumber=str2num(Firstfilenumber);
143
u=1+onemore+Firstfilenumber;
145
filenamelist(2,:)=[Filename_first ustr(2:Firstfilenumbersize(1,2)+1) Lastname_first];
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);
154
fit_options = optimset('Display', 'off');
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);
163
while exist([ImageFolder, 'end.txt'],'file') ==0;
166
if exist([ImageFolder, filenamelist((counter+1),:)],'file') ==2;
167
% warning(['# Processed Images: ', num2str(numberofimages-1),'; # markers:',num2str(numberofmarkers), '; Processing Image: ',filenamelist(counter+1,:)]);
170
normxcorr_hw(hwid, 13, strcat(ImageFolder, filenamelist((counter+1),:)));
171
input_correl = normxcorr_hw(hwid, 14);
173
input = uint8(mean(double(imread([ImageFolder, filenamelist((counter+1),:)])),3));
174
normxcorr_hw(hwid, 13, input);
175
input_correl = normxcorr_hw(hwid, 14);
178
input_correl_x=double(input_correl(:,1));
179
input_correl_y=double(input_correl(:,2));
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 ;-)
185
validy(:,counter+1)=input_correl_y;
186
saveliney=input_correl_y';
187
dlmwrite([ImageFolder, 'resultsimcorry.txt'], saveliney , 'delimiter', '\t', '-append');
190
imshow([ImageFolder, filenamelist(counter+1,:)]);
192
plot(grid_x,grid_y,'g+');
193
plot(input_correl_x,input_correl_y,'r+');
197
displx(:,counter+1)=validx(:,counter+1)-validx(:,1);
198
disply(:,counter+1)=validy(:,counter+1)-validy(:,1);
201
xdata=validx(:,counter+1);
202
ydata=displx(:,counter+1);
207
[x,resnormx,residual,exitflagx,output] = lsqcurvefit(@linearfit, [x(1) x(2)], xdata, ydata, [], [], fit_options);
208
plot(xdata,ydata,'.');
210
ydatafit=x(1)*xdata+x(2);
211
plot(xdata,ydatafit,'r');
213
xlabel('x-pos [pixel]');
214
ylabel('x-displ [pixel]');
215
title('x displ. versus x pos. in [pixel]');
217
slopex(counter,:)=[i x(1) x(2)];
220
xdata=validy(:,counter+1);
221
ydata=disply(:,counter+1);
226
[y,resnormx,residual,exitflagx,output] = lsqcurvefit(@linearfit, [y(1) y(2)], xdata, ydata, [], [], fit_options);
227
plot(xdata,ydata,'.g');
229
ydatafit=y(1)*xdata+y(2);
230
plot(xdata,ydatafit,'r');
232
xlabel('y-pos [pixel]')
233
ylabel('y-displ [pixel]')
234
title('y displ. versus y pos. in [pixel]');
236
slopey(counter,:)=[i y(1) y(2)];
239
plot(slopex(:,2),'-b');
241
plot(slopey(:,2),'-g');
243
xlabel('Image # [ ]');
244
ylabel('x- and y-strain [ ]');
245
title('Strain in x and y direction versus Image #');
251
filenamelist(counter+1,:)=[Filename_first ustr(2:Firstfilenumbersize(1,2)+1) Lastname_first];
252
[numberofmarkers numberofimages]=size(validx);
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');
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,:)]);
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');
278
%----------------------------------
281
function [processingtime]=fpstestfunc(hwid, OPTIMIZE, grid_x, grid_y, filenamelist, ImageFolder)
287
normxcorr_hw(hwid, 13, strcat(ImageFolder, filenamelist(2,:)));
288
input_correl = normxcorr_hw(hwid, 14);
290
input = uint8(mean(double(imread([ImageFolder, filenamelist(2,:)])),3));
291
normxcorr_hw(hwid, 13, input)
292
input_correl = normxcorr_hw(hwid, 14);
294
input_correl_x=double(input_correl(:,1));
295
input_correl_y=double(input_correl(:,2));
297
input_points_x=grid_x;
298
base_points_x=grid_x;
300
input_points_y=grid_y;
301
base_points_y=grid_y;
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
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
b'\\ No newline at end of file'