/normxcorr/trunk

To get this branch, use:
bzr branch http://suren.me/webbzr/normxcorr/trunk
1 by Suren A. Chilingaryan
Initial import
1
% written by Chris
2
3
function [rasterx, rastery, validx,validy,x,y]=ppselection_func(validx,validy,x,y);
4
5
% Code to analyze the displacement data (contained by validx and validy)
6
% Programmed by Chris
7
% Last revision: 8/24/06
8
9
% Prompt user for displacement data
10
if exist('validx')==0
11
    [validx,Pathvalidx] = uigetfile('*.mat','Open validx.mat');
12
    if validx==0
13
        return;
14
    end
15
    cd(Pathvalidx);
16
    validx=importdata(validx,'\t');
17
end
18
if exist('validy')==0
19
    [validy,Pathvalidy] = uigetfile('*.mat','Open validy.mat');
20
    if validy==0
21
        return;
22
    end
23
    cd(Pathvalidy);
24
    validy=importdata(validy,'\t');
25
end
26
27
% Checking for plot orientation and give standard orientations
28
if exist('x')==0
29
    x=1;
30
    y=2;
31
end
32
if x~1|2|3
33
    x=1;
34
    y=2;
35
end
36
37
validxbackup=validx;
38
validybackup=validy;
39
40
% Choose an image
41
[looppoints loopimages]=size(validx);
42
selectedimage=0;
43
prompt = {'From which image do you want to select the view?'};
44
dlg_title = 'Marker selection';
45
num_lines= 1;
46
if selectedimage==0
47
    defaultimage=loopimages;
48
end
49
if selectedimage~0
50
    defaultimage=selectedimage;
51
end
52
def     = {num2str(defaultimage)};
53
answer = inputdlg(prompt,dlg_title,num_lines,def);
54
selectedimage = str2num(cell2mat(answer(1,1)));
55
if selectedimage>loopimages
56
    selectedimage=loopimages;
57
end
58
if selectedimage<1
59
    selectedimage=1;
60
end
61
rasterx=0;
62
rastery=0;
63
% Call the selection tool
64
[rasterx, rastery, validx,validy,x,y]=gridtypeselection(validx,validy,x,y,selectedimage,rasterx,rastery);
65
66
return
67
68
%-------------------------------
69
%
70
% Decide which type of raster you want to analyze
71
72
function [rasterx, rastery, validx,validy,x,y] = gridtypeselection(validx,validy,x,y,selectedimage,rasterx,rastery);
73
74
gridselection = menu(sprintf('Which type of grid do you want to use'),...
75
    'Two Markers','Rectangular','Two Rectangles of Markers','Circular','Line','Change view','Cancel');
76
77
if gridselection==1
78
    [validx,validy,rasterx, rastery,selectedimage,x,y]=twop_grid_displ(validx,validy,x,y,selectedimage,rasterx,rastery);
79
    exist('validx');
80
    exist('validy');
81
    exist('rasterx');
82
    exist('rastery');
83
    exist('x');
84
    exist('y');
85
    return
86
end
87
88
if gridselection==2
89
    [validx,validy,rasterx, rastery,selectedimage,x,y]=rect_grid_displ(validx,validy,x,y,selectedimage,rasterx,rastery);
90
    exist('validx');
91
    exist('validy');
92
    exist('rasterx');
93
    exist('rastery');
94
    exist('x');
95
    exist('y');
96
    return
97
end
98
99
if gridselection==5
100
    [validx,validy,rasterx, rastery,selectedimage,x,y]=line_grid_displ(validx,validy,x,y,selectedimage,rasterx,rastery);
101
    exist('validx');
102
    exist('validy');
103
    exist('rasterx');
104
    exist('rastery');
105
    exist('x');
106
    exist('y');
107
    return
108
end
109
110
if gridselection==3
111
    [validx,validy,rasterx, rastery,selectedimage,x,y]=tworect_grid_displ(validx,validy,x,y,selectedimage,rasterx,rastery);
112
    exist('validx');
113
    exist('validy');
114
    exist('rasterx');
115
    exist('rastery');
116
    exist('x');
117
    exist('y');
118
    return
119
end
120
121
if gridselection==4
122
    [validx,validy,rasterx, rastery,selectedimage,x,y]=circ_grid_displ(validx,validy,x,y,selectedimage,rasterx,rastery);
123
    exist('validx');
124
    exist('validy');
125
    exist('rasterx');
126
    exist('rastery');
127
    exist('x');
128
    exist('y');
129
    return
130
end
131
132
if gridselection==6
133
    [validx,validy,x,y,selectedimage,rasterx,rastery] = change_view_displ(validx,validy,x,y,selectedimage,rasterx,rastery);
134
    exist('validx');
135
    exist('validy');
136
    exist('rasterx');
137
    exist('rastery');
138
    exist('x');
