/normxcorr/trunk

To get this branch, use:
bzr branch http://suren.me/webbzr/normxcorr/trunk
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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
% Initialize data
% written by Chris and Dan

% Displacement.m allows you to analyze the data you aquiered with the
% correlation, fitting or mean routine. It only needs the validx and
% validy and can calculate strain from it. Before you start you should 
% consider cleaning up the data as described in the guide. After that step
% you can analyze parts of your data, or the full set. Try to use also the
% console command, e.g. if you want to analyze only image 100-110 since
% something really interesting happend there, load validx and validy into
% your workspace and call
% displacement(validx(:,100:110),validy(:,100:110));
% In this case displacement only loads the important images and you can
% clean this part of your data set.

% Changed 3. February 2008


function [validx,validy]=displacement(validx,validy);

%load data in case you did not load it into workspace yet
if exist('validx')==0
    [validxname,Pathvalidx] = uigetfile('*.dat','Open validx.dat');
    if validxname==0
        disp('You did not select a file!')
        return
    end
    cd(Pathvalidx);
    validx=importdata(validxname,'\t');
end
if exist('validy')==0
    [validyname,Pathvalidy] = uigetfile('*.dat','Open validy.dat');
    if validyname==0
        disp('You did not select a file!')
        return
    end
    cd(Pathvalidy);
    validy=importdata(validyname,'\t');
end

%define the size of the data set
sizevalidx=size(validx);
sizevalidy=size(validy);
looppoints=sizevalidx(1,1);
loopimages=sizevalidx(1,2);

%calculate the displacement relative to the first image in x and y
%direction
clear displx;
validxfirst=zeros(size(validx));
validxfirst=mean(validx(:,1),2)*ones(1,sizevalidx(1,2));
displx=validx-validxfirst;
clear validxfirst
clear disply;
validyfirst=zeros(size(validy));
validyfirst=mean(validy(:,1),2)*ones(1,sizevalidy(1,2));
disply=validy-validyfirst;
clear validyfirst

%Prompt user for type of plotting / visualization
selection10 = menu(sprintf('How do you want to visualize your data?'),'3D Mesh Plot of Displacement'...
    ,'Full Strain Plots','Strain Measurement between 2 Points','1D Average Strain Measurement',...
    'Rotate Orientation (exchange x and y)','Remove badly tracked marker, one by one (Position)',...
    'Delete multible markers (Position)','Delete markers from displacement vs. position plot',...
    'Delete points moving relative to their neighbours','Select Markers to Analyze ',...
    'Save validx and validy','Average a couple of images','Cancel');

% Selection for Cancel - All windows will be closed and you jump back to
% the command line
if selection10==13
    close all;
    return
end

% This selection will average up a specified number of images to reduce the
% noise of the data set. I would like to point out that you will need to
% average your other sensor data (e.g. load data), too, to match it to your
% strain data.
if selection10==12
    prompt = {'How many images would you like to combine as a base image?'};
    dlg_title = 'Input number of images:';
    num_lines= 1;
    def     = {'5'};
    answer = inputdlg(prompt,dlg_title,num_lines,def);
    if str2num(cell2mat(answer(1)))==0
        disp('Get out, you changed your mind?')
        [validx validy]=displacement(validx,validy);
        return
    else
        baseimages = str2num(cell2mat(answer(1)));
        if baseimages==[]
            disp('Give me a number, will you?')
            [validx validy]=displacement(validx,validy);
            return
        end
        if baseimages>loopimages
            disp('That is too large?!')
        else
            baseimagemean=mean(validx(:,1:baseimages),2);
            validx(:,1:baseimages-1)=[];
            validx(:,1)=baseimagemean;
            baseimagemean=mean(validy(:,1:baseimages),2);
            validy(:,1:baseimages-1)=[];
            validy(:,1)=baseimagemean;
        end
    end
    [validx validy]=displacement(validx,validy);
    return
end

% Save validx and validy, very useful if you cleaned up your dataset. Data
% will be saved as -ascii text file. If you send data like this by email
% you can reduce the size tremendously by compressing it. Use ZIP or RAR.
if selection10==11
    [FileName,PathName] = uiputfile('validx_corr.dat','Save validx');
    if FileName==0
        disp('You did not save your file!')
        [validx validy]=displacement(validx,validy);
        return
    else
        cd(PathName)
        save(FileName,'validx','-ascii')
        [FileName,PathName] = uiputfile('validy_corr.dat','Save validy');
        if FileName==0
            disp('You did not save your file!')
            [validx validy]=displacement(validx,validy);
        else
            cd(PathName)
            save(FileName,'validy','-ascii')
        end
        [validx validy]=displacement(validx,validy);
        return
    end
end

% Select Points from detailed Analysis
if selection10==10
    [validx validy validxbackup validybackup]=ppselection_func(validx,validy);
    if validx==0
        validx=validxbackup;
        validy=validybackup;
    end
    if validy==0
        validx=validxbackup;
        validy=validybackup;
    end
    [validx validy]=displacement(validx,validy);
end

% Remove markers moving relativ to their neighbours
if selection10==9
    [validx,validy,displx,disply]=delete_jumpers(validx,validy,displx,disply);
    [validx validy]=displacement(validx,validy);
end

