/normxcorr/trunk

To get this branch, use:
bzr branch http://suren.me/webbzr/normxcorr/trunk
1 by Suren A. Chilingaryan
Initial import
1
function [grid_x,grid_y]=grid_generator(FileNameBase,PathNameBase);
2
3
% Code to generate the DIC analysis grid
4
% Completely rewritten by Chris
5
% Programmed first by Dan and Rob 
6
% 
7
% Last revision: 12/27/06
8
9
% The grid_generator function will help you create grids of markers. The
10
% dialog has different options allowing you to create a marker grid which is rectangular,
11
% circular, a line or two rectangels of a shape or contains only of two
12
% markers. After choosing one of the shapes you will be asked for the base
13
% image which is typically your first image. After opening that image you
14
% will be asked to click at the sites of interest and the markers will be
15
% plotted on top of your image. You can choose if you want to keep these
16
% markers or if you want to try again.
17
% It has to be noted that you can
18
% always generate your own marker positions. Therefore the marker position
19
% in pixel has to be saved as a text based format where the x-position is
20
% saved as grid_x.dat and the y-position saved as grid_y.dat.
21
%
22
23
24
25
% Prompt user for base image
26
if exist('FileNameBase')==0
27
[FileNameBase,PathNameBase] = uigetfile( ...
28
    {'*.bmp;*.tif;*.jpg;*.TIF;*.BMP;*.JPG','Image files (*.bmp,*.tif,*.jpg)';'*.*',  'All Files (*.*)'}, ...
29
    'Open base image for grid creation');
30
31
end
31 by Suren A. Chilingaryan
CUDAfication of real-time module
32
curdir = pwd;
33
cd(PathNameBase);
1 by Suren A. Chilingaryan
Initial import
34
im_grid = imread(FileNameBase);
35
36
[grid_x,grid_y,FileNameBase,PathNameBase] = gridtypeselection(FileNameBase, PathNameBase, im_grid);
37
38
close all
31 by Suren A. Chilingaryan
CUDAfication of real-time module
39
cd(curdir);
1 by Suren A. Chilingaryan
Initial import
40
41
%-------------------------------
42
%
43
% Decide which type of grid you want to create
44
45
function [grid_x,grid_y,FileNameBase,PathNameBase] = gridtypeselection(FileNameBase, PathNameBase, im_grid);
46
47
hold off
31 by Suren A. Chilingaryan
CUDAfication of real-time module
48
imshow(im_grid,'InitialMagnification', 100);
1 by Suren A. Chilingaryan
Initial import
49
50
gridselection = menu(sprintf('Which type of grid do you want to use'),...
51
    'Rectangular','Circular','Two Markers','Line','Two Rectangles of Markers','Cancel');
52
53
54
if gridselection==1
55
    [grid_x,grid_y,FileNameBase,PathNameBase] = rect_grid(FileNameBase, PathNameBase, im_grid);
56
    return
57
end
58
59
if gridselection==2
60
    [grid_x,grid_y,FileNameBase,PathNameBase] = circ_grid(FileNameBase, PathNameBase, im_grid);
61
    return
62
end
63
64
if gridselection==3
65
    [grid_x,grid_y,FileNameBase,PathNameBase] = twop_grid(FileNameBase, PathNameBase, im_grid);
66
    return
67
end
68
69
if gridselection==4
70
    [grid_x,grid_y,FileNameBase,PathNameBase] = line_grid(FileNameBase, PathNameBase, im_grid);
71
    return
72
end
73
74
if gridselection==5
75
    [grid_x,grid_y,FileNameBase,PathNameBase] = tworect_grid(FileNameBase, PathNameBase, im_grid);
76
    return
77
end
78
79
if gridselection==6
80
    return;
