-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathBscanFFTpeak.cpp
2790 lines (2228 loc) · 78.8 KB
/
BscanFFTpeak.cpp
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
#ifdef _WIN64
#include "stdafx.h"
#include "windows.h"
// anything before a precompiled header is ignored,
// so no endif here! add #endif to compile on __unix__ !
#endif
#ifdef _WIN64
#include <qhyccd.h>
#endif
/*
* modified from BscanFFT.cpp
*
* Implementing line scan FFT
* for SD OCT
* with binning
* and inputs from ini file.
*
* Captures (background) spectrum on receipt of b key
* Captures pi shifted or J0 frame on receipt of p key
* Saves frames on receipt of s key
* Saves J0 null frame for subtraction on receipt of j key
* Clears the J0 thresholding mask on c key
*
* Do manual frame by frame averaging with ini file option
*
* Save individual frames on averaging if option chosen in ini file
*
* + (or =) key increases exposure time by 0.1 ms
* - (or _) key decreases exposure time by 0.1 ms
* u key increases exposure time by 1 ms
* d key decreases exposure time by 1 ms
* U key increases exposure time by 10 ms
* D key decreases exposure time by 10 ms
* A key toggles averaging
* Q key toggles clamping upper bound of displayed Bscan to 50 dB
* ] key increases thresholding in final Bscan
* [ key decreases thresholding in final Bscan
* 9 or ( key decreases the index of the reported ascan max value
* 0 or ) key increases the index of the reported ascan max value
*
* w decreases width of ROI for which avg val is reported, W increases width
* h decreases the height (position), H increases
* location of ROI is to the right of the index of reported ascan
* e toggles rEporting/plotting of intensity
*
**********************
* The following are for the vibration mode detection functionality:
* 1 to start peak-hold for 1st reading - without modulation
* 2 to start peak-hold for 2nd reading - with J0 null on ref arm
* 3 to start peak-hold for 3rd reading - with sample being vibrated (with or without ref biasing)
* 4 to start peak-hold for 4th reading (optional) - with biasing on ref arm
*
* R to select Peak Hold Region of Interest (toggle ROI display on and off)
* < for ROI (top-left) position left
* > for ROI (top-left) position right
* . for ROI (top-left) position up
* , for ROI (top-left) position down
* m for ROI width decrease
* M for ROI width increase
* / for ROI height decrease
* ? for ROI height increase
*
* V to toggle display of vibration profile
* Z to save vibration profile
*
* ESC, x or X key quits
*
*
*
* Hari Nandakumar
* 02 Apr 2019 *
*
*
*/
//#define _WIN64
//#define __unix__
#include <stdio.h>
#include <stdlib.h>
#ifdef __unix__
#include <unistd.h>
#include <libqhy/qhyccd.h>
#endif
#include <string.h>
#include <time.h>
#include <sys/stat.h>
// this is for mkdir
#include <opencv2/opencv.hpp>
// used the above include when imshow was being shown as not declared
// removing
// #include <opencv/cv.h>
// #include <opencv/highgui.h>
using namespace cv;
// have to make these global so that the onMouse can see them
int ROIposx = 0, ROIposy = 0, ROIw = 10, ROIh = 10;
Point P1, P2;
bool clicked;
Mat profileimg, profilearray, max1vals, max2vals, max3vals, max4vals;
void onMouse(int event, int x, int y, int f, void*)
{
// modified from https://stackoverflow.com/questions/22140880/drawing-rectangle-or-line-using-mouse-events-in-open-cv-using-python
switch (event)
{
case CV_EVENT_LBUTTONDOWN:
clicked = 1;
P1.x = x;
P1.y = y;
P2.x = x;
P2.y = y;
break;
case CV_EVENT_LBUTTONUP:
P2.x = x;
P2.y = y;
clicked = 0;
break;
case CV_EVENT_MOUSEMOVE:
if (clicked == 1) {
P2.x = x;
P2.y = y;
}
break;
default: break;
}
if (clicked == 1)
{
if (P1.x>P2.x)
{
ROIposx = P2.x;
ROIw = P1.x - P2.x;
}
else
{
ROIposx = P1.x;
ROIw = P2.x - P1.x;
}
if (P1.y>P2.y)
{
ROIposy = P2.y;
ROIh = P1.y - P2.y;
}
else
{
ROIposy = P1.y;
ROIh = P2.y - P1.y;
}
} // end if clicked
max1vals = Mat::zeros(Size(ROIw, 1), CV_64F);
max2vals = Mat::zeros(Size(ROIw, 1), CV_64F);
max3vals = Mat::zeros(Size(ROIw, 1), CV_64F);
max4vals = Mat::zeros(Size(ROIw, 1), CV_64F);
profilearray = Mat::zeros(Size(ROIw, 1), CV_64F);
}
inline void normalizerows(Mat& src, Mat& dst, double lowerlim, double upperlim)
{
// https://stackoverflow.com/questions/10673715/how-to-normalize-rows-of-an-opencv-mat-without-a-loop
// for syntax _OutputArray(B.ptr(i), B.cols))
for (uint ii = 0; ii<src.rows; ii++)
{
normalize(src.row(ii), dst.row(ii), lowerlim, upperlim, NORM_MINMAX);
}
}
inline void printAvgROI(Mat bscandb, uint ascanat, uint vertposROI, uint widthROI, Mat& ROIplot, uint& ROIploti, Mat& statusimg)
{
Mat AvgROI;
Scalar meanVal;
uint heightROI = 3;
char textbuffer[80];
Mat sixthrowofstatusimg = statusimg(Rect(0, 250, 600, 50)); // x,y,width,height
if (ascanat + widthROI<bscandb.cols)
{
bscandb(Rect(ascanat, vertposROI, widthROI, heightROI)).copyTo(AvgROI);
//imshow("ROI",AvgROI);
AvgROI.reshape(0, 1); // make it into a 1D array
meanVal = mean(AvgROI);
//printf("Mean of ROI at %d = %f dB\n", ascanat, meanVal(0));
sprintf(textbuffer, "Mean of ROI at %d = %f dB", ascanat, meanVal(0));
sixthrowofstatusimg = Mat::zeros(cv::Size(600, 50), CV_64F);
putText(statusimg, textbuffer, Point(0, 280), FONT_HERSHEY_SIMPLEX, 1, Scalar(255, 255, 255), 3, 1);
imshow("Status", statusimg);
// in ROIplot, we take the range to be 0 to 50 dB
// this is mapped to 0 to 300 vertical pixels
uint vertindex = uint(abs(6 * floor(meanVal(0))));
if (vertindex < 300)
vertindex = 300 - vertindex; // since Mat is shown 0th row on top with imshow
ROIplot.col(ROIploti) = Scalar::all(0);
for (int smalloopi = -2; smalloopi < 4; smalloopi++)
{
if ((vertindex + smalloopi) > 0)
if ((vertindex + smalloopi) < 300)
ROIplot.at<double>(vertindex + smalloopi, ROIploti) = 1;
}
imshow("ROI intensity", ROIplot);
if (ROIploti < 599)
ROIploti++;
else
ROIploti = 0;
}
else
sprintf(textbuffer, "ascanat+widthROI > width of image!");
sixthrowofstatusimg = Mat::zeros(cv::Size(600, 50), CV_64F);
putText(statusimg, textbuffer, Point(0, 280), FONT_HERSHEY_SIMPLEX, 1, Scalar(255, 255, 255), 3, 1);
imshow("Status", statusimg);
}
inline double besseldbinverse(double y)
{
// implements a lookup table to calculate the inverse of
// y = | 20*log10(besselj(0,x)) |
double x;
if (y > 30)
x = 2.38;
else if (y > 25)
x = 2.33;
else if (y > 21.65)
x = 2.27;
else if (y > 19.2)
x = 2.22;
else if (y > 17.18)
x = 2.17;
else if (y > 15.56)
x = 2.12;
else if (y > 14.19)
x = 2.07;
else if (y > 13)
x = 2.02;
else if (y > 11.94)
x = 1.97;
else if (y > 11)
x = 1.92;
else if (y > 10.15)
x = 1.87;
else if (y > 9.37)
x = 1.82;
else if (y > 8.66)
x = 1.77;
else if (y > 8)
x = 1.72;
else if (y > 7.4)
x = 1.67;
else if (y > 6.83)
x = 1.62;
else if (y > 6.30)
x = 1.57;
else if (y > 5.82)
x = 1.52;
else if (y > 5.36)
x = 1.47;
else if (y > 4.931)
x = 1.42;
else if (y > 4.528)
x = 1.37;
else if (y > 4.151)
x = 1.32;
else if (y > 3.797)
x = 1.27;
else if (y > 3.464)
x = 1.22;
else if (y > 3.151)
x = 1.17;
else if (y > 2.858)
x = 1.12;
else if (y > 2.583)
x = 1.07;
else if (y > 2.3245)
x = 1.02;
else if (y > 2.08286)
x = 0.97;
else if (y > 1.85689)
x = 0.92;
else if (y > 1.64601)
x = 0.87;
else if (y > 1.44964)
x = 0.82;
else if (y > 1.26729)
x = 0.77;
else if (y > 1.09850)
x = 0.72;
else if (y > 0.94288)
x = 0.67;
else if (y > 0.80006)
x = 0.62;
else if (y > 0.66972)
x = 0.57;
else if (y > 0.55159)
x = 0.52;
else if (y > 0.44542)
x = 0.47;
else if (y > 0.35097)
x = 0.42;
else if (y > 0.26807)
x = 0.37;
else if (y > 0.19654)
x = 0.32;
else if (y > 0.13625)
x = 0.27;
else if (y > 0.08708)
x = 0.22;
else if (y > 0.04893)
x = 0.17;
else if (y > 0.02173)
x = 0.12;
else if (y > 0.00543)
x = 0.07;
else x = 0.0;
return x;
}
inline double errnull(double y)
{
// implements a lookup table to calculate the error in
// getting the null value of besselj(0,x).
//
// y = | 20*log10(besselj(0,x)) |
//
// For example, y = -30 dB indicates an error of 0.05 in x, since
// besseldbinverse(30)=2.35, which is approx 0.05 away from
// the actual null at 2.405
double x, err;
x = besseldbinverse(y);
err = 2.405 - x;
return err;
}
inline void showVibProfile(Mat profilearray)
{
Mat profileflipped;
profileflipped = Mat::zeros(cv::Size(profilearray.cols, 4 * 160), CV_8U);
profileimg = Mat::zeros(cv::Size(profilearray.cols, 4 * 160), CV_8U);
// we assume the vibration values in nm to be between -160 and 160 nm
// and we multiply by 2 to make the line a bit thicker.
int vertindex;
for (int xindex = 0; xindex < profilearray.cols; xindex++)
{
// since we want the zero to be in the middle of the image,
// we add 160*2 to the vertindex
vertindex = int( 160*2 + (2 * round(profilearray.at<double>(0, xindex))));
for (int smalloopi = 0; smalloopi < 2; smalloopi++)
{
if ((vertindex + smalloopi) > 0)
if ((vertindex + smalloopi) < 4 * 160)
profileflipped.at<uchar>(vertindex + smalloopi, xindex) = 255;
}
}
// debug test module
// to plot y=x and check for straight line
//////////////////
/*
for (int xindex = 0; xindex < 150; xindex++)
{
vertindex = 2 * xindex;
for (int smalloopi = 0; smalloopi < 2; smalloopi++)
{
if ((vertindex + smalloopi) > 0)
if ((vertindex + smalloopi) < 4*160)
profileflipped.at<uchar>(vertindex + smalloopi, xindex) = 255;
}
}
///////////////////////
* */
flip(profileflipped, profileimg, 0); // to change origin from top left to bottom left.
imshow("Vibration profile - -160 to 160 nm", profileimg);
}
inline void printPeakHoldAscan(Mat bscandb, uint ascanat, int numdisplaypoints, Mat& statusimg, \
uint& peakholdframecount, uint peakholdnumframes, \
bool& num1keypressed, bool& num2keypressed, bool& num3keypressed, bool& num4keypressed, bool& bROI, \
double& max1val, double& max2val, double& max3val, double& max4val, float lambda0, bool displayvibrprofile)
{
if (num1keypressed || num2keypressed || num3keypressed || num4keypressed == 1)
{
if (ROIposx == 0 and ROIposy == 0) // meaning ROI has not been initialized
{
// instead of going through, just prompt user
// to select ROI
num1keypressed = 0;
num2keypressed = 0;
num3keypressed = 0;
num4keypressed = 0;
bROI = 1;
// so that the user selects an ROI
}
Mat ascan, ascandisp;
Mat bs, bsdisptemp, bsdisp, maxarray;
double minVal, maxVal;
char textbuffer[80];
double dispnm, dBdiff13, dBdiff12, errnm;
Mat dBdiff13vals;
double pi = 3.141592653589793;
Mat thirdrowofstatusimg = statusimg(Rect(0, 100, 600, 50));
bscandb.col(ascanat).copyTo(ascan);
bscandb.copyTo(bs);
// not considering the DC in pixels 0,1,2,3,4
ascandisp = ascan.rowRange(ROIposy, ROIposy + ROIh);
minMaxLoc(ascandisp, &minVal, &maxVal);
bsdisptemp = bs.colRange(ROIposx, ROIposx + ROIw);
bsdisp = bsdisptemp.rowRange(ROIposy, ROIposy + ROIh);
reduce(bsdisp, maxarray, 0, CV_REDUCE_MAX);
if (num1keypressed == 1)
{
if (peakholdframecount == 0) // beginning the hold
{
// display a confirmation message
sprintf(textbuffer, "PkHold%d 1 of Ascan%d begun.", peakholdnumframes, ascanat);
thirdrowofstatusimg = Mat::zeros(cv::Size(600, 50), CV_64F);
putText(statusimg, textbuffer, Point(0, 130), FONT_HERSHEY_SIMPLEX, 1, Scalar(255, 255, 255), 3, 1);
}
if (peakholdframecount < peakholdnumframes)
{
peakholdframecount++;
if (maxVal > max1val)
max1val = maxVal;
max1vals = max(maxarray, max1vals);
sprintf(textbuffer, "PkHold%d 1 of Ascan%d = %d fr", peakholdnumframes, ascanat, peakholdframecount);
thirdrowofstatusimg = Mat::zeros(cv::Size(600, 50), CV_64F);
putText(statusimg, textbuffer, Point(0, 130), FONT_HERSHEY_SIMPLEX, 1, Scalar(255, 255, 255), 3, 1);
}
else // the peak hold is finished, display
{
num1keypressed = 0;
peakholdframecount = 0;
sprintf(textbuffer, "PkHold%d 1 = %f dB", peakholdnumframes, max1val);
thirdrowofstatusimg = Mat::zeros(cv::Size(600, 50), CV_64F);
putText(statusimg, textbuffer, Point(0, 130), FONT_HERSHEY_SIMPLEX, 1, Scalar(255, 255, 255), 3, 1);
}
} // end if (num1keypressed == 1 )
if (num2keypressed == 1)
{
if (peakholdframecount == 0) // beginning the hold
{
// display a confirmation message
sprintf(textbuffer, "PkHold%d 2 of Ascan%d begun.", peakholdnumframes, ascanat);
thirdrowofstatusimg = Mat::zeros(cv::Size(600, 50), CV_64F);
putText(statusimg, textbuffer, Point(0, 130), FONT_HERSHEY_SIMPLEX, 1, Scalar(255, 255, 255), 3, 1);
}
if (peakholdframecount < peakholdnumframes)
{
peakholdframecount++;
if (maxVal > max2val)
max2val = maxVal;
max2vals = max(maxarray, max2vals);
sprintf(textbuffer, "PkHold%d 2 of Ascan%d = %d fr", peakholdnumframes, ascanat, peakholdframecount);
thirdrowofstatusimg = Mat::zeros(cv::Size(600, 50), CV_64F);
putText(statusimg, textbuffer, Point(0, 130), FONT_HERSHEY_SIMPLEX, 1, Scalar(255, 255, 255), 3, 1);
}
else // the peak hold is finished, display
{
num2keypressed = 0;
peakholdframecount = 0;
sprintf(textbuffer, "PkHold%d 2 = %f dB", peakholdnumframes, max2val);
thirdrowofstatusimg = Mat::zeros(cv::Size(600, 50), CV_64F);
putText(statusimg, textbuffer, Point(0, 130), FONT_HERSHEY_SIMPLEX, 1, Scalar(255, 255, 255), 3, 1);
}
} // end if (num2keypressed == 1 )
if (num3keypressed == 1)
{
if (peakholdframecount == 0) // beginning the hold
{
// display a confirmation message
sprintf(textbuffer, "PkHold%d 3 of Ascan%d begun.", peakholdnumframes, ascanat);
thirdrowofstatusimg = Mat::zeros(cv::Size(600, 50), CV_64F);
putText(statusimg, textbuffer, Point(0, 130), FONT_HERSHEY_SIMPLEX, 1, Scalar(255, 255, 255), 3, 1);
}
if (peakholdframecount < peakholdnumframes)
{
peakholdframecount++;
if (maxVal > max3val)
max3val = maxVal;
max3vals = max(maxarray, max3vals);
sprintf(textbuffer, "PkHold%d 3 of Ascan%d = %d fr", peakholdnumframes, ascanat, peakholdframecount);
thirdrowofstatusimg = Mat::zeros(cv::Size(600, 50), CV_64F);
putText(statusimg, textbuffer, Point(0, 130), FONT_HERSHEY_SIMPLEX, 1, Scalar(255, 255, 255), 3, 1);
}
else // the peak hold is finished, display
{
num3keypressed = 0;
peakholdframecount = 0;
sprintf(textbuffer, "PkHold%d 3 = %f dB", peakholdnumframes, max3val);
thirdrowofstatusimg = Mat::zeros(cv::Size(600, 50), CV_64F);
putText(statusimg, textbuffer, Point(0, 130), FONT_HERSHEY_SIMPLEX, 1, Scalar(255, 255, 255), 3, 1);
Mat fourthrowofstatusimg = statusimg(Rect(0, 150, 600, 50));
Mat fifthrowofstatusimg = statusimg(Rect(0, 200, 600, 50));
Mat sixthrowofstatusimg = statusimg(Rect(0, 250, 600, 50)); // x,y,width,height
dBdiff12 = max1val - max2val;
sprintf(textbuffer, "1/2 = %f dB", dBdiff12);
fourthrowofstatusimg = Mat::zeros(cv::Size(600, 50), CV_64F);
putText(statusimg, textbuffer, Point(0, 180), FONT_HERSHEY_SIMPLEX, 1, Scalar(255, 255, 255), 3, 1);
dBdiff13 = max1val - max3val;
dBdiff13vals = max1vals - max3vals;
sprintf(textbuffer, "1/3 = %f dB", dBdiff13);
fifthrowofstatusimg = Mat::zeros(cv::Size(600, 50), CV_64F);
putText(statusimg, textbuffer, Point(0, 230), FONT_HERSHEY_SIMPLEX, 1, Scalar(255, 255, 255), 3, 1);
// assuming the max3val to be due to vibration
// which is less than the first J0 null
// x = besseldbinverse(dBdiff13);
// where displacement A is given by
// x=2*k*A for wavenumber k
// taking k=2*pi/lambda0 for centre wavelength lambda0
// A = x*lambda0 / (4*pi)
dispnm = besseldbinverse(dBdiff13)*lambda0*1e9 / (4 * pi);
errnm = errnull(dBdiff12)*lambda0*1e9 / (4 * pi);
//////////////////////////
// loop to calculate displacement profiles
for (int indexv = 0; indexv<ROIw; indexv++)
{
profilearray.at<double>(0, indexv) = besseldbinverse(dBdiff13vals.at<double>(0, indexv))*lambda0*1e9 / (4 * pi);
}
/////////////////////////////
// debug
//std::cout<<"errnull="<<errnull(dBdiff12)<<std::endl;
sprintf(textbuffer, "disp = %3.2f +- %1.2f nm", dispnm, errnm);
sixthrowofstatusimg = Mat::zeros(cv::Size(600, 50), CV_64F);
putText(statusimg, textbuffer, Point(0, 280), FONT_HERSHEY_SIMPLEX, 1, Scalar(255, 255, 255), 3, 1);
if (displayvibrprofile == 1)
showVibProfile(profilearray);
}
} // end if (num3keypressed == 1 )
if (num4keypressed == 1)
{
Mat profilearray3, profilearray4;
profilearray.copyTo(profilearray3);
profilearray.copyTo(profilearray4); // just to initialize profilearray4
double dBdiff14, dBdiff43;
Mat dBdiff14vals;
if (peakholdframecount == 0) // beginning the hold
{
// display a confirmation message
sprintf(textbuffer, "PkHold%d 4 of Ascan%d begun.", peakholdnumframes, ascanat);
thirdrowofstatusimg = Mat::zeros(cv::Size(600, 50), CV_64F);
putText(statusimg, textbuffer, Point(0, 130), FONT_HERSHEY_SIMPLEX, 1, Scalar(255, 255, 255), 3, 1);
}
if (peakholdframecount < peakholdnumframes)
{
peakholdframecount++;
if (maxVal > max4val)
max4val = maxVal;
max4vals = max(maxarray, max4vals);
sprintf(textbuffer, "PkHold%d 4 of Ascan%d = %d fr", peakholdnumframes, ascanat, peakholdframecount);
thirdrowofstatusimg = Mat::zeros(cv::Size(600, 50), CV_64F);
putText(statusimg, textbuffer, Point(0, 130), FONT_HERSHEY_SIMPLEX, 1, Scalar(255, 255, 255), 3, 1);
}
else // the peak hold is finished, display
{
num4keypressed = 0;
peakholdframecount = 0;
sprintf(textbuffer, "PkHold%d 4 = %f dB", peakholdnumframes, max4val);
thirdrowofstatusimg = Mat::zeros(cv::Size(600, 50), CV_64F);
putText(statusimg, textbuffer, Point(0, 130), FONT_HERSHEY_SIMPLEX, 1, Scalar(255, 255, 255), 3, 1);
Mat fourthrowofstatusimg = statusimg(Rect(0, 150, 600, 50));
Mat fifthrowofstatusimg = statusimg(Rect(0, 200, 600, 50));
Mat sixthrowofstatusimg = statusimg(Rect(0, 250, 600, 50)); // x,y,width,height
dBdiff12 = max1val - max2val;
sprintf(textbuffer, "1/2 = %f dB", dBdiff12);
fourthrowofstatusimg = Mat::zeros(cv::Size(600, 50), CV_64F);
putText(statusimg, textbuffer, Point(0, 180), FONT_HERSHEY_SIMPLEX, 1, Scalar(255, 255, 255), 3, 1);
dBdiff14 = max1val - max4val;
dBdiff43 = max4val - max3val;
dBdiff14vals = max1vals - max4vals;
sprintf(textbuffer, "4/3 = %f dB", dBdiff43);
fifthrowofstatusimg = Mat::zeros(cv::Size(600, 50), CV_64F);
putText(statusimg, textbuffer, Point(0, 230), FONT_HERSHEY_SIMPLEX, 1, Scalar(255, 255, 255), 3, 1);
// assuming the max3val to be due to vibration
// which is less than the first J0 null
// x = besseldbinverse(dBdiff13);
// where displacement A is given by
// x=2*k*A for wavenumber k
// taking k=2*pi/lambda0 for centre wavelength lambda0
// A = x*lambda0 / (4*pi)
dispnm = besseldbinverse(dBdiff14)*lambda0*1e9 / (4 * pi);
//errnm = errnull(dBdiff12)*lambda0*1e9 / (4 * pi);
//////////////////////////
// loop to calculate displacement profiles
for (int indexv = 0; indexv<ROIw; indexv++)
{
profilearray4.at<double>(0, indexv) = besseldbinverse(dBdiff14vals.at<double>(0, indexv))*lambda0*1e9 / (4 * pi);
}
/////////////////////////////
// profile array 3 - profile array 4 gives the displacement
// with bias as in the profilearray4 reading
profilearray = profilearray3 - profilearray4;
sprintf(textbuffer, "disp = %3.2f +- %1.2f nm", dispnm, errnm);
sixthrowofstatusimg = Mat::zeros(cv::Size(600, 50), CV_64F);
putText(statusimg, textbuffer, Point(0, 280), FONT_HERSHEY_SIMPLEX, 1, Scalar(255, 255, 255), 3, 1);
if (displayvibrprofile == 1)
showVibProfile(profilearray);
}
} // end if (num4keypressed == 1 )
} // end if (num1keypressed || num2keypressed || num3keypressed == 1)
}
inline void printMinMaxAscan(Mat bscandb, uint ascanat, int numdisplaypoints, Mat& statusimg)
{
Mat ascan, ascandisp;
double minVal, maxVal;
char textbuffer[80];
Mat thirdrowofstatusimg = statusimg(Rect(0, 100, 600, 50));
Mat fourthrowofstatusimg = statusimg(Rect(0, 150, 600, 50));
Mat fifthrowofstatusimg = statusimg(Rect(0, 200, 600, 50));
bscandb.col(ascanat).copyTo(ascan);
ascan.row(5).copyTo(ascan.row(1)); // masking out the DC in the display
ascan.row(5).copyTo(ascan.row(0));
ascan.row(5).copyTo(ascan.row(2));
ascan.row(5).copyTo(ascan.row(3));
ascan.row(5).copyTo(ascan.row(4));
ascandisp = ascan.rowRange(0, numdisplaypoints);
//debug
//normalize(ascan, ascandebug, 0, 1, NORM_MINMAX);
//imshow("debug", ascandebug);
minMaxLoc(ascandisp, &minVal, &maxVal);
sprintf(textbuffer, "Max of Ascan%d = %f dB", ascanat, maxVal);
fourthrowofstatusimg = Mat::zeros(cv::Size(600, 50), CV_64F);
putText(statusimg, textbuffer, Point(0, 180), FONT_HERSHEY_SIMPLEX, 1, Scalar(255, 255, 255), 3, 1);
sprintf(textbuffer, "Min of Ascan%d = %f dB", ascanat, minVal);
fifthrowofstatusimg = Mat::zeros(cv::Size(600, 50), CV_64F);
putText(statusimg, textbuffer, Point(0, 230), FONT_HERSHEY_SIMPLEX, 1, Scalar(255, 255, 255), 3, 1);
imshow("Status", statusimg);
}
inline void makeonlypositive(Mat& src, Mat& dst)
{
// from https://stackoverflow.com/questions/48313249/opencv-convert-all-negative-values-to-zero
max(src, 0, dst);
}
inline Mat zeropadrowwise(Mat sm, int sn)
{
// increase fft points sn times
// newnumcols = numcols*sn;
// by fft, zero padding, and then inv fft
// returns CV_64F
// guided by https://stackoverflow.com/questions/10269456/inverse-fourier-transformation-in-opencv
// inspired by Drexler & Fujimoto 2nd ed Section 5.1.10
// needs fftshift implementation for the zero pad to work correctly if done on borders.
// or else adding zeros directly to the higher frequencies.
// freqcomplex=fftshift(fft(signal));
// zp2=4*ifft(ifftshift(zpfreqcomplex));
// result of this way of zero padding in the fourier domain is to resample the same min / max range
// at a higher sampling rate in the initial domain.
// So this improves the k linear interpolation.
Mat origimage;
Mat fouriertransform, fouriertransformzp;
Mat inversefouriertransform;
int numrows = sm.rows;
int numcols = sm.cols;
int newnumcols = numcols * sn;
sm.convertTo(origimage, CV_32F);
dft(origimage, fouriertransform, DFT_SCALE | DFT_COMPLEX_OUTPUT | DFT_ROWS);
// implementing fftshift row-wise
// like https://docs.opencv.org/2.4/doc/tutorials/core/discrete_fourier_transform/discrete_fourier_transform.html
int cx = fouriertransform.cols / 2;
// here we assume fouriertransform.cols is even
Mat LHS(fouriertransform, Rect(0, 0, cx, fouriertransform.rows)); // Create a ROI per half
Mat RHS(fouriertransform, Rect(cx, 0, cx, fouriertransform.rows)); // Rect(topleftx, toplefty, w, h),
// OpenCV typically assumes that the top and left boundary of the rectangle are inclusive, while the right and bottom boundaries are not.
// https://docs.opencv.org/3.2.0/d2/d44/classcv_1_1Rect__.html
Mat tmp; // swap LHS & RHS
LHS.copyTo(tmp);
RHS.copyTo(LHS);
tmp.copyTo(RHS);
copyMakeBorder(fouriertransform, fouriertransformzp, 0, 0, floor((newnumcols - numcols) / 2), floor((newnumcols - numcols) / 2), BORDER_CONSTANT, 0.0);
// this does the zero pad - copyMakeBorder(src, dest, top, bottom, left, right, borderType, value)
// Now we do the ifftshift before ifft
cx = fouriertransformzp.cols / 2;
Mat LHSzp(fouriertransformzp, Rect(0, 0, cx, fouriertransformzp.rows)); // Create a ROI per half
Mat RHSzp(fouriertransformzp, Rect(cx, 0, cx, fouriertransformzp.rows)); // Rect(topleftx, toplefty, w, h)
LHSzp.copyTo(tmp);
RHSzp.copyTo(LHSzp);
tmp.copyTo(RHSzp);
dft(fouriertransformzp, inversefouriertransform, DFT_INVERSE | DFT_REAL_OUTPUT | DFT_ROWS);
inversefouriertransform.convertTo(inversefouriertransform, CV_64F);
return inversefouriertransform;
}
inline Mat smoothmovavg(Mat sm, int sn)
{
// smooths each row of Mat m using 2n+1 point weighted moving average
// x(p) = ( x(p-n) + x(p-n+1) + .. + 2*x(p) + x(p+1) + ... + x(p+n) ) / 2*(n+1)
// The window size is truncated at the edges.
// can see https://docs.opencv.org/2.4/doc/tutorials/core/how_to_scan_images/how_to_scan_images.html#howtoscanimagesopencv
// for efficient ways
// accept only double type matrices
// sm needs to be CV_64FC1
CV_Assert(sm.depth() == CV_64F);
Mat sresult;
sm.copyTo(sresult); // initializing size of result
int smaxcols = sm.cols;
int smaxrows = sm.rows;
double ssum;
int sindexi;
double* srcptr;
double* destptr;
for (int si = 0; si < smaxrows; si++)
{
srcptr = sm.ptr<double>(si);
destptr = sresult.ptr<double>(si);
for (int sj = 0; sj < smaxcols; sj++)
{
ssum = 0;
for (int sk = -sn; sk < (sn + 1); sk++)
{
// address as m.at<double>(y, x); ie (row,column)
sindexi = sj + sk;
if ((sindexi > -1) && (sindexi < smaxcols)) // truncate window
ssum = ssum + srcptr[sindexi]; //equivalent to ssum = ssum + sm.at<double>(si,sindexi);
else
ssum = ssum + srcptr[sj]; // when window is truncated,
// weight of original point increases
}
// we want to add m.at<double>(i,j) once again, since its weight is 2
ssum = ssum + srcptr[sj];
destptr[sj] = ssum / 2 / (sn + 1); //equivalent to sresult.at<double>(si,sj) = ssum / (2 * (sn+1) );
}
}
return sresult;
}
inline void savematasimage(char* p, char* d, char* f, Mat m)
{
// saves a Mat m using imwrite as filename f appending .png, both windows and unix versions
// p=pathname, d=dirname, f=filename
#ifdef __unix__
strcpy(p, d);
strcat(p, "/");
strcat(p, f);
strcat(p, ".png");
imwrite(p, m);
#else
strcpy(p, d);
strcat(p, "\\"); // imwrite needs path with \\ separators, not /, on windows
strcat(p, f);
strcat(p, ".png");
imwrite(p, m);
#endif
}
// the next function saves a Mat m as variablename f by dumping to outfile o, both windows and unix versions
#ifdef __unix__
inline void savematasdata(std::ofstream& o, char* f, Mat m)
{
// saves a Mat m as variable named f in Matlab m file format
o << f << "=";
o << m;
o << ";" << std::endl;
}
#else
inline void savematasdata(cv::FileStorage& o, char* f, Mat m)
{
// saves Mat m by serializing to xml as variable named f
o << f << m;
}
#endif
int main(int argc, char *argv[])
{
int num = 0;
qhyccd_handle *camhandle = NULL;
int ret;
char id[32];
//char camtype[16];
int found = 0;
unsigned int w, h, bpp = 8, channels, cambitdepth = 16;
//unsigned int offsetx = 0, offsety = 0;
unsigned int indexi, manualindexi, averages = 1, opw, oph;
unsigned int vibrindexi=0;
uint indextemp;
//uint indextempl;
uint ascanat = 20;
uint averagestoggle = 1;
int camtime = 1, camgain = 1, camspeed = 1, cambinx = 2, cambiny = 2, usbtraffic = 10;
int camgamma = 1, binvalue = 1, normfactor = 1, normfactorforsave = 25;
int numfftpoints = 1024;
int numdisplaypoints = 512;
bool saveframes = 0;
bool manualaveraging = 0, saveinterferograms = 0;
unsigned int manualaverages = 1;
int movavgn = 0;
bool clampupper = 0;
bool ROIreport = 0;
bool doneflag = 0, skeypressed = 0, bkeypressed = 0, pkeypressed = 0;
bool bROI = 0, displayvibrprofile = 1;
Mat ROIoverlay, output;
bool jlockin = 0, jkeypressed = 0, ckeypressed = 0;
bool num1keypressed = 0, num2keypressed = 0, num3keypressed = 0, num4keypressed = 0;
uint peakholdframecount = 0;
Mat jmask, jmaskt;
double lambdamin, lambdamax;
lambdamin = 816e-9;
lambdamax = 884e-9;
int mediann = 5;
uint increasefftpointsmultiplier = 1;
double bscanthreshold = -30.0;
bool rowwisenormalize = 0;
bool donotnormalize = 1;
uint peakholdnumframes = 100;
double max1val = 0.0, max2val = 0.0, max3val = 0.0, max4val = 0.0;
w = 640;
h = 480;
int fps, key;
int t_start, t_end;