% Remove markers from the displacement vs. position plot
if selection10==8
    [validx,validy,displx,disply]=removepoints_func(validx,validy,displx,disply);
    [validx validy]=displacement(validx,validy);
end

% Remove bad points
if selection10==7
    [validx,validy]=removepoints_func2(validx,validy);
    [validx validy]=displacement(validx,validy);
end

% Remove bad points
if selection10==6
    [validx validy]=removepoints_func3(validx,validy);
    [validx validy]=displacement(validx,validy);
end

% Rotate Matrix
if selection10==5
    [validx, validy]=rotatematrix(validx,validy);
    [validx validy]=displacement(validx,validy);
end

% 1D Strain plot using average strains for ELASTIC STRAIN only
if selection10==4
    [validx validy]=strain_1D_average_func(validx, validy,displx,disply);
    [validx validy]=displacement(validx,validy);
end

% 1D Strain plot
if selection10==3
    [validx, validy,displx,disply]=strain_1D_2Points_func(validx, validy,displx,disply);
    [validx validy]=displacement(validx,validy);
end

% Fast plotting, cropping needed for polynomial fit
if selection10==2
    [validx, validy,displx,disply]=polyfit3D(validx, validy,displx,disply);
    [validx validy]=displacement(validx,validy);
end

% 3D Mesh Plotting
if selection10==1
    if sizevalidx(1,1)>2
    [validx, validy,displx,disply]=meshplot(validx,validy,displx,disply);
    else
        disp('You need at least three markers to display the 3D-plot')
        msgbox('You need at least three markers to display the 3D-plot','3D-Plot','warn');
    end
    [validx validy]=displacement(validx,validy);
end

%---------------------------------

function [validx,validy,displx,disply]=delete_jumpers(validx,validy,displx,disply);

% written by Chris

% This is a filter which helps to find jumpy data points which are 
% oscillating or stop moving.
% The Filter starts by finding the next 10 datapoint neighbours 
% (num_neighbours), calculates their mean position and then plots the
% difference between each data point and its neighbours versus image
% number. If a data point is jumping around it will show up as a spike. But
% be careful, one bad one will also affect his neighbours, therefore its
% worthwhile to use this filter step by step.

% Changed 3. February 2008

num_neighbours=10;

doitonemoretime=1

while doitonemoretime==1
    % defining variables
    sizevalidx=size(validx);
    sizevalidy=size(validy);
    looppoints=sizevalidx(1,1);
    loopimages=sizevalidx(1,2);

    % clear the used ones
    clear validxtemp
    clear validytemp
    clear meandistancetemp
    clear sizevalidxtemp
    clear sizevalidytemp
    clear looppointstemp
    clear loopimagestemp
    clear max_distance
    clear min_distance
    clear dist_matrix
    clear dist_sort
    clear dist_index
    clear meandistance
    tic
    % calculate the distance to the next data points
%     dist_matrix=zeros(looppoints,looppoints);
    meandistance=zeros(sizevalidx);
    
    g=waitbar(0,'Processing the markers...');
    for i=1:looppoints
        waitbar(i/looppoints);
        dist_matrix=(((validx(:,1)-validx(i,1)).^2+(validy(:,1)-validy(i,1)).^2).^(0.5))';
        %   end

        % find the next neighbours by indexing the ones closest
        [dist_sort, dist_index]=sort(dist_matrix);

        % take the mean position of the closest data points of each for all
        % images
        meandistance(i,:)= validx(i,:)-mean(validx(dist_index(2:num_neighbours),:),1);
        max_distance(i)= max(diff(meandistance(i,:)-meandistance(i,1)));
        min_distance(i)= min(diff(meandistance(i,:)-meandistance(i,1)));
    end
    close(g)
toc
    for i=1:looppoints
        plot(diff(meandistance(i,:)-meandistance(i,1)))
        hold on
    end
   toc     
    % Select an upper and lower boundary
    xlabel('Image number[ ]')
    ylabel('Relative marker dispacement [Pixels]')
    title(sprintf('Define the upper and lower bound by clicking above and below the valid points'))
    marker_pt=(ginput(1));
    x_mark(1,1) = marker_pt(1);
    y_mark(1,1) = marker_pt(2);
    plot([1;loopimages],[y_mark(1,1);y_mark(1,1)],'r');

    title(sprintf('Define the upper and lower bound by clicking above and below the valid points'))
    marker_pt=(ginput(1));
    x_mark(1,2) = marker_pt(1);
    y_mark(1,2) = marker_pt(2);
    plot([1;loopimages],[y_mark(1,2);y_mark(1,2)],'r');

    upperbound=max(y_mark);
    lowerbound=min(y_mark);

    hold off
    
    validxtemp=validx;
    validytemp=validy;
    meandistancetemp=meandistance;
    
    
    validxtemp(find(max_distance>upperbound | min_distance<lowerbound),:)=[];
    validytemp(find(max_distance>upperbound | min_distance<lowerbound),:)=[];
    meandistancetemp(find(max_distance>upperbound |min_distance<lowerbound),:)=[];
    sizevalidxtemp=size(validxtemp);
    sizevalidytemp=size(validytemp);
    looppointstemp=sizevalidxtemp(1,1);
    loopimagestemp=sizevalidxtemp(1,2);
    
    for i=1:looppointstemp
        plot(diff(meandistancetemp(i,:)-meandistancetemp(i,1)))
        hold on
    end
    plot([1;loopimages],[y_mark(1,1);y_mark(1,1)],'r');
    plot([1;loopimages],[y_mark(1,2);y_mark(1,2)],'r');
    
    hold off

    selection_filter = menu('Do you like the result?','Take it as is','Want to select more','Try again','Cancel');
    if selection_filter==1
        validx=validxtemp;
        validy=validytemp;
        doitonemoretime=0;
    elseif selection_filter==2
        validx=validxtemp;
        validy=validytemp;
        doitonemoretime=1;
    elseif selection_filter==3
        doitonemoretime=1;
    elseif selection_filter==4
        return
    end