81
end
82
83
84
85
%-------------------------------
86
%
87
% Define two rectangles and add them to one marker array
88
89
function [grid_x,grid_y,FileNameBase,PathNameBase] = tworect_grid(FileNameBase, PathNameBase, im_grid);
90
91
[grid_x1,grid_y1,FileNameBase,PathNameBase] = rect_grid(FileNameBase, PathNameBase, im_grid);
31 by Suren A. Chilingaryan
CUDAfication of real-time module
92
imshow(im_grid,'InitialMagnification', 100);
1 by Suren A. Chilingaryan
Initial import
93
[grid_x2,grid_y2,FileNameBase,PathNameBase] = rect_grid(FileNameBase, PathNameBase, im_grid);
94
95
grid_x1=reshape(grid_x1,[],1);
96
grid_x2=reshape(grid_x2,[],1);
97
grid_y1=reshape(grid_y1,[],1);
98
grid_y2=reshape(grid_y2,[],1);
99
100
grid_x=[grid_x1; grid_x2];
101
grid_y=[grid_y1; grid_y2];
102
31 by Suren A. Chilingaryan
CUDAfication of real-time module
103
imshow(im_grid,'InitialMagnification', 100);
1 by Suren A. Chilingaryan
Initial import
104
hold on
105
plot(grid_x,grid_y,'.')
106
title(['Selected grid has ',num2str(length(grid_x)), ' rasterpoints'])    % plot a title onto the image
107
108
% Accept the chosen markers, try again or give up 
109
110
confirmcircselection = menu(sprintf('Do you want to use these markers?'),...
111
    'Yes','No, try again','Go back to grid-type selection');
112
113
if confirmcircselection==2
114
    close all
115
    hold off
31 by Suren A. Chilingaryan
CUDAfication of real-time module
116
    imshow(im_grid,'InitialMagnification', 100);
1 by Suren A. Chilingaryan
Initial import
117
    tworect_grid(FileNameBase, PathNameBase, im_grid);
118
end
119
120
if confirmcircselection==3
121
    close all
122
    gridtypeselection(FileNameBase, PathNameBase, im_grid);
123
end
124
125
if confirmcircselection==1
126
    close all
127
    save grid_x.dat grid_x -ascii -tabs
128
    save grid_y.dat grid_y -ascii -tabs
129
end
130
131
%-------------------------------
132
%
133
% Define line and create markers
134
135
function [grid_x,grid_y,FileNameBase,PathNameBase] = line_grid(FileNameBase, PathNameBase, im_grid);
136
137
title(sprintf('Pick two points on the sample.') )
138
139
[x(1,1),y(1,1)]=ginput(1);
140
hold on
141
plot(x(1,1),y(1,1),'+g')
142
143
[x(2,1),y(2,1)]=ginput(1);
144
plot(x(2,1),y(2,1),'+g')
145
146
147
linelength=sqrt((x(2,1)-x(1,1))*(x(2,1)-x(1,1))+(y(2,1)-y(1,1))*(y(2,1)-y(1,1)));
148
lineslope=(y(2,1)-y(1,1))/(x(2,1)-x(1,1));
149
intersecty=y(1,1)-lineslope*x(1,1);
150
ycalc=zeros(2,1);
151
ycalc=lineslope*x+intersecty;
152
plot(x(:,1),ycalc(:,1),'-b')
153
154
155
prompt = {'Enter the number of intersections between markers on the line:'};
156
dlg_title = 'Input for grid creation';
157
num_lines= 1;
158
def     = {'30'};
159
answer = inputdlg(prompt,dlg_title,num_lines,def);
160
linediv = str2num(cell2mat(answer(1,1)));
161
linestep=((max(x)-min(x))/linediv);
162
grid_x(1:linediv+1)=min(x)+linestep*(1:linediv+1)-linestep;
163
grid_y=lineslope*grid_x+intersecty;
164
165
plot(grid_x,grid_y,'ob')
166
title(['Selected grid has ',num2str(linediv), ' rasterpoints'])    % plot a title onto the image
167
168
% Accept the chosen markers, try again or give up 
169
170
confirmcircselection = menu(sprintf('Do you want to use these markers?'),...
171
    'Yes','No, try again','Go back to grid-type selection');