139
    exist('y');
140
end
141
142
if gridselection==7
143
    close all
144
    exist('validx');
145
    exist('validy');
146
    exist('rasterx');
147
    exist('rastery');
148
    exist('x');
149
    exist('y');
150
    if exist('validx')==0
151
        validx=0;
152
        validy=0;
153
    end
154
    if exist('rasterx')==0
155
        rasterx=0;
156
        rastery=0;
157
    end
158
    if exist('x')==0
159
        x=0;
160
        y=0;
161
    end
162
end
163
164
if exist('validx')==0
165
    validx=0
166
    validy=0
167
end
168
if exist('rasterx')==0
169
    rasterx=0
170
    rastery=0
171
end
172
if exist('x')==0
173
    x=0
174
    y=0
175
end
176
177
return
178
179
%-------------------------------
180
%
181
% Change the view
182
183
function [validx,validy,x,y,selectedimage,rasterx,rastery] = change_view_displ(validx,validy,x,y,selectedimage,rasterx,rastery);
184
185
[looppoints loopimages]=size(validx);
186
if x==1
187
    pos=validx;
188
end
189
if x==2
190
    pos=validy;
191
end
192
if x==3
193
    validxfirst=zeros(size(validx));
194
    validxfirst=validx(:,1)*ones(1,loopimages);
195
    pos=validx-validxfirst;
196
end
197
if x==4
198
    validyfirst=zeros(size(validy));
199
    validyfirst=validy(:,1)*ones(1,loopimages);
200
    pos=validy-validyfirst;
201
end
202
if y==1
203
    displ=validx;
204
end
205
if y==2
206
    displ=validy;
207
end
208
if y==3
209
    validxfirst=zeros(size(validx));
210
    validxfirst=validx(:,1)*ones(1,loopimages);
211
    displ=validx-validxfirst;
212
end
213
if y==4
214
    validyfirst=zeros(size(validy));
215
    validyfirst=validy(:,1)*ones(1,loopimages);
216
    displ=validy-validyfirst;
217
end
218
219
h=figure;
220
plot(pos(:,selectedimage),displ(:,selectedimage),'o','MarkerEdgeColor','k','MarkerFaceColor','g')
221
222
title(sprintf('View.') )
223
224
changeviewselection = menu(sprintf('Do you want to change the coordinate system to select markers?'),...
225
    'x-position vs. y-position','x-position vs. x-displacement','y-position vs. x-displacement',...
226
    'x-position vs. y-displacement','y-position vs. y-displacement','Change image #','Go back to grid-type selection');
227
228
if changeviewselection==1
229
    close all
230
    x=1;
231
    y=2;
232
    [validx,validy,x,y,selectedimage,rasterx,rastery] = change_view_displ(validx,validy,x,y,selectedimage,rasterx,rastery);
233
end
234
if changeviewselection==2
235
    close all
236
    x=1;
237
    y=3;
238
    [validx,validy,x,y,selectedimage,rasterx,rastery] = change_view_displ(validx,validy,x,y,selectedimage,rasterx,rastery);
239
end
240
if changeviewselection==3
241
    close all
242
    x=2;
243
    y=3;
244
    [validx,validy,x,y,selectedimage,rasterx,rastery] = change_view_displ(validx,validy,x,y,selectedimage,rasterx,rastery);
245
end
246
if changeviewselection==4
247
    close all
248
    x=1;
249
    y=4;
250
    [validx,validy,x,y,selectedimage,rasterx,rastery] = change_view_displ(validx,validy,x,y,selectedimage,rasterx,rastery);
251
end
252
if changeviewselection==5
253
    close all
254
    x=2;
255
    y=4;
256
    [validx,validy,x,y,selectedimage,rasterx,rastery] = change_view_displ(validx,validy,x,y,selectedimage,rasterx,rastery);
257
end
258
if changeviewselection==6
259
    prompt = {'From which image do you want to select the view?'};
260
    dlg_title = 'Marker selection';
261
    num_lines= 1;
262
    if selectedimage==0
263
        defaultimage=loopimages;
264
    end
265
    if selectedimage~0
266
        defaultimage=selectedimage;
267
    end
268
    def     = {num2str(defaultimage)};
269
    answer = inputdlg(prompt,dlg_title,num_lines,def);
270
    selectedimage = str2num(cell2mat(answer(1,1)));
271
    if selectedimage>loopimages
272
        selectedimage=loopimages;
273
    end
274
    if selectedimage<1
275
        selectedimage=1;
276
    end
277
    [validx,validy,x,y,selectedimage,rasterx,rastery] = change_view_displ(validx,validy,x,y,selectedimage,rasterx,rastery);
278
end
279
if changeviewselection==7
280
    close all
281
    [rasterx, rastery, validx,validy,x,y]=gridtypeselection(validx,validy,x,y,selectedimage,rasterx,rastery);
