-
Notifications
You must be signed in to change notification settings - Fork 59
/
dftarea.cpp
1639 lines (1337 loc) · 48 KB
/
dftarea.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
/******************************************************************************
**
** Copyright 2016 Dale Eason
** This file is part of DFTFringe
** is free software: you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation version 3 of the License
** DFTFringe is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with DFTFringe. If not, see <http://www.gnu.org/licenses/>.
****************************************************************************/
#include "dftarea.h"
#include "ui_dftarea.h"
#include "dfttools.h"
#include "vortex.h"
#include <queue>
#include "punwrap.h"
#include "zernikeprocess.h"
#include "settings2.h"
#include "myutils.h"
#include "opencv2/imgproc/imgproc.hpp"
#include "utils.h"
#include "showaliasdlg.h"
#include <QLabel>
#include <QShortcut>
#include <opencv2/core/core_c.h>
using namespace cv;
cv::Mat makeMask(CircleOutline outside, CircleOutline center, cv::Mat data,
QVector<std::vector<cv::Point> > poly){
int width = data.cols;
int height = data.rows;
double radm = ceil(outside.m_radius) + 1;
double rady = radm;
mirrorDlg &md = *mirrorDlg::get_Instance();
if (md.isEllipse())
rady = radm * md.m_verticalAxis/md.diameter;
double rado = center.m_radius;
double cx = outside.m_center.x();
double cy = outside.m_center.y();
cv::Mat mask = cv::Mat::zeros(height,width,CV_8UC1);
for (int y = 0; y < height; ++y){
for (int x = 0; x < width; ++x){
double dx = (double)(x - cx)/(radm);
double dy = (double)(y - cy)/(rady);
if (sqrt(dx * dx + dy * dy) <= 1.)
mask.at<uchar>(y,x) = 255;
}
}
cx = center.m_center.x();
cy = center.m_center.y();
if (rado > 0) {
for (int y = 0; y < height; ++y){
for (int x = 0; x < width; ++x){
double dx = (double)(x - (cx))/(rado);
double dy = (double)(y - (cy))/(rado);
if (sqrt(dx * dx + dy * dy) < 1.)
mask.at<uchar>(y,x) = 0;
}
}
}
if (poly.size()>0){
for (int n = 0; n < poly.size(); ++n){
cv::Point points[1][poly[n].size()];
for (std::size_t i = 0; i < poly[n].size(); ++i){
points[0][i] = cv::Point(poly[n][i].x, poly[n][i].y);
}
for (std::size_t j = 0; j < poly[n].size()-1; ++j){
cv::line(mask, points[0][j], points[0][j+1], cv::Scalar(0));
}
const Point* ppt[1] = { points[0]};
int npt[] = { static_cast<int>(poly[n].size()) };
fillPoly( mask, ppt, npt, 1, Scalar(0), 8 );
}
}
if (Settings2::showMask())
showData("DFT mask",mask);
return mask;
}
DFTArea *DFTArea::m_Instance = 0;
DFTArea * DFTArea::get_Instance(QWidget *parent, IgramArea *ip, DFTTools *tools, vortexDebug *vdbug){
if (m_Instance == NULL){
m_Instance = new DFTArea(parent, ip,tools,vdbug);
}
return m_Instance;
}
DFTArea::DFTArea(QWidget *mparent, IgramArea *ip, DFTTools * tools, vortexDebug *vdbug) :
QWidget(mparent),m_size(640), tools(tools),
dftSizeStr("640 X 640"), m_center_filter(10.),ui(new Ui::DFTArea),igramArea(ip),m_smooth(9.),
m_vortexDebugTool(vdbug),zoom(1.)
{
m_outlineComplete = false;
m_PSIstate = 0;
QRect rec = QGuiApplication::primaryScreen()->geometry();
m_Psidlg = new PSI_dlg(nullptr);
rec.setLeft(rec.width()/6);
rec.setTop(rec.height()/4);
rec.setWidth(rec.width()/4);
rec.setHeight(rec.height()/4);
m_Psidlg->setGeometry(rec);
connect(m_Psidlg, SIGNAL(computePhase()),this, SLOT(computePhases()));
ui->setupUi(this);
m_gamma = 2.5;
connect(tools,SIGNAL(dftChannel(const QString&)), this, SLOT(setChannel(const QString&)));
connect(tools,SIGNAL(dftSizeVal(int)), this, SLOT(dftSizeVal(int)));
connect(tools,SIGNAL(dftCenterFilter(double)), this, SLOT(dftCenterFilter(double)));
connect(tools,SIGNAL(makeSurface()), this,SLOT(makeSurface()));
connect(tools,SIGNAL(doDFT()), this,SLOT(doDFT()));
connect(tools,SIGNAL(showResized()),this, SLOT(showResizedDlg()));
QShortcut *shortcut = new QShortcut(QKeySequence(Qt::Key_Plus), this);
QObject::connect(shortcut, SIGNAL(activated()), this, SLOT(zoomPlus()));
shortcut = new QShortcut(QKeySequence(Qt::Key_Minus), this);
QObject::connect(shortcut, SIGNAL(activated()), this, SLOT(zoomMinus()));
shortcut = new QShortcut(QKeySequence(Qt::Key_F), this);
QObject::connect(shortcut, SIGNAL(activated()), this, SLOT(zoomFit()));
tools->connectTo(this);
capture = false;
QSettings set;
m_center_filter = set.value("DFT Center Filter", 10).toDouble();
qDebug() << "init center" << m_center_filter;
emit updateFilterSize(m_center_filter);
installEventFilter(this);
/*
QwtPlot *test = new QwtPlot();
QwtPlotGrid *grid = new QwtPlotGrid();
grid->attach(test);
QPolygonF points;
mirrorDlg &md = *mirrorDlg::get_Instance();
double roc = md.roc;
double diam = md.diameter;
double r3 = roc * roc * roc;
double d4 = diam * diam * diam * diam;
for (double i = 0; i < .03; i += .0001){
double z1 = -i * 384. * r3 * md.lambda * 1.E-6/(d4);
points << QPointF( -i, z1 );
}
test->setAxisTitle( test->xBottom, "Z8" );
test->setAxisTitle( test->yLeft, "Best conic" );
QwtPlotCurve *c3 = new QwtPlotCurve("Best fit Conic");
c3->setSamples(points);
c3->attach(test);
test->resize(800,800);
test->show();
*/
}
#include <QWheelEvent>
void DFTArea::zoomPlus(){
zoom *= 1.1;
update();
}
void DFTArea::zoomMinus(){
zoom /= 1.1;
update();
}
void DFTArea::zoomFit(){
zoom = double(this->size().height())/magIImage.size().height();
update();
}
bool DFTArea::eventFilter(QObject * /*obj*/, QEvent *event) {
if (event->type() == QEvent::Wheel) {
QWheelEvent *w = (QWheelEvent *)event;
if (w->angleDelta().y() > 0){
zoom *= 1.1;
}
else {
zoom /= 1.1;
}
if (zoom < 1.)
zoom = 1.;
//magIImage = magIImage.scaled(magIImage.width() * scale, magIImage.height() * scale);
update();
// Your implementation.
// You can't use QWheelEvent, as Graphicscene works with its own events...
//handleWheelOnGraphicsScene(static_cast<QGraphicsSceneWheelEvent*> (event));
// Don't propagate
return true;
}
// Other events should propagate
return false;
}
DFTArea::~DFTArea()
{
delete m_Psidlg;
delete ui;
}
void DFTArea::setChannel(const QString& val){
channel = val;
}
void DFTArea::dftCenterFilter(double v){
m_center_filter = v;
QSettings set;
set.setValue("DFT Center Filter", v);
emit updateFilterSize(v);
update();
}
void DFTArea::dftSizeVal(int val){
m_size = val;
}
void DFTArea::dftSizeChanged(const QString& val){
dftSizeStr = val;
if (val == "same as Igram"){
int newVal = igramArea->igramGray.width();
emit setDftSizeVal(newVal);
}
if (val == "640 x 640") {
int newVal = 640;
emit setDftSizeVal(newVal);
}
}
cv::Mat DFTArea::grayComplexMatfromImage(QImage &img){
// create an roi that is a square around the outline.
double centerX = igramArea->m_outside.m_center.x();
double centerY = igramArea->m_outside.m_center.y();
mirrorDlg &md = *mirrorDlg::get_Instance();
double pixelsPermm =(igramArea->m_outside.m_radius/(md.diameter/2.));
double reduction = md.aperatureReduction * pixelsPermm;
if (md.m_aperatureReductionEnabled == false)
reduction = 0;
double rad = igramArea->m_outside.m_radius - reduction;
double rady = rad;
if (md.isEllipse()){
rady = rady * md.m_verticalAxis/ md.diameter;
}
double left = centerX - rad;
double top = centerY - rad;
std::vector<Mat > bgr_planes;
top = std::max(top,0.);
left = std::max(left,0.);
int width = 2. * (rad);
int height = width;
width = min(width, img.width());
height = min(height, img.height());
// new center because of crop
double xCenterShift = centerX - left;
double yCenterShift = centerY - top;
cv::Mat iMat(img.height(), img.width(), CV_8UC3, img.bits(), img.bytesPerLine());
cv::Mat tmp = iMat.clone();
cv::Mat roi = iMat(cv::Rect((int)left,(int)top,(int)width,(int)height)).clone();
double centerDx = centerX - igramArea->m_center.m_center.x();
double centerDy = centerY - igramArea->m_center.m_center.y();
roi.convertTo(roi,CV_32FC3);
QSettings set;
int dftSize = set.value("DFTSize", 640).toInt();
double scaleFactor = (double)dftSize/roi.cols;
m_outside = CircleOutline(QPointF(xCenterShift,yCenterShift), rad);
m_center = CircleOutline(QPointF(xCenterShift - centerDx, yCenterShift - centerDy),
igramArea->m_center.m_radius);
//scaleFactor = 1;
if (scaleFactor < 1.){
cv::resize(roi,roi, cv::Size(0,0), scaleFactor, scaleFactor,INTER_AREA);
double roicx = (roi.cols-1)/2.;
double roicy = (roi.rows-1)/2.;
m_outside = CircleOutline(QPointF(roicx,roicy),roicx);
m_center = CircleOutline(QPointF((roicx - centerDx * scaleFactor), (roicy - centerDy * scaleFactor)),
m_center.m_radius * scaleFactor);
}
else {
scaleFactor = 1.;
}
m_poly.clear();
for (int n = 0; n < igramArea->m_polygons.size(); ++n){
m_poly.append(std::vector< cv::Point>());
for (unsigned int i = 0; i < igramArea->m_polygons[n].size(); ++i){
int x = round((igramArea->m_polygons[n][i].x - left) * scaleFactor);
int y = round((igramArea->m_polygons[n][i].y - top) * scaleFactor);
// make sure x and y values of regions are inside our matrix
if (x < 0)
x=0;
if (x >= roi.cols)
x=roi.cols-1;
if (y < 0)
y=0;
if (y >= roi.rows)
y = roi.rows-1;
m_poly.back().push_back(cv::Point(x,y));
}
}
// split image into three color planes
split( roi, bgr_planes );
cv::Scalar mean;
mean = cv::mean(bgr_planes[0]);
n = 0;
cv::Mat padded = bgr_planes[0].clone();
padded = padded - mean[0];
// disabled adding optomizing DFT boarder.
Mat planes[] = {Mat_<float>(padded), Mat::zeros(padded.size(), CV_32F)};
m_mask = makeMask(m_outside,m_center,*planes, m_poly);
if (Settings2::showMask())
showData("Mask", m_mask);
cv::Mat tmpMask;
planes[0].copyTo(tmpMask,m_mask); // Convert image to binary
planes[0] = tmpMask.clone();
mean = cv::mean(planes[0],m_mask);
planes[0] -= mean;
Mat complexI;
merge(planes, 2, complexI); // Add to the expanded another plane with zeros
if (scaleFactor == 1.){
tools->imageSize(QString("DFT Size will be %1").arg(width));
}
else
tools->imageSize(QString("Image is resized from %1 to %2 pixels").arg(width).arg(complexI.cols));
return complexI;
}
//swap quadrants
void shiftDFT(cv::Mat &magI){
// crop the spectrum, if it has an odd number of rows or columns
magI = magI(Rect(0, 0, magI.cols & -2, magI.rows & -2));
// rearrange the quadrants of Fourier image so that the origin is at the image center
int cx = magI.cols/2;
int cy = magI.rows/2;
Mat q0(magI, Rect(0, 0, cx, cy)); // Top-Left - Create a ROI per quadrant
Mat q1(magI, Rect(cx, 0, cx, cy)); // Top-Right
Mat q2(magI, Rect(0, cy, cx, cy)); // Bottom-Left
Mat q3(magI, Rect(cx, cy, cx, cy)); // Bottom-Right
Mat tmp; // swap quadrants (Top-Left with Bottom-Right)
q0.copyTo(tmp);
q3.copyTo(q0);
tmp.copyTo(q3);
q1.copyTo(tmp); // swap quadrant (Top-Right with Bottom-Left)
q2.copyTo(q1);
tmp.copyTo(q2);
}
void showData(const std::string& txt, cv::Mat mat, bool useLog){
cv::Mat tmp = mat.clone();
if (useLog){
tmp = mat+1;
log(tmp, tmp);
}
cv::namedWindow(txt, WINDOW_NORMAL);
normalize(tmp, tmp,0,255,CV_MINMAX);
tmp.convertTo(tmp,CV_8U);
cvtColor(tmp,tmp, cv::COLOR_GRAY2RGB);
cv::imshow(txt, tmp);
cv::waitKey(1);
}
QImage showMag(cv::Mat complexI, bool show, const char* title, bool doLog, double gamma){
// compute the magnitude and switch to logarithmic scale
// => log(1 + sqrt(Re(DFT(I))^2 + Im(DFT(I))^2))
Mat planes[2];
split(complexI, planes); // planes[0] = Re(DFT(I), planes[1] = Im(DFT(I))
magnitude(planes[0], planes[1], planes[0]);// planes[0] = magnitude
Mat magI = planes[0];
double mmin;
double mmax;
minMaxIdx(magI, &mmin,&mmax);
magI-= mmin;
if (doLog)
log((magI+0.1), magI);
if (gamma != 0.){
cv::pow(magI,gamma,magI);
}
normalize(magI, magI,0,255,CV_MINMAX, CV_8U);
minMaxIdx(magI, &mmin,&mmax);
cv::Mat tmp = magI.clone();
cv::waitKey(1);
cvtColor(magI,magI, cv::COLOR_GRAY2RGB);
if (show){
imshow(title, magI);
waitKey(1);
}
return QImage((uchar*)magI.data, magI.cols, magI.rows, magI.step, QImage::Format_RGB888).copy();
}
RNG rng(12345);
void DFTArea::doDFT(){
QApplication::setOverrideCursor(Qt::WaitCursor);
QImage img = igramArea->igramGray;
cv::Mat complexI = grayComplexMatfromImage(img);
cv::Mat planes[2];
split(complexI,planes);
cv::Scalar mean,std;
cv::meanStdDev(planes[0],mean,std, m_mask);
//qDebug() << "Mean " << mean[0];
planes[0] -= mean[0];
//cv::imshow("input", planes[0].clone());
//cv::waitKey(1);
cv::merge(planes,2,complexI);
dft(complexI, complexI);
cv::Mat realImage;
dft(complexI,realImage,DFT_INVERSE);
split(realImage,planes);
shiftDFT(complexI);
m_dft.release();
m_dft = complexI/complexI.size().area();
magIImage = showMag(complexI,false,"", true, m_gamma);
magIImage = magIImage.scaled(magIImage.width() , magIImage.height() );
setMinimumSize(magIImage.size());
emit selectDFTTab();
update();
zoom = double(this->size().height())/magIImage.size().height();
QSettings set;
if (set.value("showDFTHelp", true).toBool()){
if (QMessageBox::Yes == QMessageBox::question(0,"Did you know you can use the Scroll wheel to zoom this image?.","Select yes to not display this message again."))
{
set.setValue("showDFTHelp",false);
}
}
//if (Settings2::showDFT())
//emit dftReady(magIImage); //Creates a thumbnail dft area
QApplication::restoreOverrideCursor();
}
void DFTArea::gamma(int i){
double v = 1. + 5. * (double)i/99.;
m_gamma = v;
doDFT();
}
void DFTArea::paintEvent(QPaintEvent *)
{
QPainter painter(this);
QPixmap rp = QPixmap::fromImage(magIImage);
QPainter dftpainter(&rp);
dftpainter.drawImage(QPoint(0,0),magIImage);
dftpainter.setBrush(QColor(0,0,100,50));
int centerx = (magIImage.width()+1)/2;
int centery = (magIImage.height()+1)/2;
dftpainter.drawEllipse(QPointF(centerx, centery),
m_center_filter, m_center_filter);
if (m_center_filter > 0.) {
double val = (double)m_center_filter;
val *= val;
int last = 0;
for (int i = 0; i < magIImage.width()-1; ++i){
double rho = i -magIImage.width()/2;
rho *= rho;
double a =1-exp (-rho/(.25 * val));
int y = magIImage.height()/2 + 50 -a * 50;
dftpainter.drawLine(i,last,i+1,y);
last = y;
}
}
int xoffset = -centerx * (zoom -1) +(width() - magIImage.width() )/2;
int yoffset = -centery * (zoom -1) + (height() - magIImage.height())/2;
painter.drawPixmap(xoffset,yoffset,rp.scaled( zoom * rp.width(), zoom * rp.height()));
}
#define WRAP(x) (((x) > 0.5) ? ((x)-1.0) : (((x) <= -0.5) ? ((x)+1.0) : (x)))
#define WRAPPI(x) (((x) > M_PI) ? ((x)-2*M_PI) : (((x) <= -M_PI) ? ((x)+2*M_PI) : (x)))
#define BORDER 0x1
#define UNWRAPPED 0x2
double *g_qmap;
class comp_qual {
public:
int operator() (const int &p1, const int &p2) {
return (g_qmap[p1] < g_qmap[p2]);
}
};
#define unwrap_and_insert(ndx, val) \
{ \
unwrapped[ndx] = val; \
flags[ndx] |= UNWRAPPED; \
path[ndx] = order++; \
todo.push (ndx); \
}
// Quality-guided path following phase unwrapper.
void qg_path_follower_vortex (Size size, double *phase, double *qmap,
double *unwrapped, double *path)
{
std::priority_queue<int, std::vector<int>, comp_qual> todo;
int total = size.area();
int order = 0;
g_qmap = qmap;
// Initialize the flags array to mark the border.
char *flags = new char[total];
for (int k=0; k < total; ++k)
flags[k] = phase[k] == 0.0;
// Repeat while still elements to unwrap (handles disjoint regions).
while (1) {
// Find the point of highest quality.
double m = -HUGE_VAL;
int mndx;
for (int k=0; k < total; ++k)
if (qmap[k] > m && ! flags[k])
m = qmap[mndx = k];
if (m == -HUGE_VAL) break;
// Unwrap the first point.
unwrap_and_insert (mndx, phase[mndx]);
// Unwrap the rest of the points in order of quality.
while (!todo.empty()) {
int ndx = todo.top();
todo.pop();
int x = ndx%size.width;
int y = ndx/size.width;
double val = unwrapped[ndx];
if (x > 0 && ! flags[ndx-1])
unwrap_and_insert (ndx-1, val+WRAP(phase[ndx-1]-phase[ndx]));
if (x < size.width-1 && ! flags[ndx+1])
unwrap_and_insert (ndx+1, val+WRAP(phase[ndx+1]-phase[ndx]));
if (y > 0 && ! flags[ndx-size.width])
unwrap_and_insert (ndx-size.width, val+WRAP(phase[ndx-size.width]-phase[ndx]));
if (y < size.height-1 && ! flags[ndx+size.width])
unwrap_and_insert (ndx+size.width, val+WRAP(phase[ndx+size.width]-phase[ndx]));
}
}
delete[] flags;
}
cv::Mat DFTArea::vortex(QImage &img, double low)
{
try
{
showmem("start Vortex");
cv::Mat image = grayComplexMatfromImage(img);
// convert from 32 to 64 bit double values
cv::Mat imageMat;
image.convertTo(imageMat, numType);
image.release();
int xsize = imageMat.cols;
int ysize = imageMat.rows;
int size = xsize*ysize;
double smooth = .01 * m_vortexDebugTool->m_smooth * xsize/2.;
double *dir = new double[size];
double *path = new double[size];
double *qmap = new double[size];
double *orient = new double[size];
//double *fdom[2]; fdom[0] = new double[size]; fdom[1] = new double[size];
double *d1[2]; d1[0] = new double[size]; d1[1] = new double[size];
double *d2[2]; d2[0] = new double [size]; d2[1] = new double [size];
double *spiralRe = new double [size];
double *spiralIm = new double[size];
if (m_vortexDebugTool->m_showInput){
double *imReTmp = new double[size];
{
cv::Mat tmp = cv::Mat(ysize,xsize,numType, imReTmp);
cv::Mat xx;
tmp.convertTo(xx,CV_32F);
cv::imshow("input", xx);
cv::waitKey(1);
} // delete imReTmp after tmp is destructed by end of scope
delete[] imReTmp;
}
cv::Mat fdomMat;
cv::Mat fdomPlanes[2];
dft(imageMat,fdomMat);
split(fdomMat, fdomPlanes);
// Create rho and theta arrays for later use.
int *ix = new int[xsize];
int *iy = new int[ysize];
for (int i=0; i<=xsize/2; ++i) ix[i] = -i;
for (int i=1; i<=xsize/2; ++i) ix[xsize-i] = i;
for (int i=0; i<=ysize/2; ++i) iy[i] = -i;
for (int i=1; i<=ysize/2; ++i) iy[ysize-i] = i;
double *rho = new double[size];
double *theta = new double[size];
for (int j=0; j<ysize; ++j) {
int base = j*xsize;
for (int i=0; i<xsize; ++i) {
rho[base+i] = sqrt (ix[i]*ix[i] + iy[j]*iy[j]);
theta[base+i] = atan2 (iy[j], ix[i]);
}
}
delete[] ix;
delete[] iy;
// High-pass filter the Fourier domain to remove the background.
double *planes[2] = {fdomPlanes[0].ptr<double>(0), fdomPlanes[1].ptr<double>(0)};
if (low > 0)
{
for (int i=0; i<size; ++i) {
double a = 1.0 - exp (-(rho[i]*rho[i])/(low*low));
planes[0][i] *= a;
planes[1][i] *= a;
}
}
merge(fdomPlanes,2, fdomMat);
if (m_vortexDebugTool->m_showFdom){
showMag(fdomMat.clone(),true,"fdom");
}
// Take the inverse Fourier transform to get the cleaned igram.
//p = fftw_plan_dft_2d (ysize, xsize, fdom, im, FFTW_BACKWARD, FFTW_ESTIMATE);
//fftw_execute (p);
dft(fdomMat, imageMat, DFT_INVERSE);
cv::Mat imPlanes[2];
split(imageMat,imPlanes);
imPlanes[0]/= size;
cv::Mat tmp;
imPlanes[0].copyTo(tmp, m_mask);
imPlanes[0] = tmp.clone();
tmp.release();
// Normalize the image by removing the exterior and centering values.
cv::Scalar mean,std;
cv::meanStdDev(imPlanes[0],mean,std, m_mask);
double sum = 0;
double *q = imPlanes[0].ptr<double>(0);
merge(imPlanes,2, imageMat);
dft(imageMat,fdomMat);
imageMat.release();
double dc = fdomMat.at<double>(0,0,0);
dc/=size;
int count = 0;
bool *bp = m_mask.ptr<bool>(0);
for (int i = 0; i < size; ++i){
if (bp[i]){
sum += q[i];
if (q[i] != 0.0)
++count;
}
}
double m2 = sum/count;
imPlanes[0] -= m2;
imPlanes[1] *= 0.;
double *imRe = (double *)(imPlanes[0].data);
if (0) { //(0 == strcmp (what, "im2")) {
showData("im border added", imPlanes[0].clone());
}
// Calculate the intermediate values d1 and d2.
for (int i=0; i<size; ++i) {
spiralRe[i] = cos(theta[i]);
spiralIm[i] = sin(theta[i]);
}
delete[] theta;
split(fdomMat,fdomPlanes);
double *fdomRe = (double*)(fdomPlanes[0].data);
double *fdomIm = (double *)(fdomPlanes[1].data);
for (int i=0; i<size; ++i) {
double re = fdomRe[i]*spiralRe[i] - fdomIm[i]*spiralIm[i];
double im = fdomRe[i]*spiralIm[i] + fdomIm[i]*spiralRe[i];
fdomRe[i] = re;
fdomIm[i] = im;
}
merge(fdomPlanes,2,fdomMat);
cv::Mat d1Mat;
dft(fdomMat,d1Mat, DFT_INVERSE);
//p = fftw_plan_dft_2d (ysize, xsize, fdomMat, d1Mat, FFTW_BACKWARD, FFTW_ESTIMATE);
//fftw_execute (p);
d1Mat/= size;
if (0){ //(0 == strcmp (what, "d1")) {
showMag(d1Mat.clone(), true, "D1");
}
for (int i=0; i<size; ++i) {
double re = fdomRe[i]*spiralRe[i] - fdomIm[i]*spiralIm[i];
double im = fdomRe[i]*spiralIm[i] + fdomIm[i]*spiralRe[i];
fdomRe[i] = re;
fdomIm[i] = im;
}
cv::Mat d2Mat;
merge(fdomPlanes,2, fdomMat);
dft(fdomMat,d2Mat,DFT_INVERSE);
//p = fftw_plan_dft_2d (ysize, xsize, fdom, d2, FFTW_BACKWARD, FFTW_ESTIMATE);
//fftw_execute (p);
d2Mat/= size;
if (0){//(0 == strcmp (what, "d2")) {
showMag(d2Mat.clone(),true, "D2");
}
cv::Mat rMat;
cv::Mat rPlanes[2] = {cv::Mat::zeros(Size(xsize,ysize),numType), cv::Mat::zeros(Size(xsize,ysize),numType)};
cv::Mat d1Planes[2];
cv::Mat d2Planes[2];
split(d1Mat,d1Planes);
d1Mat.release();
split(d2Mat,d2Planes);
d2Mat.release();
double *d1Re = (double *)(d1Planes[0].data);
double *d1Im = (double *)(d1Planes[1].data);
double *d2Re = (double *)(d2Planes[0].data);
double *d2Im = (double *)(d2Planes[1].data);
double *rRe = (double *)(rPlanes[0].data);
double *rIm = (double *)(rPlanes[1].data);
// Calculate the orientation and the quality map for unwrapping.
for (int i=0; i<size; ++i) {
rRe[i] = d1Re[i]*d1Re[i] - d1Im[i]*d1Im[i] - imRe[i]*d2Re[i];
rIm[i] = d1Re[i]*d1Im[i] + d1Im[i]*d1Re[i] - imRe[i]*d2Im[i];
}
//smooth = 0;
if (smooth > 0) {
merge(rPlanes,2, rMat);
cv::Mat temp;
cv::Mat tempPlanes[2];
// Low-pass filter r to smooth it.
dft(rMat,temp);
//p = fftw_plan_dft_2d (ysize, xsize, r, temp, FFTW_FORWARD, FFTW_ESTIMATE);
//fftw_execute (p);
split(temp,tempPlanes);
double *tempRe = (double *)(tempPlanes[0].data);
double *tempIm = (double *)(tempPlanes[1].data);
for (int i=0; i<size; ++i) {
double a = exp (-(rho[i]*rho[i])/(smooth * smooth));
tempRe[i] *= a;
tempIm[i] *= a;
}
delete[] rho;
merge(tempPlanes,2,temp);
tempPlanes[0].release();
tempPlanes[1].release();
dft(temp,rMat,DFT_INVERSE);
temp.release();
//p = fftw_plan_dft_2d (ysize, xsize, temp, r, FFTW_BACKWARD, FFTW_ESTIMATE);
//fftw_execute (p);
rMat/= size;
split(rMat,rPlanes);
rMat.release();
}
for (int i=0; i<size; ++i)
orient[i] = atan2 (rIm[i], rRe[i]);
if (m_vortexDebugTool->m_showOrientation){
cv::Mat orm(ysize,xsize,numType,orient);
showData("orient", orm.clone());
}
for (int i=0; i<size; ++i) {
qmap[i] = sqrt (rRe[i]*rRe[i] + rIm[i]*rIm[i]);
orient[i] /= (2.*M_PI); // put in range -.5..5 for unwrap
}
// Unwrap the orientation to get the direction.
qg_path_follower_vortex (Size(xsize,ysize), orient, qmap, dir, path);
for (int i=0; i<size; ++i)
dir[i] = WRAPPI(dir[i]*M_PI);
// Calculate the quadrature.
imPlanes[1] = cv::Mat::zeros(Size(xsize,ysize), numType);
double *imIm = (double *)(imPlanes[1].data);
for (int i=0; i<size; ++i)
imIm[i] = d1Re[i]*cos(-dir[i]) - d1Im[i]*sin(-dir[i]);
// Display the isolated side lobe.
if (m_vortexDebugTool->m_showFdom3){
cv::Mat sideLobe;
merge(imPlanes,2,sideLobe);
shiftDFT(sideLobe);
dft(sideLobe,fdomMat);
shiftDFT(fdomMat);\
showMag(fdomMat, true, "fdom3");
sideLobe.release();
}
cv::Mat phase(Size(xsize,ysize), numType);
double *p = (double *)(phase.data);
for (int i=0; i<size; ++i) {
p[i] = atan2 (imIm[i], imRe[i]);
}
imPlanes[0].release();
imPlanes[1].release();
// mask to only the mirror portion.
phase.copyTo(tmp, m_mask);
phase = tmp.clone();
if (m_vortexDebugTool->m_showWrapped){
cv::Mat tt = phase.clone();
cv::normalize(tt,tt,0.f,1.f,CV_MINMAX);
cv::imshow(" wrapped ", tt);
cv::waitKey(1);
}
delete[] qmap;
delete[] orient;
delete[] d1[0];
delete[] d1[1];
delete[] d2[0];
delete[] d2[1];
delete[] spiralRe;
delete[] spiralIm;
delete[] dir;
delete[] path;
tmp.release();
return phase;
}
catch (std::bad_alloc &e){
showmem();
qDebug() << QString("Error %1").arg(e.what());
//cv::Mat phase = cv::Mat::zeros(Size(100,100), numType);
return cv::Mat();
}
catch ( std::exception &e){
qDebug() << QString(" some Error %1").arg(e.what());
cv::Mat phase = cv::Mat::zeros(Size(0,0), numType);
return phase;
}
catch (...){
qDebug() << QString(" Unknown error ");
cv::Mat phase = cv::Mat::zeros(Size(100,100), numType);
return phase;
}
}
cv::Mat_<double> subtractPlane(cv::Mat_<double> phase, cv::Mat_<bool> mask){
cv::Mat_<double> coeff(3,1);
cv::Mat_<double> X(phase.rows * phase.cols,3);
cv::Mat_<double> Z(phase.rows * phase.cols,1);
int ndx = 0;
for (int y = 0; y < phase.rows; ++y){
for (int x = 0; x < phase.cols; ++x){
if (mask(y,x)){
Z(ndx) = phase(y,x);
X(ndx,0) = x;
X(ndx,1) = y;
X(ndx++,2) = 1.;
}
}
}
cv::solve(X,Z,coeff,CV_SVD);
// plane generation, Z = Ax + By + C
// distance calculation d = Ax + By - z + C / sqrt(A^2 + B^2 + C^2)
cv::Mat_<double> newPhase(phase.size());
for (int y = 0; y < phase.rows; ++y){
for (int x = 0; x < phase.cols; ++x){
int b = (int)mask(y,x);
if (b == 0 ){
continue;
}
double val = x * coeff(0) + y * coeff(1) + coeff(2) - phase(y,x);
double z = val/sqrt(coeff(0) * coeff(0) + coeff(1) * coeff(1) + 1);
newPhase(y,x) = z;
}
}
return newPhase;
}
// make a surface from the image using DFT and vortex transfroms.
void DFTArea::makeSurface(){
if (!tools->wasPressed)
return;
MainWindow::me->m_outlinePlots->hide();
if (igramArea->igramGray.isNull()){
QMessageBox::warning(0,"warning","First load an interferogram and circle the mirror.");
return;
}
QApplication::setOverrideCursor(Qt::WaitCursor);
tools->wasPressed = false;
if (Settings2::getInstance()->m_igram->m_autoSaveOutline){
igramArea->writeOutlines(igramArea->makeOutlineName()); // save outlines including center filter
}
cv::Mat phase;
try {
phase = vortex(igramArea->igramGray, m_center_filter);
}
catch (std::exception const& e){