172
173
if confirmcircselection==2
174
    close all
175
    hold off
31 by Suren A. Chilingaryan
CUDAfication of real-time module
176
    imshow(im_grid,'InitialMagnification', 100);
1 by Suren A. Chilingaryan
Initial import
177
    twop_grid(FileNameBase, PathNameBase, im_grid);
178
end
179
180
if confirmcircselection==3
181
    close all
182
    gridtypeselection(FileNameBase, PathNameBase, im_grid);
183
end
184
185
if confirmcircselection==1
186
    save grid_x.dat grid_x -ascii -tabs
187
    save grid_y.dat grid_y -ascii -tabs
188
end
189
190
%-------------------------------
191
%
192
% Select two markers
193
194
function [grid_x,grid_y,FileNameBase,PathNameBase] = twop_grid(FileNameBase, PathNameBase, im_grid);
195
196
title(sprintf('Pick two points on the sample.') )
197
198
[x(1,1),y(1,1)]=ginput(1);
199
hold on
200
plot(x(1,1),y(1,1),'+g')
201
202
[x(2,1),y(2,1)]=ginput(1);
203
plot(x(2,1),y(2,1),'+g')
204
205
% Accept the chosen markers, try again or give up 
206
207
confirmcircselection = menu(sprintf('Do you want to use these two markers?'),...
208
    'Yes','No, try again','Go back to grid-type selection');
209
210
if confirmcircselection==2
211
    close all
212
    hold off
31 by Suren A. Chilingaryan
CUDAfication of real-time module
213
    imshow(im_grid,'InitialMagnification', 100);
1 by Suren A. Chilingaryan
Initial import
214
    twop_grid(FileNameBase, PathNameBase, im_grid);
215
end
216
217
if confirmcircselection==3
218
    close all
219
    gridtypeselection(FileNameBase, PathNameBase, im_grid);
220
end
221
222
if confirmcircselection==1
223
    grid_x=x;
224
    grid_y=y;
225
    save grid_x.dat grid_x -ascii -tabs
226
    save grid_y.dat grid_y -ascii -tabs
227
end
228
%-------------------------------
229
%
230
% Select a circular area
231
232
function [grid_x,grid_y,FileNameBase,PathNameBase] = circ_grid(FileNameBase, PathNameBase, im_grid);
233
234
title(sprintf('Pick three points on the circle in clockwise order at the upper boundary of the sample.') )
235
236
[x(1,1),y(1,1)]=ginput(1);
237
hold on
238
plot(x(1,1),y(1,1),'+g')
239
240
[x(2,1),y(2,1)]=ginput(1);
241
plot(x(2,1),y(2,1),'+g')
242
243
[x(3,1),y(3,1)]=ginput(1);
244
plot(x(3,1),y(3,1),'+g')
245
246
xnew=x;
247
ynew=y;
248
249
% Calculate center between the 3 sorted points and the normal slope of the vectors
250
slope12=-1/((ynew(2,1)-ynew(1,1))/(xnew(2,1)-xnew(1,1)));
251
slope23=-1/((ynew(3,1)-ynew(2,1))/(xnew(3,1)-xnew(2,1)));
252
center12(1,1)=(xnew(2,1)-xnew(1,1))/2+xnew(1,1);
253
center12(1,2)=(ynew(2,1)-ynew(1,1))/2+ynew(1,1);
254
center23(1,1)=(xnew(3,1)-xnew(2,1))/2+xnew(2,1);
255
center23(1,2)=(ynew(3,1)-ynew(2,1))/2+ynew(2,1);
256
% plot(center12(1,1),center12(1,2),'+b')
257
% plot(center23(1,1),center23(1,2),'+b')
258
259
if slope12==slope23
260
    return