282
end
283
284
%-------------------------------
285
%
286
% Define two rectangles and add them to one marker array
287
288
function [validx,validy,rasterx,rastery,selectedimage,x,y] = tworect_grid_displ(validx,validy,x,y,selectedimage,rasterx,rastery);
289
290
[validx,validy,rasterx1, rastery1,selectedimage]=rect_grid_displ(validx,validy,x,y,selectedimage,rasterx,rastery);
291
[validx,validy,rasterx2, rastery2,selectedimage]=rect_grid_displ(validx,validy,x,y,selectedimage,rasterx,rastery);
292
293
% if size(rasterx1)~size(rasterx2)
294
%     close all
295
%     rasterx=0;rastery=0;
296
%     return
297
% end
298
299
rasterx=[rasterx1; rasterx2];
300
rastery=[rastery1; rastery2];
301
302
if x==1
303
    pos1=validx;pos2=rasterx;
304
end
305
if x==2
306
    pos1=validy;pos2=rasterx;
307
end
308
if x==3
309
    validxfirst=zeros(size(validx));rasterxfirst=zeros(size(rasterx));
310
    validxfirst=validx(:,1)*ones(1,loopimages);rasterxfirst=rasterx(:,1)*ones(1,loopimages);
311
    pos1=validx-validxfirst;pos2=rasterx-rasterxfirst;
312
end
313
if x==4
314
    validyfirst=zeros(size(validy));rasteryfirst=zeros(size(rastery));
315
    validyfirst=validy(:,1)*ones(1,loopimages);rasteryfirst=rastery(:,1)*ones(1,loopimages);
316
    pos1=validy-validyfirst;pos2=rastery-rasteryfirst;
317
end
318
if y==1
319
    displ1=validx;displ2=rasterx;
320
end
321
if y==2
322
    displ1=validy;displ2=rastery;
323
end
324
if y==3
325
    validyfirst=zeros(size(validy));rasteryfirst=zeros(size(rastery));
326
    validyfirst=validy(:,1)*ones(1,loopimages);rasteryfirst=rastery(:,1)*ones(1,loopimages);
327
    displ1=validy-validyfirst;displ2=rastery-rasteryfirst;
328
end
329
330
[looppoints loopimages]=size(validx);
331
defaultimage=loopimages;
332
333
prompt = {'From which image do you want to select the rectangles?'};
334
dlg_title = 'Marker selection';
335
num_lines= 1;
336
if selectedimage==0
337
    defaultimage=loopimages;
338
end
339
if selectedimage~0
340
    defaultimage=selectedimage;
341
end
342
def     = {num2str(defaultimage)};
343
answer = inputdlg(prompt,dlg_title,num_lines,def);
344
selectedimage = str2num(cell2mat(answer(1,1)));
345
if selectedimage>loopimages
346
    selectedimage=loopimages;
347
end
348
if selectedimage<1
349
    selectedimage=1;
350
end
351
352
h=figure;
353
plot(pos1(:,selectedimage),displ1(:,selectedimage),'o','MarkerEdgeColor','k','MarkerFaceColor','g')
354
hold on
355
plot(pos2(:,selectedimage),displ2(:,selectedimage),'o','MarkerEdgeColor','k','MarkerFaceColor','r')
356
357
% Accept the chosen markers, try again or give up 
358
359
confirmcircselection = menu(sprintf('Do you want to use these markers?'),...
360
    'Yes','No, try again','Go back to grid-type selection');
361
362
if confirmcircselection==2
363
    close all
364
    [validx,validy,rasterx,rastery,selectedimage,x,y] = tworect_grid_displ(validx,validy,x,y,selectedimage,rasterx,rastery);
365
end
366
367
if confirmcircselection==3
368
    close all
369
    [rasterx, rastery,validx,validy, x,y] = gridtypeselection(validx,validy,x,y,selectedimage,rasterx,rastery);
370
end
371
372
if confirmcircselection==1
373
    close all
374
end
375
376
%-------------------------------
377
%
378
% Define line and find markers
379
380
function [validx,validy,rasterx,rastery,selectedimage,x,y]=line_grid_displ(validx,validy,x,y,selectedimage,rasterx,rastery);
381
382
[looppoints loopimages]=size(validx);
383
defaultimage=loopimages;
384
385
if x==1
386
    pos=validx;
387
end
388
if x==2
389
    pos=validy;
390
end
391
if x==3
392
    validxfirst=zeros(size(validx));
393
    validxfirst=validx(:,1)*ones(1,loopimages);
394
    pos=validx-validxfirst;
395
end
396
if x==4
397
    validyfirst=zeros(size(validy));
398
    validyfirst=validy(:,1)*ones(1,loopimages);
399
    pos=validy-validyfirst;
400
end
401
if y==1
402
    displ=validx;