end

%---------------------------------
% Rotate Matrix
% written by Chris
function [validx, validy]=rotatematrix(validx,validy);
validxrot=validx;
clear validx;
validyrot=validy;
clear validy;
validy=validxrot;
validx=validyrot;


%---------------------------------
% Delete points from the displacement plot
% written by Chris
function [validx,validy,displx,disply] = removepoints_func(validx,validy,displx,disply) ; %delete points

close all

if exist('validx')==0
    [validx,Pathvalidx] = uigetfile('*.mat; *.txt','Open validx.mat or validx.txt');
    cd(Pathvalidx);
    validx=importdata(validx,'\t');
    [validy,Pathvalidy] = uigetfile('*.mat;*.txt','Open validy.mat or validy.txt');
    cd(Pathvalidy);
    validy=importdata(validy,'\t');
end

selectremove1 = menu(sprintf('Do you want to delete makers?'),'Yes','No');
if selectremove1==2
    return
end

% if yes
if selectremove1==1

    sizevalidx=size(validx);
    sizevalidy=size(validy);

    selectionremove2=selectremove1;
    counter=0
    sizevalidx=size(validx);
    looppoints=sizevalidx(1,1);
    loopimages=sizevalidx(1,2);
    defaultimage=loopimages
    numberbadpoints=0

    while selectionremove2==1
        counter=counter+1
        clear xplot
        clear sizevalidx
        clear selectremove11
        clear selection2
        %         clear badpoints

        sizevalidx=size(validx);
        looppoints=sizevalidx(1,1);
        loopimages=sizevalidx(1,2);

        % update temporary matrices
        validxtemp=validx;
        validytemp=validy;
        displxtemp=displx;
        displytemp=disply;

        % get the image number from which the bad points will be chosen
        prompt = {'From which image do you want to delete markers?'};
        dlg_title = 'Marker removal';
        num_lines= 1;
        if numberbadpoints==0
            defaultimage=loopimages
        end
        if numberbadpoints~0
            defaultimage=numberbadpoints
        end
        def     = {num2str(defaultimage)};
        answer = inputdlg(prompt,dlg_title,num_lines,def);
        numberbadpoints = str2num(cell2mat(answer(1,1)));
        if numberbadpoints>loopimages
            numberbadpoints=loopimages
        end
        if numberbadpoints<1
            numberbadpoints=1
        end

%         displx(:,1)=-validx(:,1)+validx(:,numberbadpoints);
%         disply(:,1)=-validy(:,1)+validy(:,numberbadpoints);

        plot(validx(:,numberbadpoints),displx(:,numberbadpoints),'o');
        xlabel('position [pixel]')
        ylabel('displacement [pixel]')
        title(['Displacement versus position',sprintf(' (Current image #: %1g)',numberbadpoints)]);

%         validxtemp=validx;
%         validytemp=validy;
        displxtemp=displx;
        validxdelete=validxtemp;
        validydelete=validytemp;
        displxdelete=displxtemp;
        displydelete=displytemp;

        title(sprintf('Define the region of interest.  \n  All points ouside that region will be deleted'))

        [xgrid,ygrid]=ginput(2);
        x(1,1) = xgrid(1);
        x(1,2) = xgrid(2);
        y(1,1) = ygrid(2);
        y(1,2) = ygrid(1);

        deletepoints=find(validxdelete(:,numberbadpoints)>min(x) & validxdelete(:,numberbadpoints)<max(x) & displxdelete(:,numberbadpoints)<max(y) & displxdelete(:,numberbadpoints)>min(y));
        [loopnum one]=size(deletepoints);

        validxdelete(deletepoints,:)=[];
        validydelete(deletepoints,:)=[];


        % update temporary data matrices; delete bad points
        displxtemp(deletepoints,:)=[];
        displytemp(deletepoints,:)=[];
        validxtemp(deletepoints,:)=[];
        validytemp(deletepoints,:)=[];

        plot(validxtemp(:,numberbadpoints),displxtemp(:,numberbadpoints),'o');

        % delete point permanently?
        selectremove3 = menu(sprintf('Do you want to delete these markers permanently?'),'Yes','No');
        if selectremove3==1
            displx=displxtemp;
            disply=displytemp;
            validx=validxtemp;
            validy=validytemp;
        end
        if selectremove3==2
            displxtemp=displx;
            displytemp=disply;
            validxtemp=validx;
            validytemp=validy;
        end
        selectremove2 = menu(sprintf('Do you want to mark another bad point?'),'Yes','No');
        if selectremove2==2
            clear displx;
            clear disply;
            validxfirst=zeros(size(validx));
            validxfirst=validx(:,1)*ones(1,sizevalidx(1,2));
            validyfirst=zeros(size(validy));
            validyfirst=validy(:,1)*ones(1,sizevalidy(1,2));
            displx=validx-validxfirst;
            disply=validy-validyfirst;
            return
        end
    end