261
end
262
263
% Calculate the crossing point of the two vectors
264
achsenabschnitt1=center12(1,2)-center12(1,1)*slope12;
265
achsenabschnitt2=center23(1,2)-center23(1,1)*slope23;
266
xdata=min(x):max(x);
267
ydata1=achsenabschnitt1+slope12*xdata;
268
ydata2=achsenabschnitt2+slope23*xdata;
269
% plot(xdata,ydata1,'-b')
270
% plot(xdata,ydata2,'-b')
271
xcross=(achsenabschnitt2-achsenabschnitt1)/(slope12-slope23);
272
ycross=slope12*xcross+achsenabschnitt1;
273
plot(xcross,ycross,'or')
274
275
% Calculate radius and plot circle
276
R=sqrt((xcross-xnew(1,1))*(xcross-xnew(1,1))+(ycross-ynew(1,1))*(ycross-ynew(1,1)));
277
% ydata=ycross-sqrt(R*R-(xdata-xcross).*(xdata-xcross));
278
% plot(xdata,ydata,'-b')
279
280
% Calculate angle between vectors
281
xvector=[1;0];
282
x1vec(1,1)=xnew(1,1)-xcross;x1vec(2,1)=ynew(1,1)-ycross
283
x3vec(1,1)=xnew(3,1)-xcross;x3vec(2,1)=ynew(3,1)-ycross
284
alpha13=acos((dot(x1vec,x3vec))/(sqrt(x1vec'*x1vec)*sqrt(x3vec'*x3vec)))*180/pi;
285
alpha01=acos((dot(xvector,x1vec))/(sqrt(x1vec'*x1vec)*sqrt(xvector'*xvector)))*180/pi;
286
alpha03=acos((dot(xvector,x3vec))/(sqrt(xvector'*xvector)*sqrt(x3vec'*x3vec)))*180/pi;
287
totalangle=alpha13;
288
minangle=alpha01;
289
maxangle=alpha03;
290
angldiv=abs(round(totalangle))*10;
291
anglstep=(totalangle/angldiv);
292
anglall(1:angldiv+1)=maxangle+anglstep*(1:angldiv+1)-anglstep;
293
xcircle(1:angldiv+1)=xcross+R*cos(-anglall(1:angldiv+1)/180*pi);
294
ycircle(1:angldiv+1)=ycross+R*sin(-anglall(1:angldiv+1)/180*pi);
295
plot(xcircle,ycircle,'-b')
296
drawnow
297
298
title(['Segment of circle spreads over ',num2str(totalangle),'°'])
299
300
301
% Accept the chosen circle, try again or give up 
302
303
confirmcircselection = menu(sprintf('Do you want to use this circle as basis?'),...
304
    'Yes','No, try again','Go back to grid-type selection');
305
306
if confirmcircselection==2
307
    close all
31 by Suren A. Chilingaryan
CUDAfication of real-time module
308
    imshow(im_grid,'InitialMagnification', 100);
1 by Suren A. Chilingaryan
Initial import
309
    circ_grid(FileNameBase, PathNameBase, im_grid);
310
end
311
312
if confirmcircselection==3
313
    close all
314
    gridtypeselection(FileNameBase, PathNameBase, im_grid);
315
end
316
317
if confirmcircselection==1
318
    
319
    prompt = {'Enter the number of intersections between markers on the circle:'};
320
    dlg_title = 'Input for grid creation';
321
    num_lines= 1;
322
    def     = {'30'};
323
    answer = inputdlg(prompt,dlg_title,num_lines,def);
324
    angldiv = str2num(cell2mat(answer(1,1)));
325
    
326
    anglstep=(totalangle/angldiv);
327
    anglall(1:angldiv+1)=maxangle+anglstep*(1:angldiv+1)-anglstep;
328
    