403
end
404
if y==2
405
    displ=validy;
406
end
407
if y==3
408
    validxfirst=zeros(size(validx));
409
    validxfirst=validx(:,1)*ones(1,loopimages);
410
    displ=validx-validxfirst;
411
end
412
if y==4
413
    validyfirst=zeros(size(validy));
414
    validyfirst=validy(:,1)*ones(1,loopimages);
415
    displ=validy-validyfirst;
416
end
417
418
prompt = {'From which image do you want to select markers?'};
419
dlg_title = 'Marker selection';
420
num_lines= 1;
421
if selectedimage==0
422
    defaultimage=loopimages;
423
end
424
if selectedimage~0
425
    defaultimage=selectedimage;
426
end
427
def     = {num2str(defaultimage)};
428
answer = inputdlg(prompt,dlg_title,num_lines,def);
429
selectedimage = str2num(cell2mat(answer(1,1)));
430
if selectedimage>loopimages
431
    selectedimage=loopimages;
432
end
433
if selectedimage<1
434
    selectedimage=1;
435
end
436
437
h=figure;
438
plot(pos(:,selectedimage),displ(:,selectedimage),'o','MarkerEdgeColor','k','MarkerFaceColor','g')
439
440
title(sprintf('Pick two points on the sample.') )
441
442
[xpos(1,1),ypos(1,1)]=ginput(1);
443
hold on
444
plot(xpos(1,1),ypos(1,1),'+g')
445
446
[xpos(2,1),ypos(2,1)]=ginput(1);
447
plot(xpos(2,1),ypos(2,1),'+g')
448
449
centerpoint=[xpos(1,1)+(xpos(2,1)-xpos(1,1))/2; ypos(1,1)+(ypos(2,1)-ypos(1,1))/2];
450
plot(centerpoint(1,1),centerpoint(2,1),'+b')
451
452
linelength=sqrt((xpos(2,1)-xpos(1,1))*(xpos(2,1)-xpos(1,1))+(ypos(2,1)-ypos(1,1))*(ypos(2,1)-ypos(1,1)));
453
lineslope=(ypos(2,1)-ypos(1,1))/(xpos(2,1)-xpos(1,1));
454
intersecty=ypos(1,1)-lineslope*xpos(1,1);
455
ycalc=zeros(2,1);
456
ycalc=lineslope*xpos+intersecty;
457
plot(xpos(:,1),ycalc(:,1),'-b')
458
intercept=[0; centerpoint(2,1)-centerpoint(1,1)*(-1/lineslope)];
459
460
distancefromline=(abs((xpos(2,1)-xpos(1,1))*(ypos(1,1)-displ(:,selectedimage))-(xpos(1,1)-pos(:,selectedimage))*(ypos(2,1)-ypos(1,1))))/sqrt((xpos(2,1)-xpos(1,1))*(xpos(2,1)-xpos(1,1))+(ypos(2,1)-ypos(1,1))*(ypos(2,1)-ypos(1,1)));
461
distancefromcenterpoint=(abs((intercept(1,1)-centerpoint(1,1))*(centerpoint(2,1)-displ(:,selectedimage))-(centerpoint(1,1)-pos(:,selectedimage))*(intercept(2,1)-centerpoint(2,1))))/sqrt((intercept(1,1)-centerpoint(1,1))*(intercept(1,1)-centerpoint(1,1))+(intercept(2,1)-centerpoint(2,1))*(intercept(2,1)-centerpoint(2,1)));
462
463
linewidthquestion=0;
464
linewidth=20;
465
466
while linewidthquestion==0
467
    prompt = {'Enter the width of the line in [pixel]:'};
468
    dlg_title = 'Input for grid creation';
469
    num_lines= 1;
470
    def     = {num2str(linewidth)};
471
    answer = inputdlg(prompt,dlg_title,num_lines,def);
472
    linewidth = str2num(cell2mat(answer(1,1)))
473
    
474
    selectpoints=find(distancefromline<linewidth & distancefromcenterpoint<linelength/2);
475
    plot(pos(selectpoints,selectedimage),displ(selectpoints,selectedimage),'.r')
476
    drawnow
477
    
478
    confirmlineselection = menu(sprintf('Do you want to use these markers?'),...
479
        'Yes','No, try different linewidth','No, different line','Go back to grid-type selection');
480
    
481
    if confirmlineselection==1
482
        close all
483
        linewidthquestion=1;
484
        rasterx=[validx(selectpoints,:);validx(selectpoints,:)];
485
        rastery=[validy(selectpoints,:);validy(selectpoints,:)];
486
        close all
487
    end
488
    if confirmlineselection==2
489
        hold off
490
        plot(pos(:,selectedimage),displ(:,selectedimage),'o','MarkerEdgeColor','k','MarkerFaceColor','g')
491
        hold on