end

%---------------------------------
% Delete single points
% written by Chris
function [validx,validy]=removepoints_func3(validx,validy);

%sort out badpoints?

selection1 = menu(sprintf('Do you want to mark bad points?'),'Yes','No');
if selection1==2
    close all;
    return
end

% if yes
if selection1==1
    selection2=selection1;
    %     figure
    counter=0
    sizevalidx=size(validx);
    looppoints=sizevalidx(1,1);
    loopimages=sizevalidx(1,2)-1;
    defaultimage=loopimages
    numberbadpoints=0

    while selection2==1
        counter=counter+1
        clear xplot
        clear sizevalidx
        clear selection1
        clear selection2
        clear badpoints

        sizevalidx=size(validx);
        looppoints=sizevalidx(1,1);
        loopimages=sizevalidx(1,2)-1;

        clear displx;
        validxfirst=zeros(size(validx));
        validxfirst=validx(:,1)*ones(1,sizevalidx(1,2));
        displx=validx-validxfirst;

        % update temporary matrices
        displxtemp=displx;
        validxtemp=validx;
        validytemp=validy;
        %         resnormxtemp=resnormx;

        % get the image number from which the bad points will be chosen
        prompt = {'From which image do you want to choose the bad points?'};
        dlg_title = 'Bad points removal';
        num_lines= 1;
        if numberbadpoints==0
            defaultimage=loopimages
        end
        if numberbadpoints~0
            defaultimage=numberbadpoints
        end
        def     = {num2str(defaultimage)};
        answer = inputdlg(prompt,dlg_title,num_lines,def);
        numberbadpoints = str2num(cell2mat(answer(1,1)));
        if numberbadpoints>loopimages
            numberbadpoints=loopimages
        end
        if numberbadpoints<1
            numberbadpoints=1
        end

        gridsizex=10*round(min(min(validx))/10):10:10*round(max(max(validx))/10);
        gridsizey=10*round(min(min(validy))/10):10:10*round(max(max(validy))/10);
        [XI,YI]=meshgrid(gridsizex,gridsizey);
        ZI=griddata(validx(:,numberbadpoints),validy(:,numberbadpoints),displx(:,numberbadpoints),XI,YI,'cubic');
        epsxx = gradient(ZI,10,10);

        % find max and min point and point them out
        mindisplx=find(displx(:,numberbadpoints)==min(displx(:,numberbadpoints)));
        maxdisplx=find(displx(:,numberbadpoints)==max(displx(:,numberbadpoints)));


        pcolor(XI,YI,epsxx);
        axis('equal')
        caxis([min(min(epsxx)) max(max(epsxx))])
        colorbar
        shading('interp')
        hold on
        plot3(validx(:,numberbadpoints),validy(:,numberbadpoints),displx(:,numberbadpoints)-min(displx(:,numberbadpoints)),'o','MarkerEdgeColor','k','MarkerFaceColor','g'), hold on;
        plot3(validx(mindisplx,numberbadpoints),validy(mindisplx,numberbadpoints),displx(mindisplx,numberbadpoints)-min(displx(:,numberbadpoints)),'o','MarkerEdgeColor','y','MarkerFaceColor','b')
        plot3(validx(maxdisplx,numberbadpoints),validy(maxdisplx,numberbadpoints),displx(maxdisplx,numberbadpoints)-min(displx(:,numberbadpoints)),'o','MarkerEdgeColor','y','MarkerFaceColor','r')
        axis([min(min(XI))-10 max(max(XI))+10 min(min(YI))-10 max(max(YI))+10])
        drawnow;
        hold off

        % get the bad point position

        title(sprintf('Click on the bad point',counter))
        [badpoint]=ginput(1);
        badpointx = badpoint(1,1);
        badpointy = badpoint(1,2);

        % find the point matching the given position
        wherethehellisthispoint=abs(validx(:,numberbadpoints)-badpoint(1,1))+abs(validy(:,numberbadpoints)-badpoint(1,2));
        badpointnum=find(wherethehellisthispoint==min(wherethehellisthispoint));

        % update temporary data matrices; delete bad points

        displxtemp(badpointnum,:)=[];
        validxtemp(badpointnum,:)=[];
        validytemp(badpointnum,:)=[];
        mindisplx=find(displxtemp(:,numberbadpoints)==min(displxtemp(:,numberbadpoints)));
        maxdisplx=find(displxtemp(:,numberbadpoints)==max(displxtemp(:,numberbadpoints)));

        % update the figure
        ZI=griddata(validxtemp(:,numberbadpoints),validytemp(:,numberbadpoints),displxtemp(:,numberbadpoints),XI,YI,'cubic');
        epsxx = gradient(ZI,10,10);
        pcolor(XI,YI,epsxx);
        axis('equal')
        caxis([min(min(epsxx)) max(max(epsxx))])
        colorbar
        shading('interp')
        hold on
        plot3(validxtemp(:,numberbadpoints),validytemp(:,numberbadpoints),displxtemp(:,numberbadpoints),'o','MarkerEdgeColor','k','MarkerFaceColor','g')
        plot3(validxtemp(mindisplx,numberbadpoints),validytemp(mindisplx,numberbadpoints),displxtemp(mindisplx,numberbadpoints)-min(displxtemp(:,numberbadpoints)),'o','MarkerEdgeColor','y','MarkerFaceColor','b')
        plot3(validxtemp(maxdisplx,numberbadpoints),validytemp(maxdisplx,numberbadpoints),displxtemp(maxdisplx,numberbadpoints)-min(displxtemp(:,numberbadpoints)),'o','MarkerEdgeColor','y','MarkerFaceColor','r')
        axis([min(min(XI))-10 max(max(XI))+10 min(min(YI))-10 max(max(YI))+10])
        drawnow;hold off;

        % delete point permanently?
        selection3 = menu(sprintf('Do you want to delete this point permanently?'),'Yes','No');
        if selection3==1
            displx=displxtemp;
            validx=validxtemp;
            validy=validytemp;
            %             resnormx=resnormxtemp;
        end
        if selection3==2
            displxtemp=displx;
            validxtemp=validx;
            validytemp=validy;
            %             resnormxtemp=resnormx;
        end
        selection2 = menu(sprintf('Do you want to mark another bad point?'),'Yes','No');
        if selection2==2
            close all;
            return
        end

    end