329
    markerxpos(1:angldiv+1)=xcross+R*cos(-anglall(1:angldiv+1)/180*pi);
330
    markerypos(1:angldiv+1)=ycross+R*sin(-anglall(1:angldiv+1)/180*pi);
331
    
332
    plot(markerxpos,markerypos,'ob');
333
    
334
    % Pick the lower bound in the image
335
    title(sprintf('Pick three points lying on the circle in clockwise order. The first and last one define the width of the raster') )
336
    
337
    [x(4,1),y(4,1)]=ginput(1);
338
    hold on
339
    plot(x(1,1),y(1,1),'+r')
340
    
341
    lowboundx=x(4,1);
342
    lowboundy=y(4,1);
343
    
344
    R2=sqrt((xcross-lowboundx(1,1))*(xcross-lowboundx(1,1))+(ycross-lowboundy(1,1))*(ycross-lowboundy(1,1)));
345
    markerxposlb(1:angldiv+1)=xcross+R2*cos(-anglall(1:angldiv+1)/180*pi);
346
    markeryposlb(1:angldiv+1)=ycross+R2*sin(-anglall(1:angldiv+1)/180*pi);
347
    
348
    plot(markerxposlb,markeryposlb,'ob');
349
    
350
    prompt = {'Enter the number of intersections between the upper and lower bound:'};
351
    dlg_title = 'Input for grid creation';
352
    num_lines= 1;
353
    def     = {'5'};
354
    answer = inputdlg(prompt,dlg_title,num_lines,def);
355
    Rdiv = str2num(cell2mat(answer(1,1)));
356
    
357
    Rstep=((R-R2)/Rdiv);
358
    Rall(1:Rdiv+1)=R2+Rstep*(1:Rdiv+1)-Rstep;
359
    
360
    grid_x=ones(Rdiv+1,angldiv+1)*xcross;
361
    grid_y=ones(Rdiv+1,angldiv+1)*ycross;
362
    A=Rall;
363
    B=cos(-anglall(1:angldiv+1)/180*pi);
364
    C=A'*B;
365
    grid_x=grid_x+Rall'*cos(-anglall(1:angldiv+1)/180*pi);
366
    grid_y=grid_y+Rall'*sin(-anglall(1:angldiv+1)/180*pi);
367
    
368
    close all
31 by Suren A. Chilingaryan
CUDAfication of real-time module
369
    imshow(im_grid,'InitialMagnification', 100);
1 by Suren A. Chilingaryan
Initial import
370
    hold on
371
    plot(grid_x,grid_y,'.b')    
372
    
373
    title(['Selected grid has ',num2str(angldiv*Rdiv), ' rasterpoints'])    % plot a title onto the image
374
375
    
376
    % Do you want to keep the grid?
377
    confirmselection = menu(sprintf('Do you want to use this grid?'),...
378
        'Yes','No, try again','Go back to grid-type selection');
379
    
380
    if confirmselection==1
381
        % Save settings and grid files in the image directory for visualization/plotting later
382
        %         save settings.dat xspacing yspacing xmin_new xmax_new ymin_new ymax_new -ascii -tabs
383
        save grid_x.dat grid_x -ascii -tabs
384
        save grid_y.dat grid_y -ascii -tabs
385
    end
386
    
387
    if confirmselection==2
388
        close all
389
        hold off
31 by Suren A. Chilingaryan
CUDAfication of real-time module
390
        imshow(im_grid,'InitialMagnification', 100);
1 by Suren A. Chilingaryan
Initial import
391
        circ_grid(FileNameBase, PathNameBase, im_grid);
392
    end
393
    
394
    if confirmselection==3
395
        gridtypeselection(FileNameBase, PathNameBase, im_grid);
396
    end
397
    