492
        plot(xpos(:,1),ycalc(:,1),'-b')
493
        plot(xpos(1,1),ypos(1,1),'+g')
494
        plot(xpos(2,1),ypos(2,1),'+g')
495
    end
496
    if confirmlineselection==3
497
        close all
498
        linewidthquestion=1;
499
        [validx,validy,rasterx, rastery,selectedimage,x,y]=line_grid_displ(validx,validy,x,y,selectedimage,rasterx,rastery);
500
    end
501
    
502
    if confirmlineselection==4
503
        close all
504
        linewidthquestion=1;
505
        [rasterx, rastery,validx,validy, x,y] = gridtypeselection(validx,validy,x,y,selectedimage,rasterx,rastery);
506
    end
507
    
508
end
509
510
%-------------------------------
511
%
512
% Choose two markers
513
514
function [validx,validy,rasterx, rastery,selectedimage,x,y]=twop_grid_displ(validx,validy,x,y,selectedimage,rasterx,rastery);
515
516
[looppoints loopimages]=size(validx);
517
defaultimage=loopimages;
518
519
if x==1
520
    pos=validx;
521
end
522
if x==2
523
    pos=validy;
524
end
525
if x==3
526
    validxfirst=zeros(size(validx));
527
    validxfirst=validx(:,1)*ones(1,loopimages);
528
    pos=validx-validxfirst;
529
end
530
if x==4
531
    validyfirst=zeros(size(validy));
532
    validyfirst=validy(:,1)*ones(1,loopimages);
533
    pos=validy-validyfirst;
534
end
535
if y==1
536
    displ=validx;
537
end
538
if y==2
539
    displ=validy;
540
end
541
if y==3
542
    validxfirst=zeros(size(validx));
543
    validxfirst=validx(:,1)*ones(1,loopimages);
544
    displ=validx-validxfirst;
545
end
546
if y==4
547
    validyfirst=zeros(size(validy));
548
    validyfirst=validy(:,1)*ones(1,loopimages);
549
    displ=validy-validyfirst;
550
end
551
prompt = {'From which image do you want to select markers?'};
552
dlg_title = 'Marker selection';
553
num_lines= 1;
554
if selectedimage==0
555
    defaultimage=loopimages;
556
end
557
if selectedimage~0
558
    defaultimage=selectedimage;
559
end
560
def     = {num2str(defaultimage)};
561
answer = inputdlg(prompt,dlg_title,num_lines,def);
562
selectedimage = str2num(cell2mat(answer(1,1)));
563
if selectedimage>loopimages
564
    selectedimage=loopimages;
565
end
566
if selectedimage<1
567
    selectedimage=1;
568
end
569
570
h=figure;
571
plot(pos(:,selectedimage),displ(:,selectedimage),'o','MarkerEdgeColor','k','MarkerFaceColor','g')
572
573
title(sprintf('Pick two markers.') )
574
575
[xpos(1,1),ypos(1,1)]=ginput(1);
576
whereisthispoint=abs(pos(:,selectedimage)-xpos(1,1))+abs(displ(:,selectedimage)-ypos(1,1));
577
selectedpoint1=find(whereisthispoint==min(whereisthispoint));
578
hold on
579
plot(pos(selectedpoint1,selectedimage),displ(selectedpoint1,selectedimage),'+r')
580
581
[xpos(2,1),ypos(2,1)]=ginput(1);
582
whereisthispoint=abs(pos(:,selectedimage)-xpos(2,1))+abs(displ(:,selectedimage)-ypos(2,1));
583
selectedpoint2=find(whereisthispoint==min(whereisthispoint));
584
plot(pos(selectedpoint2,selectedimage),displ(selectedpoint2,selectedimage),'+r')
585
586
587
% Accept the chosen markers, try again or give up 
588
589
confirmcircselection = menu(sprintf('Do you want to use these two markers?'),...
590
    'Yes','No, try again','Go back to grid-type selection');
591
592
if confirmcircselection==2
593
    close all
594
    hold off
595
    [validx,validy,rasterx,rastery,selectedimage,x,y]=twop_grid_displ(validx,validy,x,y,selectedimage,rasterx,rastery);
596
end
597
598
if confirmcircselection==3
599
    close all
600
    [rasterx,rastery,validx,validy,x,y]=gridtypeselection(validx,validy,x,y,selectedimage,rasterx,rastery);
601
end
602
603
if confirmcircselection==1
604
    rasterx=[validx(selectedpoint1,:);validx(selectedpoint2,:)];
605
    rastery=[validy(selectedpoint1,:);validy(selectedpoint2,:)];
606
    close all
607
end
608
609
%-------------------------------
610
%
611
%Circular selection
612
613
function [validx,validy,rasterx,rastery,selectedimage,x,y] = circ_grid_displ(validx,validy,x,y,selectedimage,rasterx,rastery);
614
615
if x==1
616
    pos=validx;
