/normxcorr/trunk

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