398
end
399
400
401
return
402
403
404
405
%-------------------------------
406
%
407
408
function [grid_x,grid_y,FileNameBase,PathNameBase] = rect_grid(FileNameBase, PathNameBase, im_grid);
409
410
title(sprintf('Define the region of interest.  Pick (single click) a point in the LOWER LEFT region of the gage section.\n  Do the same for a point in the UPPER RIGHT portion of the gage section.'))
411
412
[x(1,1),y(1,1)]=ginput(1);
413
hold on
414
plot(x(1,1),y(1,1),'+b')
415
416
[x(2,1),y(2,1)]=ginput(1);
417
hold on
418
plot(x(2,1),y(2,1),'+b')
419
420
drawnow
421
422
xmin = min(x);
423
xmax = max(x);
424
ymin = min(y);
425
ymax = max(y);
426
427
lowerline=[xmin ymin; xmax ymin];
428
upperline=[xmin ymax; xmax ymax];
429
leftline=[xmin ymin; xmin ymax];
430
rightline=[xmax ymin; xmax ymax];
431
432
plot(lowerline(:,1),lowerline(:,2),'-b')
433
plot(upperline(:,1),upperline(:,2),'-b')
434
plot(leftline(:,1),leftline(:,2),'-b')
435
plot(rightline(:,1),rightline(:,2),'-b')
436
437
% closereq
438
439
cd(PathNameBase)
440
441
% Prompt user for grid spacing/resolution
442
prompt = {'Enter horizontal (x) resolution for image analysis [pixels]:', ...
443
        'Enter vertical (y) resolution for image analysis [pixels]:'};
444
dlg_title = 'Input for grid creation';
445
num_lines= 1;
446
def     = {'50','50'};
447
answer = inputdlg(prompt,dlg_title,num_lines,def);
448
xspacing = str2num(cell2mat(answer(1,1)));
449
yspacing = str2num(cell2mat(answer(2,1)));
450
451
% Round xmin,xmax and ymin,ymax "up" based on selected spacing
452
numXelem = ceil((xmax-xmin)/xspacing)-1;
453
numYelem = ceil((ymax-ymin)/yspacing)-1;
454
455
xmin_new = (xmax+xmin)/2-((numXelem/2)*xspacing);
456
xmax_new = (xmax+xmin)/2+((numXelem/2)*xspacing);
457
ymin_new = (ymax+ymin)/2-((numYelem/2)*yspacing);
458
ymax_new = (ymax+ymin)/2+((numYelem/2)*yspacing);
459
460
% Create the analysis grid and show user
461
[x,y] = meshgrid(xmin_new:xspacing:xmax_new,ymin_new:yspacing:ymax_new);
462
[rows columns] = size(x);
463
zdummy = 200.*ones(rows,columns);
464
imshow(FileNameBase)
465
title(['Selected grid has ',num2str(rows*columns), ' rasterpoints'])    % plot a title onto the image
466
hold on;
467
plot(x,y,'+b')
468
469
grid_x=x;
470
grid_y=y;
471
472
% Do you want to keep the grid?
473
confirmselection = menu(sprintf('Do you want to use this grid?'),...
474
    'Yes','No, try again','Go back to grid-type selection');
475
476
if confirmselection==1
477
    % Save settings and grid files in the image directory for visualization/plotting later
478
    save settings.dat xspacing yspacing xmin_new xmax_new ymin_new ymax_new -ascii -tabs
479
    save grid_x.dat x -ascii -tabs
480
    save grid_y.dat y -ascii -tabs
481
    close all
482
    hold off
483
end
484
485
if confirmselection==2
486
    close all
487
    hold off
31 by Suren A. Chilingaryan
CUDAfication of real-time module
488
    imshow(im_grid,'InitialMagnification', 100);
1 by Suren A. Chilingaryan
Initial import
489
    rect_grid(FileNameBase, PathNameBase, im_grid);
490
end
491
492
if confirmselection==3
493
    close all
494
    hold off
495
    gridtypeselection(FileNameBase, PathNameBase, im_grid);
496
end