617
end
618
if x==2
619
    pos=validy;
620
end
621
if x==3
622
    validxfirst=zeros(size(validx));
623
    validxfirst=validx(:,1)*ones(1,loopimages);
624
    pos=validx-validxfirst;
625
end
626
if x==4
627
    validyfirst=zeros(size(validy));
628
    validyfirst=validy(:,1)*ones(1,loopimages);
629
    pos=validy-validyfirst;
630
end
631
if y==1
632
    displ=validx;
633
end
634
if y==2
635
    displ=validy;
636
end
637
if y==3
638
    validxfirst=zeros(size(validx));
639
    validxfirst=validx(:,1)*ones(1,loopimages);
640
    displ=validx-validxfirst;
641
end
642
if y==4
643
    validyfirst=zeros(size(validy));
644
    validyfirst=validy(:,1)*ones(1,loopimages);
645
    displ=validy-validyfirst;
646
end
647
h=figure;
648
plot(pos(:,selectedimage),displ(:,selectedimage),'.b')
649
% axis equal
650
651
title(sprintf('Pick three points on the circle in clockwise order with the highest radius.') )
652
653
[xpos(1,1),ypos(1,1)]=ginput(1);
654
hold on
655
plot(xpos(1,1),ypos(1,1),'+g')
656
657
[xpos(2,1),ypos(2,1)]=ginput(1);
658
plot(xpos(2,1),ypos(2,1),'+g')
659
660
[xpos(3,1),ypos(3,1)]=ginput(1);
661
plot(xpos(3,1),ypos(3,1),'+g')
662
663
% Calculate center between the 3 sorted points and the normal slope of the vectors
664
slope12=-1/((ypos(2,1)-ypos(1,1))/(xpos(2,1)-xpos(1,1)));
665
slope23=-1/((ypos(3,1)-ypos(2,1))/(xpos(3,1)-xpos(2,1)));
666
center12(1,1)=(xpos(2,1)-xpos(1,1))/2+xpos(1,1);
667
center12(1,2)=(ypos(2,1)-ypos(1,1))/2+ypos(1,1);
668
center23(1,1)=(xpos(3,1)-xpos(2,1))/2+xpos(2,1);
669
center23(1,2)=(ypos(3,1)-ypos(2,1))/2+ypos(2,1);
670
plot(center12(1,1),center12(1,2),'+b')
671
plot(center23(1,1),center23(1,2),'+b')
672
673
if slope12==slope23
674
    [validx,validy,rasterx,rastery,selectedimage,x,y] = circ_grid_displ(validx,validy,x,y,selectedimage,rasterx,rastery)
675
end
676
677
% Calculate the crossing point of the two vectors
678
achsenabschnitt1=center12(1,2)-center12(1,1)*slope12;
679
achsenabschnitt2=center23(1,2)-center23(1,1)*slope23;
680
xcross=(achsenabschnitt2-achsenabschnitt1)/(slope12-slope23);
681
ycross=slope12*xcross+achsenabschnitt1;
682
xdata=min(xpos):xcross;
683
ydata1=achsenabschnitt1+slope12*xdata;
684
ydata2=achsenabschnitt2+slope23*xdata;
685
686
% Calculate radius
687
R=sqrt((xcross-xpos(1,1))*(xcross-xpos(1,1))+(ycross-ypos(1,1))*(ycross-ypos(1,1)))
688
689
% Calculate angle between vectors
690
xvector=[1;0];
691
x1vec(1,1)=xpos(1,1)-xcross;x1vec(2,1)=ypos(1,1)-ycross;
692
x3vec(1,1)=xpos(3,1)-xcross;x3vec(2,1)=ypos(3,1)-ycross;
693
alpha13=acos((dot(x1vec,x3vec))/(sqrt(x1vec'*x1vec)*sqrt(x3vec'*x3vec)))*180/pi;
694
alpha01=acos((dot(xvector,x1vec))/(sqrt(x1vec'*x1vec)*sqrt(xvector'*xvector)))*180/pi;
695
if ypos(1,1)<ycross
696
    alpha01=alpha01*(-1)+360;
697
end
698
alpha03=acos((dot(xvector,x3vec))/(sqrt(xvector'*xvector)*sqrt(x3vec'*x3vec)))*180/pi;
699
if ypos(3,1)<ycross
700
    alpha03=alpha03*(-1)+360;
701
end
702
totalangle=alpha13
703
minangle=alpha01
704
maxangle=alpha03
705
706
angldiv=abs(round(totalangle))*10;
707
anglstep=(totalangle/angldiv);
708
anglall(1:angldiv+1)=minangle-anglstep*(1:angldiv+1)-anglstep;
709
xcircle(1:angldiv+1)=xcross+R*cos(anglall(1:angldiv+1)/180*pi);
710
ycircle(1:angldiv+1)=ycross+R*sin(anglall(1:angldiv+1)/180*pi);
711
plot(xcircle,ycircle,'-r');
712
drawnow
713
714
% Accept the chosen circle, try again or give up 
715
confirmcircselection = menu(sprintf('Do you want to use this circle as basis?'),...
716
    'Yes','No, try again','Go back to grid-type selection');
