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