end

%---------------------------------
% Strain between two markers
% written by Chris
function [validx, validy,displx,disply] = strain_1D_2Points_func(validx, validy,displx,disply) ; % 1D strain calculation

sizevalidx=size(validx);
looppoints=sizevalidx(1,1);
loopimages=sizevalidx(1,2)-1;
defaultimage=loopimages;
numberbadpoints=0;
clear selection3; selection3=1;

while selection3==1

    clear xplot
    clear sizevalidx
    clear selection1
    clear selection2
    clear badpoints

    sizevalidx=size(validx);
    looppoints=sizevalidx(1,1);
    loopimages=sizevalidx(1,2)-1;

    % update temporary matrices
    displxtemp=displx;
    validxtemp=validx;
    validytemp=validy;
    %     resnormxtemp=resnormx;

    % get the image number from which the bad points will be chosen
    prompt = {'Which image do you want for point selection?'};
    dlg_title = '1D Strain Plotting';
    num_lines= 1;
    if numberbadpoints==0
        defaultimage=loopimages;
    end
    if numberbadpoints~0
        defaultimage=numberbadpoints
    end
    def     = {num2str(defaultimage)};
    answer = inputdlg(prompt,dlg_title,num_lines,def);
    numberbadpoints = str2num(cell2mat(answer(1,1)));
    if numberbadpoints>loopimages
        numberbadpoints=loopimages;
    end
    if numberbadpoints<1
        numberbadpoints=1;
    end

    gridsizex=10*round(min(min(validx))/10):10:10*round(max(max(validx))/10);
    gridsizey=10*round(min(min(validy))/10):10:10*round(max(max(validy))/10);
    [XI,YI]=meshgrid(gridsizex,gridsizey);
    ZI=griddata(validx(:,numberbadpoints),validy(:,numberbadpoints),displx(:,numberbadpoints),XI,YI,'cubic');

    pcolor(XI,YI,ZI);
    axis('equal')
    caxis([min(min(ZI)) max(max(ZI))])
    colorbar
    shading('interp')
    hold on
    plot3(validx(:,numberbadpoints),validy(:,numberbadpoints),abs(displx(:,numberbadpoints)),'o','MarkerEdgeColor','k','MarkerFaceColor','g');
    axis([min(min(XI))-10 max(max(XI))+10 min(min(YI))-10 max(max(YI))+10])
    drawnow;

    % get the bad point position

    title(sprintf('Click on the two points for strain measurement'))
    [badpoint]=ginput(2);
    badpointx = badpoint(1,1);
    badpointy = badpoint(1,2);
    badpointx2 = badpoint(2,1);
    badpointy2 = badpoint(2,2);

    % find the point matching the given position
    wherethehellisthispoint=abs(validx(:,numberbadpoints)-badpoint(1,1))+abs(validy(:,numberbadpoints)-badpoint(1,2));
    badpointnum=find(wherethehellisthispoint==min(wherethehellisthispoint));
    wherethehellisthispoint2=abs(validx(:,numberbadpoints)-badpoint(2,1))+abs(validy(:,numberbadpoints)-badpoint(2,2));
    badpointnum2=find(wherethehellisthispoint2==min(wherethehellisthispoint2));


    % update the figure
    ZI=griddata(validxtemp(:,numberbadpoints),validytemp(:,numberbadpoints),displxtemp(:,numberbadpoints),XI,YI,'cubic');
    caxis([min(min(ZI)) max(max(ZI))])
    plot3(validxtemp(badpointnum,numberbadpoints),validytemp(badpointnum,numberbadpoints),displxtemp(badpointnum,numberbadpoints),'+','MarkerEdgeColor','k','MarkerFaceColor','g')
    plot3(validxtemp(badpointnum2,numberbadpoints),validytemp(badpointnum2,numberbadpoints),displxtemp(badpointnum2,numberbadpoints),'+','MarkerEdgeColor','k','MarkerFaceColor','r')
    hold off;
    axis([min(min(XI))-10 max(max(XI))+10 min(min(YI))-10 max(max(YI))+10])
    drawnow;

    epsilon1D=(displxtemp(badpointnum,:)-displxtemp(badpointnum2,:))/(validxtemp(badpointnum,1)-validxtemp(badpointnum2,1));
    epsilonsize=size(epsilon1D);
    figure; plot(1:epsilonsize(1,2),epsilon1D,'.');
    title(['True strain versus image from two picked points']);
    xlabel('Image number [ ]');
    ylabel('True Strain [ ]');


    selection3 = menu(sprintf('Do you want to choose 2 other points?'),'Yes','No');

    if selection3==2
        selection40 = menu(sprintf('Do you want to save the data as a text file?'),'Yes','No');
        if selection40==2
            return
        end
        if selection40==1
            numimagtemp = [1:epsilonsize(1,2)]';
            alltemp = [numimagtemp epsilon1D'];
            [FileNameBase,PathNameBase] = uiputfile('','Save file with image# vs. 1Dstrain');
            cd(PathNameBase)
            save(FileNameBase,'alltemp','-ASCII');
            %             save image_1Dstrain.txt alltemp -ASCII
            return
        end
    end
    close(gcf)
end
%---------------------------------
% Measure elastic slope
% written by Chris
function [validx, validy,displx,disply] = strain_1D_average_func(validx, validy,displx,disply) ; % 1D strain calculation
videoselection = menu(sprintf('Do you want to create a video?'),'Yes','No');
if videoselection==1
    mkdir('videostrain')
    cd('videostrain');
    Vid='Vid';
end
selection50=1;
validx_fit=validx;
displx_fit=displx;
minminvalidx=min(min(validx));
maxmaxvalidx=max(max(validx));
minminvalidy=min(min(validy));
maxmaxvalidy=max(max(validy));
minmindisplx=min(min(displx));
maxmaxdisplx=max(max(displx));
h= figure
while selection50==1
    %     figure
    [pointnumber imagenumber]=size(displx);
    for i=1:imagenumber;
        plot(validx_fit(:,i),displx_fit(:,i),'o');
        xdata=validx_fit(:,i);
        ydata=displx_fit(:,i);
        if i==1
            x(1)=0
            x(2)=0
        end
        [x,resnormx,residual,exitflagx,output]  = lsqcurvefit(@linearfit, [x(1) x(2)], xdata, ydata);
        hold on;
        ydatafit=x(1)*xdata+x(2);
        plot(xdata,ydatafit,'r');
        
        hold off
        slope(i,:)=[i x(1)];
        axis([minminvalidx maxmaxvalidx minmindisplx maxmaxdisplx])
        xlabel('position [pixel]')
        ylabel('displacement [pixel]')
        title(['Displacement versus position',sprintf(' (Current image #: %1g)',i)]);
        drawnow
        if videoselection==1
            u=i+10000;
            ustr=num2str(u);
            videoname=[Vid ustr '.jpg']
            saveas(h,videoname,'jpg')
        end
    end
    g1 = figure, plot(slope(:,1),slope(:,2));
    hold on
    plot(slope(:,1),slope(:,2),'.');
        xlabel('Image [ ]')
        ylabel('True Strain [ ]')
        title(['True Strain vs. Image #']);

    selection40 = menu(sprintf('Do you want to save the data as file?'),'Yes','No');
    if selection40==2

    end
    if selection40==1
        alltemp = [slope(:,1) slope(:,2)];
        [FileNameBase,PathNameBase] = uiputfile('','Save file with image# vs. 1Dstrain');
        cd(PathNameBase)
        save(FileNameBase,'alltemp','-ASCII');
        %         save image_1Dstrain_avg.txt alltemp -ASCII
    end

    selection50 = menu(sprintf('Do you want to analyse a selected area again?'),'Yes','No');
    if selection50==2
        clear validx_fit
        clear displx_fit
        return
    end
    if selection50==1
        close(g1)
        plot(validx_fit(:,imagenumber),displx_fit(:,imagenumber),'o');
        title(['True strain versus image from all markers']);
        xlabel('Image number [ ]');
        ylabel('True Strain [ ]');
        prompt = {'Min. x-position:','Max. x-position:'};
        dlg_title = 'Regime to be analyzed in pixels';
        num_lines= 1;
        def     = {'800','1200'};
        answer = inputdlg(prompt,dlg_title,num_lines,def);
        minx= str2num(cell2mat(answer(1,1)));
        maxx= str2num(cell2mat(answer(2,1)));
        counter=0
        clear validx_fit
        clear displx_fit
        selectedmarkers=find(validx(:,imagenumber)>minx  & validx(:,imagenumber)<maxx);
        validx_fit=validx(selectedmarkers,:);
        displx_fit=displx(selectedmarkers,:);
        continue
    end

end


%---------------------------------
% 3D mesh plotting
% written by Chris
function [validx, validy,displx,disply]=meshplot(validx,validy,displx,disply);
h=figure;
sizevalidx=size(validx);
sizevalidy=size(validy);
looppoints=sizevalidx(1,1);
loopimages=sizevalidx(1,2);

videoselection = menu(sprintf('Do you want to create a video?'),'Yes','No');
if videoselection==1
    mkdir('video')
    cd('video');
    Vid='Vid';
end
gridsizex=10*round(min(min(validx))/10):10:10*round(max(max(validx))/10);
gridsizey=10*round(min(min(validy))/10):10:10*round(max(max(validy))/10);
[XI,YI]=meshgrid(gridsizex,gridsizey);
minminvalidx=min(min(validx));
maxmaxvalidx=max(max(validx));
minminvalidy=min(min(validy));
maxmaxvalidy=max(max(validy));
minmindisplx=min(min(displx));
maxmaxdisplx=max(max(displx));
minmindisply=min(min(disply));
maxmaxdisply=max(max(disply));
for i=1:(loopimages-1)
    ZI=griddata(validx(:,i),validy(:,i),displx(:,i),XI,YI,'cubic');
    mesh(XI,YI,ZI); hold on
    bottomplot=ones(size(validx))*minmindisplx;
    backyplaneplot=ones(size(validx))*maxmaxvalidy;
    backxplaneplot=ones(size(validx))*minminvalidx;
    plot3(validx(:,i),validy(:,i),displx(:,i),'.b');
    %         plot3(validx(:,i),backyplaneplot(:,i),displx(:,i),'.');
    plot3(backxplaneplot(:,i),validy(:,i),displx(:,i),'.g');
    xlabel('x-position [pixel]')
    ylabel('y-position [pixel]')
    zlabel('displacement [pixel]')
    %         plot3(validx(:,i),validy(:,i),bottomplot(:,i),'.');
    hold off
    title(['Displacement versus x-y-position',sprintf(' (Current image #: %1g)',i)]);
    axis([minminvalidx maxmaxvalidx minminvalidy maxmaxvalidy minmindisplx maxmaxdisplx])
    drawnow
    if videoselection==1
        u=i+10000;
        ustr=num2str(u);
        videoname=[Vid ustr '.jpg']
        saveas(h,videoname,'jpg')
    end
end

if videoselection==1
    cd('..')
end

%-------------------------------
% polyfit function
% written by Dan slightly changed by Chris
function [validx, validy,displx,disply]=polyfit3D(validx, validy,displx,disply);
close all
plot3dsurface_func(validx,validy,displx);

%---------------------------------------
% Just plot it
% written by Dan slightly changed by Chris
function plot3dsurface_func(validx,validy,displx,gridstyle,cropxx,cropyy)

sizevalidx=size(validx);
looppoints=sizevalidx(1,1);
loopimages=sizevalidx(1,2);
gridsizex=10*round(min(min(validx))/10):10:10*round(max(max(validx))/10);
gridsizey=10*round(min(min(validy))/10):10:10*round(max(max(validy))/10);
[XI,YI]=meshgrid(gridsizex,gridsizey);
ZI=griddata(validx(:,1),validy(:,1),displx(:,1),XI,YI,'cubic');
ZIsize=size(ZI);
displcolor = [-7 1];
straincolor = [-0.005 0.03];

maxminusminvalidx=(max(max(validx))-min(min(validx)));
maxminusminvalidy=(max(max(validx))-min(min(validy)));

for i=1:(loopimages-1)

    ZI=griddata(validx(:,i),validy(:,i),displx(:,i),XI,YI,'cubic');
    ZIsize=size(ZI);
    epsxx = gradient(ZI,(maxminusminvalidx/ZIsize(1,1)),(maxminusminvalidy/ZIsize(1,2)));

    subplot(2,1,1)
    pcolor(XI,YI,ZI)
    axis('equal')
    shading('interp')
    caxis(displcolor)
    h1 = colorbar;
    set(h1, 'PlotBoxAspectRatio',[2.0 10 8.0])
    set(h1, 'FontSize', 12);
    title(['Raw Displacement in x-direction',sprintf(' (Current image #: %1g)',i)]);

    subplot(2,1,2)
    pcolor(XI,YI,epsxx)
    axis('equal')
    shading('interp')
    caxis(straincolor)
    h1 = colorbar;
    set(h1, 'PlotBoxAspectRatio',[2.0 10 8.0])
    set(h1, 'FontSize', 12);
    title('Raw Strain in x-direction');

    drawnow

end

%--------------------------------------
% Delete some markers
% written by Chris
function [validx,validy] = removepoints_func2(validx,validy) ; %delete points

if exist('validx')==0
    [validx,Pathvalidx] = uigetfile('*.mat; *.txt','Open validx.mat or validx.txt');
    cd(Pathvalidx);
    validx=importdata(validx,'\t');
    [validy,Pathvalidy] = uigetfile('*.mat;*.txt','Open validy.mat or validy.txt');
    cd(Pathvalidy);
    validy=importdata(validy,'\t');
end


selectremove1 = menu(sprintf('Do you want to delete makers?'),'Yes','No');
if selectremove1==2

    return
end

% if yes
if selectremove1==1
    selectionremove2=selectremove1;
    %     figure
    counter=0
    sizevalidx=size(validx);
    looppoints=sizevalidx(1,1);
    loopimages=sizevalidx(1,2);
    defaultimage=loopimages
    numberbadpoints=0

    while selectionremove2==1
        counter=counter+1
        clear xplot
        clear sizevalidx
        clear selectremove11
        clear selection2
        %         clear badpoints

        sizevalidx=size(validx);
        looppoints=sizevalidx(1,1);
        loopimages=sizevalidx(1,2);

        % update temporary matrices
        %         displxtemp=displx;
        validxtemp=validx;
        validytemp=validy;
        %         resnormxtemp=resnormx;

        % get the image number from which the bad points will be chosen
        prompt = {'From which image do you want to delete markers?'};
        dlg_title = 'Marker removal';
        num_lines= 1;
        if numberbadpoints==0
            defaultimage=loopimages
        end
        if numberbadpoints~0
            defaultimage=numberbadpoints
        end
        def     = {num2str(defaultimage)};
        answer = inputdlg(prompt,dlg_title,num_lines,def);
        numberbadpoints = str2num(cell2mat(answer(1,1)));
        if numberbadpoints>loopimages
            numberbadpoints=loopimages
        end
        if numberbadpoints<1
            numberbadpoints=1
        end

        displx(:,1)=-validx(:,1)+validx(:,numberbadpoints);
        displx(:,1)=displx(:,1)-min(displx(:,1));

        gridsizex=10*round(min(min(validx))/10):10:10*round(max(max(validx))/10);
        gridsizey=10*round(min(min(validy))/10):10:10*round(max(max(validy))/10);
        [XI,YI]=meshgrid(gridsizex,gridsizey);
        ZI=griddata(validx(:,numberbadpoints),validy(:,numberbadpoints),displx(:,1),XI,YI,'cubic');
        epsxx = gradient(ZI,10,10);

        pcolor(XI,YI,epsxx);
        axis('equal')
        caxis([min(min(epsxx)) max(max(epsxx))])
        colorbar
        shading('interp')
        hold on
        plot3(validx(:,numberbadpoints),validy(:,numberbadpoints),displx(:,1),'.','MarkerEdgeColor','k','MarkerFaceColor','g'), hold off;
        axis([min(min(XI))-10 max(max(XI))+10 min(min(YI))-10 max(max(YI))+10])
        drawnow;

        validxtemp=validx;
        validytemp=validy;
        displxtemp=displx;
        validxdelete=validxtemp;
        validydelete=validytemp;
        displxdelete=displxtemp;

        title(sprintf('Define the region of interest.  \n  All points ouside that region will be deleted'))

        [xgrid,ygrid]=ginput(2);
        x(1,1) = xgrid(1);
        x(1,2) = xgrid(2);
        y(1,1) = ygrid(2);
        y(1,2) = ygrid(1);

        deletepoints=find(validxdelete(:,numberbadpoints)>min(x) & validxdelete(:,numberbadpoints)<max(x) & validydelete(:,numberbadpoints)<max(y) & validydelete(:,numberbadpoints)>min(y));
        [loopnum one]=size(deletepoints);

        validxdelete(deletepoints,:)=[];
        validydelete(deletepoints,:)=[];

        plot3(validxtemp(:,numberbadpoints),validytemp(:,numberbadpoints),displxtemp(:,1),'o','MarkerEdgeColor','k','MarkerFaceColor','g'), hold off;

        % update temporary data matrices; delete bad points
        displxtemp(deletepoints,:)=[];
        validxtemp(deletepoints,:)=[];
        validytemp(deletepoints,:)=[];

        % update the figure
        gridsizex=10*round(min(min(validxtemp))/10):10:10*round(max(max(validxtemp))/10);
        gridsizey=10*round(min(min(validytemp))/10):10:10*round(max(max(validytemp))/10);
        [XI,YI]=meshgrid(gridsizex,gridsizey);
        ZI=griddata(validxtemp(:,numberbadpoints),validytemp(:,numberbadpoints),displxtemp(:,1),XI,YI,'cubic');
        epsxx = gradient(ZI,10,10);
        pcolor(XI,YI,epsxx);
        axis('equal')
        caxis([min(min(epsxx)) max(max(epsxx))])
        colorbar
        shading('interp')
        hold on
        plot3(validxtemp(:,numberbadpoints),validytemp(:,numberbadpoints),displxtemp(:,1),'o','MarkerEdgeColor','k','MarkerFaceColor','g'), hold off;
        axis([min(min(XI))-10 max(max(XI))+10 min(min(YI))-10 max(max(YI))+10])
        drawnow;

        % delete point permanently?
        selectremove3 = menu(sprintf('Do you want to delete these markers permanently?'),'Yes','No');
        if selectremove3==1
            displx=displxtemp;
            validx=validxtemp;
            validy=validytemp;
        end
        if selectremove3==2
            displxtemp=displx;
            validxtemp=validx;
            validytemp=validy;
        end
        selectremove2 = menu(sprintf('Do you want to mark another bad point?'),'Yes','No');
        if selectremove2==2
            clear displx;
            validxfirst=zeros(size(validx));
            validxfirst=validx(:,1)*ones(1,sizevalidx(1,2));
            displx=validx-validxfirst;
            return
        end
    end
end