717
if confirmcircselection==2
718
    close all
719
    hold off
720
    [validx,validy,rasterx,rastery,selectedimage,x,y] = circ_grid_displ(validx,validy,x,y,selectedimage,rasterx,rastery);
721
end
722
723
if confirmcircselection==3
724
    close all
725
    [rasterx,rastery,validx,validy,x,y]=gridtypeselection(validx,validy,x,y,selectedimage,rasterx,rastery);
726
end
727
728
if confirmcircselection==1
729
    
730
    % Pick the lower bound in the image
731
    title(sprintf('Pick lower bound for the raster') )
732
    
733
    [xpos(4,1),ypos(4,1)]=ginput(1);
734
    hold on
735
    plot(xpos(1,1),ypos(1,1),'+r')
736
    
737
    R2=sqrt((xcross-xpos(4,1))*(xcross-xpos(4,1))+(ycross-ypos(4,1))*(ycross-ypos(4,1)))
738
    xcrossmatrix=ones(size(pos(:,selectedimage)))*xcross;
739
    ycrossmatrix=ones(size(pos(:,selectedimage)))*ycross;
740
    
741
    % Calculate Radius for all points
742
    Rall=sqrt((xcrossmatrix-pos(:,selectedimage)).*(xcrossmatrix-pos(:,selectedimage))+(ycrossmatrix-displ(:,selectedimage)).*(ycrossmatrix-displ(:,selectedimage)));
743
    
744
    % Calculate Angle for all points relativ to circle center
745
    newpos=pos(:,selectedimage)-xcross;newdispl=displ(:,selectedimage)-ycross;
746
    angleallpoints=acos((newpos.*xvector(1,1)+newdispl.*xvector(2,1))./(sqrt(newpos.*newpos+newdispl.*newdispl).*sqrt(xvector(1,1).*xvector(1,1)+xvector(2,1).*xvector(2,1))))*180/pi;
747
    negativangle=find(displ(:,selectedimage)<ycross);
748
    angleallpoints(negativangle)=angleallpoints(negativangle)*(-1)+360;
749
    selectpoints=find(Rall>min(R,R2) & Rall<max(R,R2) & angleallpoints>maxangle & angleallpoints<minangle);
750
    plot(pos(selectpoints,selectedimage),displ(selectpoints,selectedimage),'.r')
751
    drawnow
752
    
753
    
754
    % Do you want to keep the grid?
755
    confirmselectionraster = menu(sprintf('Do you want to use this raster?'),...
756
        'Yes','No, try again','Go back to raster-type selection');
757
    
758
    if confirmselectionraster==1
759
        rasterx=pos(selectpoints,:);
760
        rastery=displ(selectpoints,:);
761
        close all
762
    end
763
    
764
    if confirmselectionraster==2
765
        rasterx=0;
766
        rastery=0;
767
        close all
768
        hold off
769
        exist('rasterx')
770
        exist('rastery')
771
        exist('x')
772
        exist('y')
773
        exist('validx')
774
        exist('validy')
775
        [validx,validy,rasterx,rastery,selectedimage,x,y] = circ_grid_displ(validx,validy,x,y,selectedimage,rasterx,rastery);
776
    end
777
    
778
    if confirmselectionraster==3
779
        close all
780
        [rasterx,rastery,validx,validy,x,y]=gridtypeselection(validx,validy,x,y,selectedimage,rasterx,rastery);
781
    end
782
    
783
    
784
end    
785
786
% 
787
%-------------------------------
788
%
789
790
function [validx,validy,rasterx, rastery,selectedimage,x,y]=rect_grid_displ(validx,validy,x,y,selectedimage,rasterx,rastery);
791
792
[looppoints loopimages]=size(validx);
793
defaultimage=loopimages;
794
795
if x==1
796
    pos=validx;
797
end
798
if x==2
799
    pos=validy;
800
end
801
if x==3
802
    validxfirst=zeros(size(validx));
803
    validxfirst=validx(:,1)*ones(1,loopimages);
804
    pos=validx-validxfirst;
805
end
806
if x==4
807
    validyfirst=zeros(size(validy));
808
    validyfirst=validy(:,1)*ones(1,loopimages);
809
    pos=validy-validyfirst;
810
end
811
if y==1
812
    displ=validx;
813
end
814
if y==2
815
    displ=validy;
816
end
817
if y==3
818
    validxfirst=zeros(size(validx));
819
    validxfirst=validx(:,1)*ones(1,loopimages);
