-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRTCorrCode.m
232 lines (190 loc) · 8.84 KB
/
RTCorrCode.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
function [validx, validy, displx, disply]=RTCorrCode(grid_x,grid_y,straindir,Firstimagename)
% Real time Correlation Code
%
% Written by Chris
RTselection = menu(sprintf('End processing by end.txt or by last image?'),...
'Stop with end.txt','Stop with image check','Exit');
if RTselection==1
end
if RTselection==2
end
if RTselection==3
return
end
% Filename
if exist('Firstimagename')==0
[Firstimagename ImageFolder]=uigetfile('*.tif','Open First Image');
if Firstimagename~~[]
cd(ImageFolder);
end
end
if Firstimagename~~[]
% Get the number of image name
letters=isletter(Firstimagename);
Pointposition=findstr(Firstimagename,'.');
Firstimagenamesize=size(Firstimagename);
counter=Pointposition-1;
counterpos=1;
letterstest=0;
while letterstest==0
letterstest=letters(counter);
if letterstest==1
break
end
Numberpos(counterpos)=counter;
counter=counter-1;
counterpos=counterpos+1;
if counter==0
break
end
end
Filename_first = Firstimagename(1:min(Numberpos)-1);
Firstfilenumber=Firstimagename(min(Numberpos):max(Numberpos));
Lastname_first = Firstimagename(max(Numberpos)+1:Firstimagenamesize(1,2));
Firstfilenumbersize=size(Firstfilenumber);
onemore=10^(Firstfilenumbersize(1,2));
filenamelist(1,:)=Firstimagename;
h=figure;
if exist('grid_x')==0
fpstest=1;
Filelist=[Firstimagename;Firstimagename];
while fpstest==1
[grid_x,grid_y]=grid_generator(Firstimagename,ImageFolder);
[processingtime]=fpstestfunc(grid_x,grid_y,Filelist);
fpstest = menu(sprintf(['Processing the selected grid will allow ' , num2str(1/processingtime),' frames per second' ]),'Try again','Use the grid');
if fpstest==1
clear grid_x; clear grid_y;
end
end
end
Firstfilenumber=str2num(Firstfilenumber);
u=1+onemore+Firstfilenumber;
ustr=num2str(u);
filenamelist(2,:)=[Filename_first ustr(2:Firstfilenumbersize(1,2)+1) Lastname_first];
numberofimages=2;
counter=1;
input_points_x=grid_x;
input_points_y=grid_y;
base_points_x=grid_x;
base_points_y=grid_y;
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
numberofmarkers=max(size(grid_x))*min(size(grid_x));
validx(:,1)=reshape(grid_x,[],1);
displx=zeros(numberofmarkers,1);
validy(:,1)=reshape(grid_y,[],1);
disply=zeros(numberofmarkers,1);
tic
while exist('end.txt','file') ==0;
pause(0.01);
if exist(filenamelist((counter+1),:),'file') ==2;
warning(['# Processed Images: ', num2str(numberofimages-1),'; # markers:',num2str(numberofmarkers), '; Processing Image: ',filenamelist(counter+1,:)]) % plot a title onto the image
input = mean(double(imread(filenamelist((counter+1),:))),3); % read in the image which has to be correlated
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
input_points_for(:,2)=reshape(input_points_y,[],1);
base_points_for(:,1)=reshape(base_points_x,[],1);
base_points_for(:,2)=reshape(base_points_y,[],1);
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
input_correl_x=input_correl(:,1); % the results we get from cpcorr for the x-direction
input_correl_y=input_correl(:,2); % the results we get from cpcorr for the y-direction
validx(:,counter+1)=input_correl_x; % lets save the data
savelinex=input_correl_x';
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 ;-)
validy(:,counter+1)=input_correl_y;
saveliney=input_correl_y';
dlmwrite('resultsimcorry.txt', saveliney , 'delimiter', '\t', '-append');
base_points_x=grid_x;
base_points_y=grid_y;
input_points_x=input_correl_x;
input_points_y=input_correl_y;
subplot(2,2,1)
imshow(filenamelist(counter+1,:)) % update image
hold on
plot(grid_x,grid_y,'g+') % plot start position of raster
plot(input_correl_x,input_correl_y,'r+') % plot actual postition of raster
hold off
drawnow
displx(:,counter+1)=validx(:,counter+1)-validx(:,1);
disply(:,counter+1)=validy(:,counter+1)-validy(:,1);
subplot(2,2,2)
xdata=validx(:,counter+1);
ydata=displx(:,counter+1);
if counter==1
x(1)=0
x(2)=0
end
[x,resnormx,residual,exitflagx,output] = lsqcurvefit(@linearfit, [x(1) x(2)], xdata, ydata);
plot(xdata,ydata,'.');
hold on;
ydatafit=x(1)*xdata+x(2);
plot(xdata,ydatafit,'r');
hold off
xlabel('x-pos [pixel]')
ylabel('x-displ [pixel]')
title('x displ. versus x pos. in [pixel]')
slopex(counter,:)=[i x(1) x(2)];
subplot(2,2,4)
xdata=validy(:,counter+1);
ydata=disply(:,counter+1);
if counter==1
y(1)=0
y(2)=0
end
[y,resnormx,residual,exitflagx,output] = lsqcurvefit(@linearfit, [y(1) y(2)], xdata, ydata);
plot(xdata,ydata,'.g');
hold on;
ydatafit=y(1)*xdata+y(2);
plot(xdata,ydatafit,'r');
hold off
xlabel('y-pos [pixel]')
ylabel('y-displ [pixel]')
title('y displ. versus y pos. in [pixel]')
slopey(counter,:)=[i y(1) y(2)];
subplot(2,2,3)
plot(slopex(:,2),'-b')
hold on
plot(slopey(:,2),'-g')
hold off
xlabel('Image # [ ]')
ylabel('x- and y-strain [ ]')
title('Strain in x and y direction versus Image #')
counter=counter+1;
u=1+u;
ustr=num2str(u);
filenamelist(counter+1,:)=[Filename_first ustr(2:Firstfilenumbersize(1,2)+1) Lastname_first];
[numberofmarkers numberofimages]=size(validx);
if RTselection==2
if exist(filenamelist((counter+1),:),'file') ==0;
save validx.dat validx -ascii -tabs
save validy.dat validy -ascii -tabs
warning('Last image detected, RTCorrCode stopped')
return
end
end
subplot(2,2,1),title(['# Processed Images: ', num2str(numberofimages-1),'; fps: ', num2str((numberofimages-1)/toc),'; # markers:',num2str(numberofmarkers), '; Waiting for Image: ',filenamelist(counter+1,:)]) % plot a title onto the image
end
end
save validx.dat validx -ascii -tabs
save validy.dat validy -ascii -tabs
msgboxwicon=msgbox('end.txt file detected, RTCorrCode stopped','Processing stopped!')
warning('end.txt file detected, RTCorrCode stopped')
end
%----------------------------------
%
function [processingtime]=fpstestfunc(grid_x,grid_y,filenamelist)
tic;
input_points_x=grid_x;
base_points_x=grid_x;
input_points_y=grid_y;
base_points_y=grid_y;
% [row,col]=size(base_points_x); % this will determine the number of rasterpoints we have to run through
% [r,c]=size(filenamelist); % this will determine the number of images we have to loop through
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
input = uint8(mean(double(imread(filenamelist(2,:))),3)); % read in the image which has to be correlated
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
input_points_for(:,2)=reshape(input_points_y,[],1);
base_points_for(:,1)=reshape(base_points_x,[],1);
base_points_for(:,2)=reshape(base_points_y,[],1);
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
input_correl_x=input_correl(:,1); % the results we get from cpcorr for the x-direction
input_correl_y=input_correl(:,2); % the results we get from cpcorr for the y-direction
processingtime=toc;