-
Notifications
You must be signed in to change notification settings - Fork 6
/
BscanDark.cpp
1980 lines (1581 loc) · 53.8 KB
/
BscanDark.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
/*
* forked from BscanFFT.cpp
* to implement dark subtraction.
*
* 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 thresholding 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
* ] 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
* O or o key to capture a dark frame
* R or r key to capture reference arm only frame
* T or t key to capture sample arm only frame
* ESC, x or X key quits
*
*
* Hari Nandakumar
* 29 Dec 2018 *
*
*
*/
//#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;
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 printMinMaxAscan(Mat bscandb, uint ascanat, int numdisplaypoints)
{
Mat ascan, ascandisp;
double minVal, maxVal;
bscandb.col(ascanat).copyTo(ascan);
ascan.row(4).copyTo(ascan.row(1)); // masking out the DC in the display
ascan.row(4).copyTo(ascan.row(0));
ascan.row(4).copyTo(ascan.row(2));
ascan.row(4).copyTo(ascan.row(3));
ascandisp = ascan.rowRange(0, numdisplaypoints);
//debug
//normalize(ascan, ascandebug, 0, 1, NORM_MINMAX);
//imshow("debug", ascandebug);
minMaxLoc(ascandisp, &minVal, &maxVal);
printf("Max of Ascan at %d = %f dB\n", ascanat, maxVal);
printf("Min of Ascan at %d = %f dB\n", ascanat, minVal);
}
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 void lpfilter(Mat& sm)
{
// similar to the filter in zeropadrowwise
Mat origimage;
Mat fouriertransform, inversefouriertransform;
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);
// blank out 80% of the points
// first 40% and last 40% for low pass
// since DC is now at the centre
int dcl = fouriertransform.cols/2 - floor(fouriertransform.cols/10);
int dcr = fouriertransform.cols/2 + floor(fouriertransform.cols/10);
Mat LHSblank(fouriertransform, Rect(0, 0, dcl, fouriertransform.rows)); // Create a ROI per half
Mat RHSblank(fouriertransform, Rect(dcr, 0, dcl, fouriertransform.rows)); // Rect(topleftx, toplefty, w, h)
LHSblank=Scalar::all(0);
RHSblank=Scalar::all(0);
// Now we do the ifftshift before ifft
cx = fouriertransform.cols/2;
Mat LHSzp(fouriertransform, Rect(0, 0, cx, fouriertransform.rows)); // Create a ROI per half
Mat RHSzp(fouriertransform, Rect(cx, 0, cx, fouriertransform.rows)); // Rect(topleftx, toplefty, w, h)
LHSzp.copyTo(tmp);
RHSzp.copyTo(LHSzp);
tmp.copyTo(RHSzp);
dft(fouriertransform, inversefouriertransform, DFT_INVERSE|DFT_REAL_OUTPUT|DFT_ROWS);
inversefouriertransform.convertTo(inversefouriertransform, CV_64F);
inversefouriertransform.copyTo(sm);
}
inline Mat zeropadrowwise(Mat sm, int sn, bool bandpassfilter)
{
// 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);
if (bandpassfilter)
{
// blank out 80% of the points
// first 40% and last 40% for low pass
// since DC is now at the centre
int dcl = fouriertransform.cols/2 - floor(fouriertransform.cols/10);
int dcr = fouriertransform.cols/2 + floor(fouriertransform.cols/10);
Mat LHSblank(fouriertransform, Rect(0, 0, dcl, fouriertransform.rows)); // Create a ROI per half
Mat RHSblank(fouriertransform, Rect(dcr, 0, dcl, fouriertransform.rows)); // Rect(topleftx, toplefty, w, h)
LHSblank=Scalar::all(0);
RHSblank=Scalar::all(0);
// also blank out the DC, so that it becomes a bandpass
int dcvals = 3;
dcl = fouriertransform.cols/2 - dcvals;
Mat DCblank(fouriertransform, Rect(dcl, 0, dcvals*2, fouriertransform.rows));
DCblank=Scalar::all(0);
}
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, numofframes = 100;
unsigned int numofm1slices = 10, numofm2slices = 10, firstaccum, secondaccum;
unsigned int offsetx = 0, offsety = 0;
unsigned int indexi, manualindexi, averages = 1, opw, oph;
uint indextemp, 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 doneflag = 0, skeypressed = 0, bkeypressed = 0, pkeypressed = 0;
bool rkeypressed = 0, tkeypressed = 0, darkkeypressed = 0;
bool jthresholding = 0, jkeypressed = 0, ckeypressed = 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;
bool bandpassfilter = 0;
bool lowpassfilter = 0;
w = 640;
h = 480;
int fps, key, bscanat;
int t_start, t_end;
std::ifstream infile("BscanDark.ini");
std::string tempstring;
char dirdescr[60];
sprintf(dirdescr, "_");
//namedWindow("linearized",0); // 0 = WINDOW_NORMAL
//moveWindow("linearized", 20, 500);
//namedWindow("Bscanl",0); // 0 = WINDOW_NORMAL
//moveWindow("Bscanl", 400, 0);
char dirname[80];
char filename[20];
char filenamec[20];
char pathname[140];
char lambdamaxstr[40];
char lambdaminstr[40];
struct tm *timenow;
time_t now = time(NULL);
// inputs from ini file
if (infile.is_open())
{
infile >> tempstring;
infile >> tempstring;
infile >> tempstring;
// first three lines of ini file are comments
infile >> camgain;
infile >> tempstring;
infile >> camtime;
infile >> tempstring;
infile >> bpp;
infile >> tempstring;
infile >> w;
infile >> tempstring;
infile >> h;
infile >> tempstring;
infile >> camspeed;
infile >> tempstring;
infile >> cambinx;
infile >> tempstring;
infile >> cambiny;
infile >> tempstring;
infile >> usbtraffic;
infile >> tempstring;
infile >> binvalue;
infile >> tempstring;
infile >> dirdescr;
infile >> tempstring;
infile >> averages;
infile >> tempstring;
infile >> numfftpoints;
infile >> tempstring;
infile >> saveframes;
infile >> tempstring;
infile >> manualaveraging;
infile >> tempstring;
infile >> manualaverages;
infile >> tempstring;
infile >> saveinterferograms;
infile >> tempstring;
infile >> movavgn;
infile >> tempstring;
infile >> numdisplaypoints;
infile >> tempstring;
infile >> lambdaminstr;
infile >> tempstring;
infile >> lambdamaxstr;
infile >> tempstring;
infile >> mediann;
infile >> tempstring;
infile >> increasefftpointsmultiplier;
infile >> tempstring;
infile >> rowwisenormalize;
infile >> tempstring;
infile >> donotnormalize;
infile >> tempstring;
infile >> bandpassfilter;
infile >> tempstring;
infile >> lowpassfilter;
infile.close();
lambdamin = atof(lambdaminstr);
lambdamax = atof(lambdamaxstr);
averagestoggle = averages;
}
else std::cout << "Unable to open ini file, using defaults.";
namedWindow("show", 0); // 0 = WINDOW_NORMAL
moveWindow("show", 20, 0);
namedWindow("Bscan", 0); // 0 = WINDOW_NORMAL
moveWindow("Bscan", 800, 0);
// debug
/*
char debugwinname[80];
namedWindow("debug1", 0); // 0 = WINDOW_NORMAL
moveWindow("debug1", 100, 600);
namedWindow("debug2", 0); // 0 = WINDOW_NORMAL
moveWindow("debug2", 200, 600);
namedWindow("debug3", 0); // 0 = WINDOW_NORMAL
moveWindow("debug3", 300, 600);
namedWindow("debug4", 0); // 0 = WINDOW_NORMAL
moveWindow("debug4", 400, 600);
namedWindow("debug5", 0); // 0 = WINDOW_NORMAL
moveWindow("debug5", 500, 600);
namedWindow("debug6", 0); // 0 = WINDOW_NORMAL
moveWindow("debug6", 600, 600);
namedWindow("debug7", 0); // 0 = WINDOW_NORMAL
moveWindow("debug7", 700, 600);
namedWindow("debug8", 0); // 0 = WINDOW_NORMAL
moveWindow("debug8", 800, 600);
namedWindow("debug9", 0); // 0 = WINDOW_NORMAL
moveWindow("debug9", 900, 600);
namedWindow("debug0", 0); // 0 = WINDOW_NORMAL
moveWindow("debug0", 0, 600);
* */
if (manualaveraging)
{
namedWindow("Bscanm", 0); // 0 = WINDOW_NORMAL
moveWindow("Bscanm", 800, 400);
}
/////////////////////////////////////
// init camera, variables, etc
cambitdepth = bpp;
opw = w / binvalue;
oph = h / binvalue;
float lambda0 = (lambdamin + lambdamax) / 2;
float lambdabw = lambdamax - lambdamin;
Mat ROI;
Mat plot_result;
Mat plot_result2;
Mat data_y(oph, opw, CV_64F); // the Mat constructor Mat(rows,columns,type)
Mat data_ylin(oph, numfftpoints, CV_64F);
Mat data_yb(oph, opw, CV_64F);
Mat data_yp(oph, opw, CV_64F);
Mat data_yd(oph, opw, CV_64F); // dark, reference and sample
Mat data_yr(oph, opw, CV_64F);
Mat data_ys(oph, opw, CV_64F);
Mat padded, paddedn;
Mat barthannwin(1, opw, CV_64F); // the Mat constructor Mat(rows,columns,type);
Mat baccum, manualaccum;
uint baccumcount, manualaccumcount;
// initialize data_yb with zeros
data_yb = Mat::zeros(Size(opw, oph), CV_64F); //Size(cols,rows)
data_yp = Mat::zeros(Size(opw, oph), CV_64F);
baccum = Mat::zeros(Size(opw, oph), CV_64F);
baccumcount = 0;
manualaccumcount = 0;
Mat bscansave0[100]; // allocate buffer to save frames, max 100
Mat bscansave1[100]; // one buffer is active while other is saved on skeypressed
Mat jscansave; // to save j frames
Mat bscanmanualsave0[100];
Mat bscanmanualsave1[100];
Mat interferogramsave0[100];
Mat interferogramsave1[100];
Mat interferogrambsave0[100];
Mat interferogrambsave1[100];
bool zeroisactive = 1;
int nr, nc;
Mat m, opm, opmvector, bscan, bscanlog, bscandb, bscandisp, bscandispmanual, bscantemp, bscantemp2, bscantemp3, bscantransposed, chan[3];
Mat tempmat;
Mat bscandispj;
Mat mraw;
//Mat bscanl, bscantempl, bscantransposedl;
Mat magI, cmagI, cmagImanual;
//Mat magIl, cmagIl;
double minbscan, maxbscan;
//double minbscanl, maxbscanl;
Scalar meanval;
Mat lambdas, k, klinear;
Mat diffk, slopes, fractionalk, nearestkindex;
double kmin, kmax;
double pi = 3.141592653589793;
double minVal, maxVal, pixVal;
Mat ascan;
//minMaxLoc( m, &minVal, &maxVal, &minLoc, &maxLoc );
double deltalambda = (lambdamax - lambdamin) / data_y.cols;
klinear = Mat::zeros(cv::Size(1, numfftpoints), CV_64F);
fractionalk = Mat::zeros(cv::Size(1, numfftpoints), CV_64F);
nearestkindex = Mat::zeros(cv::Size(1, numfftpoints), CV_32S);
if (increasefftpointsmultiplier > 1)
{
lambdas = Mat::zeros(cv::Size(1, increasefftpointsmultiplier*data_y.cols), CV_64F); //Size(cols,rows)
diffk = Mat::zeros(cv::Size(1, increasefftpointsmultiplier*data_y.cols), CV_64F);
slopes = Mat::zeros(cv::Size(data_y.rows, increasefftpointsmultiplier*data_y.cols), CV_64F);
}
else
{
lambdas = Mat::zeros(cv::Size(1, data_y.cols), CV_64F); //Size(cols,rows)
diffk = Mat::zeros(cv::Size(1, data_y.cols), CV_64F);
slopes = Mat::zeros(cv::Size(data_y.rows, data_y.cols), CV_64F);
}
resizeWindow("Bscan", oph, numdisplaypoints); // (width,height)
for (indextemp = 0; indextemp<(increasefftpointsmultiplier*data_y.cols); indextemp++)
{
// lambdas = linspace(830e-9, 870e-9 - deltalambda, data_y.cols)
lambdas.at<double>(0, indextemp) = lambdamin + indextemp * deltalambda / increasefftpointsmultiplier;
}
k = 2 * pi / lambdas;
kmin = 2 * pi / (lambdamax - deltalambda);
kmax = 2 * pi / lambdamin;
double deltak = (kmax - kmin) / numfftpoints;
for (indextemp = 0; indextemp<(numfftpoints); indextemp++)
{
// klinear = linspace(kmin, kmax, numfftpoints)
klinear.at<double>(0, indextemp) = kmin + (indextemp + 1)*deltak;
}
//for (indextemp=0; indextemp<(data_y.cols); indextemp++)
//{
//printf("k=%f, klin=%f\n", k.at<double>(0,indextemp), klinear.at<double>(0,indextemp));
//}
for (indextemp = 1; indextemp<(increasefftpointsmultiplier*data_y.cols); indextemp++)
{
//find the diff of the non-linear ks
// since this is a decreasing series, RHS is (i-1) - (i)
diffk.at<double>(0, indextemp) = k.at<double>(0, indextemp - 1) - k.at<double>(0, indextemp);
//printf("i=%d, diffk=%f \n", indextemp, diffk.at<double>(0,indextemp));
}
// and initializing the first point separately
diffk.at<double>(0, 0) = diffk.at<double>(0, 1);
for (int f = 0; f < numfftpoints; f++)
{
// find the index of the nearest k value, less than the linear k
for (indextemp = 0; indextemp < increasefftpointsmultiplier*data_y.cols; indextemp++)
{
//printf("Before if k=%f,klin=%f \n",k.at<double>(0,indextemp),klinear.at<double>(0,f));
if (k.at<double>(0, indextemp) < klinear.at<double>(0, f))
{
nearestkindex.at<int>(0, f) = indextemp;
//printf("After if k=%f,klin=%f,nearestkindex=%d\n",k.at<double>(0,indextemp),klinear.at<double>(0,f),nearestkindex.at<int>(0,f));
break;
} // end if
} //end indextemp loop
} // end f loop
for (int f = 0; f < numfftpoints; f++)
{
// now find the fractional amount by which the linearized k value is greater than the next lowest k
fractionalk.at<double>(0, f) = (klinear.at<double>(0, f) - k.at<double>(0, nearestkindex.at<int>(0, f))) / diffk.at<double>(0, nearestkindex.at<int>(0, f));
//printf("f=%d, klinear=%f, diffk=%f, k=%f, nearesti=%d\n",f, klinear.at<double>(0,f), diffk.at<double>(0,nearestkindex.at<int>(0,f)), k.at<double>(0,nearestkindex.at<int>(0,f)),nearestkindex.at<int>(0,f) );
//printf("f=%d, fractionalk=%f\n",f, fractionalk.at<double>(0,f));
}
timenow = localtime(&now);
strftime(dirname, sizeof(dirname), "%Y-%m-%d_%H_%M_%S-", timenow);
strcat(dirname, dirdescr);
#ifdef _WIN64
CreateDirectoryA(dirname, NULL);
cv::FileStorage outfile;
sprintf(filename, "BscanFFT.xml");
strcpy(pathname, dirname);
strcat(pathname, "\\");
strcat(pathname, filename);
outfile.open(pathname, cv::FileStorage::WRITE);
#else
mkdir(dirname, 0755);
#endif
#ifdef __unix__
sprintf(filename, "BscanFFT.m");
strcpy(pathname, dirname);
strcat(pathname, "/");
strcat(pathname, filename);
std::ofstream outfile(pathname);
#endif
ret = InitQHYCCDResource();
if (ret != QHYCCD_SUCCESS)
{
printf("Init SDK not successful!\n");
}
num = ScanQHYCCD();
if (num > 0)
{
printf("Found QHYCCD,the num is %d \n", num);
}
else
{
printf("QHYCCD camera not found, please check the usb cable.\n");
goto failure;
}
for (int i = 0; i < num; i++)
{
ret = GetQHYCCDId(i, id);
if (ret == QHYCCD_SUCCESS)
{
//printf("connected to the first camera from the list,id is %s\n",id);
found = 1;
break;
}
}
if (found != 1)
{
printf("The camera is not QHYCCD or other error \n");
goto failure;
}
if (found == 1)
{
camhandle = OpenQHYCCD(id);
if (camhandle != NULL)
{
//printf("Open QHYCCD success!\n");
}
else
{
printf("Open QHYCCD failed \n");
goto failure;
}
ret = SetQHYCCDStreamMode(camhandle, 1);
ret = InitQHYCCD(camhandle);
if (ret == QHYCCD_SUCCESS)
{
//printf("Init QHYCCD success!\n");
}
else
{
printf("Init QHYCCD fail code:%d\n", ret);
goto failure;
}
ret = IsQHYCCDControlAvailable(camhandle, CONTROL_TRANSFERBIT);
if (ret == QHYCCD_SUCCESS)
{
ret = SetQHYCCDBitsMode(camhandle, cambitdepth);
if (ret != QHYCCD_SUCCESS)
{
printf("SetQHYCCDBitsMode failed\n");
getchar();
return 1;
}
}
ret = SetQHYCCDResolution(camhandle, 0, 0, w, h); //handle, xpos,ypos,xwidth,ywidth
if (ret == QHYCCD_SUCCESS)
{
printf("Resolution set - width = %d height = %d\n", w, h);
}
else
{
printf("SetQHYCCDResolution fail\n");
goto failure;
}
ret = SetQHYCCDParam(camhandle, CONTROL_USBTRAFFIC, usbtraffic); //handle, parameter name, usbtraffic (which can be 0..100 perhaps)
if (ret == QHYCCD_SUCCESS)
{
//printf("CONTROL_USBTRAFFIC success!\n");
}
else
{
printf("CONTROL_USBTRAFFIC fail\n");
goto failure;
}
ret = SetQHYCCDParam(camhandle, CONTROL_SPEED, camspeed); //handle, parameter name, speed (which can be 0,1,2)
if (ret == QHYCCD_SUCCESS)
{
//printf("CONTROL_CONTROL_SPEED success!\n");
}
else
{
printf("CONTROL_CONTROL_SPEED fail\n");
goto failure;
}
ret = SetQHYCCDParam(camhandle, CONTROL_EXPOSURE, camtime); //handle, parameter name, exposure time (which is in us)
if (ret == QHYCCD_SUCCESS)
{
//printf("CONTROL_EXPOSURE success!\n");
}
else
{
printf("CONTROL_EXPOSURE fail\n");
goto failure;
}
ret = SetQHYCCDParam(camhandle, CONTROL_GAIN, camgain); //handle, parameter name, gain (which can be 0..99)
if (ret == QHYCCD_SUCCESS)
{
//printf("CONTROL_GAIN success!\n");
}
else
{
printf("CONTROL_GAIN fail\n");
goto failure;
}
ret = SetQHYCCDParam(camhandle, CONTROL_GAMMA, camgamma); //handle, parameter name, gamma (which can be 0..2 perhaps)
if (ret == QHYCCD_SUCCESS)
{
//printf("CONTROL_GAMMA success!\n");
}
else
{
printf("CONTROL_GAMMA fail\n");
goto failure;
}
if (cambitdepth == 8)
{
m = Mat::zeros(cv::Size(w, h), CV_8U);
mraw = Mat::zeros(cv::Size(w, h), CV_8U);
}
else // is 16 bit
{
m = Mat::zeros(cv::Size(w, h), CV_16U);
mraw = Mat::zeros(cv::Size(w, h), CV_16U);
}
ret = BeginQHYCCDLive(camhandle);
if (ret == QHYCCD_SUCCESS)
{
printf("BeginQHYCCDLive success!\n");
key = waitKey(300);
}
else
{
printf("BeginQHYCCDLive failed\n");
goto failure;
}
/////////////////////////////////////////
/////////////////////////////////////////
//outfile<<"%Data cube in MATLAB compatible format - m(h,w,slice)"<<std::endl;
doneflag = 0;
ret = SetQHYCCDParam(camhandle, CONTROL_EXPOSURE, camtime); //handle, parameter name, exposure time (which is in us)
if (ret == QHYCCD_SUCCESS)
{
printf("Exp time = %d \n", camtime);
}
else
{
printf("CONTROL_EXPOSURE fail\n");
goto failure;
}
t_start = time(NULL);
fps = 0;
indexi = 0;
manualindexi = 0;
indextemp = 0;
bscantransposed = Mat::zeros(Size(numdisplaypoints, oph), CV_64F);
manualaccum = Mat::zeros(Size(oph, numdisplaypoints), CV_64F); // this is transposed version
//bscantransposedl = Mat::zeros(Size(opw/2, oph), CV_64F);
for (uint p = 0; p<(opw); p++)
{
// create modified Bartlett-Hann window
// https://in.mathworks.com/help/signal/ref/barthannwin.html
float nn = p;
float NN = opw - 1;
barthannwin.at<double>(0, p) = 0.62 - 0.48*std::abs(nn / NN - 0.5) + 0.38*std::cos(2 * pi*(nn / NN - 0.5));
}
while (1) //camera frames acquisition loop
{
ret = GetQHYCCDLiveFrame(camhandle, &w, &h, &bpp, &channels, mraw.data);
if (ret == QHYCCD_SUCCESS)
{
//median filter while the numbers are still int
if (mediann>0)
medianBlur(mraw, m, mediann);
else
mraw.copyTo(m);
resize(m, opm, Size(), 1.0 / binvalue, 1.0 / binvalue, INTER_AREA); // binning (averaging)
imshow("show", opm);
if (saveinterferograms)
{
// save mraw to active buffer
// inactive buffer is saved to disk when skeypressed
if (zeroisactive)
{
mraw.copyTo(interferogramsave0[indextemp]);
opm.copyTo(interferogrambsave0[indextemp]);
//printf("Saved to interferogramsave0[%d]\n",indextemp);
//sprintf(debugwinname,"debug%d",baccumcount);
//imshow(debugwinname,interferogramsave0[indextemp]);
//waitKey(30);
}
else
{
mraw.copyTo(interferogramsave1[indextemp]);
opm.copyTo(interferogrambsave1[indextemp]);
//printf("Saved to interferogramsave1[%d]\n",indextemp);
//sprintf(debugwinname,"debug%d",baccumcount);
//imshow(debugwinname,interferogramsave1[indextemp]);
//waitKey(30);
}
}
opm.convertTo(data_y, CV_64F); // initialize data_y
// smoothing by weighted moving average
if (movavgn > 0)
data_y = smoothmovavg(data_y, movavgn);
//transpose(opm, data_y); // void transpose(InputArray src, OutputArray dst)
// because we actually want the columns and not rows
// using DFT_ROWS
// But that has rolling shutter issues, so going back to rows
if (bkeypressed == 1)
{
data_yb = (data_yr - data_yd) + (data_ys - data_yd);
if (manualaveraging)
{
averagestoggle = 1;