820
    displ=validx-validxfirst;
821
end
822
if y==4
823
    validyfirst=zeros(size(validy));
824
    validyfirst=validy(:,1)*ones(1,loopimages);
825
    displ=validy-validyfirst;
826
end
827
828
prompt = {'From which image do you want to select markers?'};
829
dlg_title = 'Marker selection';
830
num_lines= 1;
831
if selectedimage==0
832
    defaultimage=loopimages;
833
end
834
if selectedimage~0
835
    defaultimage=selectedimage;
836
end
837
def     = {num2str(defaultimage)};
838
answer = inputdlg(prompt,dlg_title,num_lines,def);
839
selectedimage = str2num(cell2mat(answer(1,1)));
840
if selectedimage>loopimages
841
    selectedimage=loopimages;
842
end
843
if selectedimage<1
844
    selectedimage=1;
845
end
846
847
h=figure;
848
plot(pos(:,selectedimage),displ(:,selectedimage),'o','MarkerEdgeColor','k','MarkerFaceColor','g')
849
title(sprintf('Define the region of interest.  Pick (single click) a point in the LOWER LEFT of your selection.\n  Do the same for a point in the UPPER RIGHT.'))
850
hold on
851
[xpos(1,1),ypos(1,1)]=ginput(1);
852
hold on
853
plot(xpos(1,1),ypos(1,1),'+b')
854
drawnow
855
856
[xpos(2,1),ypos(2,1)]=ginput(1);
857
hold on
858
plot(xpos(2,1),ypos(2,1),'+b')
859
drawnow
860
861
xmin = min(xpos);
862
xmax = max(xpos);
863
ymin = min(ypos);
864
ymax = max(ypos);
865
866
lowerline=[xmin ymin; xmax ymin];
867
upperline=[xmin ymax; xmax ymax];
868
leftline=[xmin ymin; xmin ymax];
869
rightline=[xmax ymin; xmax ymax];
870
871
plot(lowerline(:,1),lowerline(:,2),'-b')
872
plot(upperline(:,1),upperline(:,2),'-b')
873
plot(leftline(:,1),leftline(:,2),'-b')
874
plot(rightline(:,1),rightline(:,2),'-b')
875
876
selectpoints=find(pos(:,selectedimage)>min(xpos) & pos(:,selectedimage)<max(xpos) & displ(:,selectedimage)<max(ypos) & displ(:,selectedimage)>min(ypos))
877
clear pos
878
clear displ
879
880
if x==1
881
    pos=validx(selectpoints,:);
882
end
883
if x==2
884
    pos=validy(selectpoints,:);
885
end
886
if x==3
887
    validxfirst=zeros(size(validx));
888
    validxfirst=validx(:,1)*ones(1,loopimages);
889
    pos=validx(selectpoints,:)-validxfirst(selectpoints,:);
890
end
891
if x==4
892
    validyfirst=zeros(size(validy));
893
    validyfirst=validy(:,1)*ones(1,loopimages);
894
    pos=validy(selectpoints,:)-validyfirst(selectpoints,:);
895
end
896
if y==1
897
    displ=validx(selectpoints,:);
898
end
899
if y==2
900
    displ=validy(selectpoints,:);
901
end
902
if y==3
903
    validxfirst=zeros(size(validx));
904
    validxfirst=validx(:,1)*ones(1,loopimages);
905
    displ=validx(selectpoints,:)-validxfirst(selectpoints,:);
906
end
907
if y==4
908
    validyfirst=zeros(size(validy));
909
    validyfirst=validy(:,1)*ones(1,loopimages);
910
    displ=validy(selectpoints,:)-validyfirst(selectpoints,:);
911
end
912
913
plot(pos(:,selectedimage),displ(:,selectedimage),'o','MarkerEdgeColor','k','MarkerFaceColor','r')
914
title(sprintf('Red dots represent your new raster.'))
915
hold off
916
917
% Do you want to keep the grid?
918
confirmselection = menu(sprintf('Do you want to use this raster?'),...
919
    'Yes','No, try again','Go back to raster-type selection');
920
921
if confirmselection==1
922
    rasterx=pos;
923
    rastery=displ;
924
    close all
925
end
926
927
if confirmselection==2
928
    rasterx=0;
929
    rastery=0;
930
    close all
931
    hold off
932
    exist('rasterx')
933
    exist('rastery')
934
    exist('x')
935
    exist('y')
936
    exist('validx')
937
    exist('validy')
938
    [validx,validy,rasterx, rastery,selectedimage,x,y]=rect_grid_displ(validx,validy,x,y,selectedimage,rasterx,rastery);
939
end
940
941
if confirmselection==3
942
    close all
943
    [rasterx,rastery,validx,validy,x,y]=gridtypeselection(validx,validy,x,y,selectedimage,rasterx,rastery);
944
end