-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwake2.cpp
1832 lines (1528 loc) · 49.6 KB
/
wake2.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
/*
* The information in this file is
* Copyright(c) 2011 Ball Aerospace & Technologies Corporation
* and is subject to the terms and conditions of the
* GNU Lesser General Public License Version 2.1
* The license text is available from
* http://www.gnu.org/licenses/lgpl.html
*/
#include "AoiElement.h"
#include "AppConfig.h"
#include "AppVerify.h"
#include "BitMask.h"
#include "DataAccessor.h"
#include "DataAccessorImpl.h"
#include "DataRequest.h"
#include "DesktopServices.h"
#include "MessageLogResource.h"
#include "ModelServices.h"
#include "ObjectResource.h"
#include "PlugInArgList.h"
#include "PlugInManagerServices.h"
#include "PlugInResource.h"
#include "PlugInRegistration.h"
#include "Progress.h"
#include "RasterDataDescriptor.h"
#include "RasterElement.h"
#include "StringUtilities.h"
#include "switchOnEncoding.h"
#include "wake2.h"
#include "TypeConverter.h"
#include <limits>
#include <vector>
#include <QtCore/QStringList>
#include <QtGui/QInputDialog>
#include "RasterUtilities.h"
#include "AppConfig.h"
#include "AppVerify.h"
#include "DataAccessor.h"
#include "DataAccessorImpl.h"
#include "DataRequest.h"
#include "MessageLogResource.h"
#include "ObjectResource.h"
#include <algorithm>
#include "SpatialDataView.h"
#include "SpatialDataWindow.h"
#include <sstream>
#include <string>
#include <math.h>
#include <QtCore/QStringList>
#include <QtGui/QInputDialog>
#include "GcpList.h"
#include "AppVerify.h"
#include "DimensionDescriptor.h"
#include "LayerList.h"
#include "ModelServices.h"
#include "RasterFileDescriptor.h"
#include "RasterLayer.h"
using namespace std;
double long pi10=3.14159265;
REGISTER_PLUGIN_BASIC(OpticksTutorial, Wake2);
namespace
{
template<typename T>
void conversion8(T* pData, double number, int rc, int cool)
{
*pData=(number)*255.0/cool;
}
};
namespace
{
template<typename T>
void conversion11(T* pData, double number)
{
*pData=number;
}
};
void updateStatistics16(double pData, double& total, double& total_sum, int& count)
{
total += pData;
total_sum += pData * pData;
count+=1;
}
Wake2::Wake2()
{
setDescriptorId("{12840922-D041-48D8-A0F7-375CB704CA1F}");
setName("Wake2");
setDescription("Using AOIs.");
setCreator("Opticks Community");
setVersion("Sample");
setCopyright("Copyright (C) 2011, Ball Aerospace & Technologies Corp.");
setProductionStatus(false);
setType("Sample");
setSubtype("Statistics");
setMenuLocation("[Tutorial]/Wake2");
setAbortSupported(true);
}
Wake2::~Wake2()
{
}
bool Wake2::getInputSpecification(PlugInArgList*& pInArgList)
{
VERIFY(pInArgList = Service<PlugInManagerServices>()->getPlugInArgList());
pInArgList->addArg<Progress>(Executable::ProgressArg(), NULL, "Progress reporter");
pInArgList->addArg<RasterElement>(Executable::DataElementArg(), "Generate statistics for this raster element");
if (isBatch())
{
pInArgList->addArg<AoiElement>("AOI", NULL, "The AOI to calculate statistics over");
}
return true;
}
bool Wake2::getOutputSpecification(PlugInArgList*& pOutArgList)
{
VERIFY(pOutArgList = Service<PlugInManagerServices>()->getPlugInArgList());
pOutArgList->addArg<RasterElement>("RResult", NULL);
return true;
}
bool Wake2::execute(PlugInArgList* pInArgList, PlugInArgList* pOutArgList)
{
StepResource pStep("Wake2", "app", "DFE96709-69B4-47B7-8A54-3B4B19231F5A");
if (pInArgList == NULL || pOutArgList == NULL)
{
return false;
}
//Accessing the input raster
Progress* pProgress = pInArgList->getPlugInArgValue<Progress>(Executable::ProgressArg());
RasterElement* pCube = pInArgList->getPlugInArgValue<RasterElement>(Executable::DataElementArg());
FactoryResource<DataRequest> pRequest2;
pRequest2->setInterleaveFormat(BSQ);
DataAccessor pAcc2 = pCube->getDataAccessor(pRequest2.release());
//Checking if raster has been specified or ot
if (pCube == NULL)
{
std::string msg = "A raster cube must be specified.";
pStep->finalize(Message::Failure, msg);
if (pProgress != NULL)
{
pProgress->updateProgress(msg, 0, ERRORS);
}
return false;
}
RasterDataDescriptor* pDesc = static_cast<RasterDataDescriptor*>(pCube->getDataDescriptor());
VERIFY(pDesc != NULL);
AoiElement* pAoi = NULL;
if (isBatch())
{
pAoi = pInArgList->getPlugInArgValue<AoiElement>("AOI");
}
else
{
Service<ModelServices> pModel;
std::vector<DataElement*> pAois = pModel->getElements(pCube, TypeConverter::toString<AoiElement>());
if (!pAois.empty())
{
QStringList aoiNames("<none>");
for (std::vector<DataElement*>::iterator it = pAois.begin(); it != pAois.end(); ++it)
{
aoiNames << QString::fromStdString((*it)->getName());
}
QString aoi = QInputDialog::getItem(Service<DesktopServices>()->getMainWidget(),
"Select an AOI", "Select an AOI for processing", aoiNames);
// select AOI
if (aoi != "<none>")
{
std::string strAoi = aoi.toStdString();
for (std::vector<DataElement*>::iterator it = pAois.begin(); it != pAois.end(); ++it)
{
if ((*it)->getName() == strAoi)
{
pAoi = static_cast<AoiElement*>(*it);
break;
}
}
if (pAoi == NULL)
{
std::string msg = "Invalid AOI.";
pStep->finalize(Message::Failure, msg);
if (pProgress != NULL)
{
pProgress->updateProgress(msg, 0, ERRORS);
}
return false;
}
}
}
} // end if
int count_vijay=0; //This count keeps tab of the number of AOI points
int rowSize9=pDesc->getRowCount(); //Size of height of input raster
int colSize9=pDesc->getColumnCount(); //Size of width of input raster
int zero=0; //The zero variable is used for max,min function (complication)
const BitMask* pPoints = (pAoi == NULL) ? NULL : pAoi->getSelectedPoints(); //Setting up to access/recognize the AOI points in the input
FactoryResource<DataRequest> pRequest;
pRequest->setInterleaveFormat(BSQ);
DataAccessor pAcc = pCube->getDataAccessor(pRequest.release());
QStringList Names("0.0000001");
QString value = QInputDialog::getItem(Service<DesktopServices>()->getMainWidget(),
"Input a PFA value", "Input a PFA value (0.0000001 or 0.00000001)", Names);
std::string strAoi = value.toStdString();
std::istringstream stm;
stm.str(strAoi);
double long PFA_k = 0.0;
PFA_k=::atof(strAoi.c_str());
//The 4 constants below are the default values for the window thats scans through the raster to calculate the local statistics
int DEPTH1 = 10;
int DEPTH2 = 10;
int DEPTH3 = 1;
int DEPTH4 = 1;
//Window width input
QStringList Names1("10");
QString value1 = QInputDialog::getItem(Service<DesktopServices>()->getMainWidget(),
"Input the size of the window width", "Input the size of the window width in terms of the number of pixels (eg. 10)", Names1);
std::string strAoi1 = value1.toStdString();
std::istringstream stm1;
stm1.str(strAoi1);
DEPTH1=::atof(strAoi1.c_str());
//Window height input
QStringList Names2("10");
QString value2 = QInputDialog::getItem(Service<DesktopServices>()->getMainWidget(),
"Input the size of the window height", "Input the size of the window height in terms of the number of pixels (eg. 10)", Names2);
std::string strAoi2 = value2.toStdString();
std::istringstream stm2;
stm2.str(strAoi2);
DEPTH2=::atof(strAoi2.c_str());
//Gaurd width input
QStringList Names3("1");
QString value3 = QInputDialog::getItem(Service<DesktopServices>()->getMainWidget(),
"Input the size of the gaurd width", "Input the size of the guard width in terms of the number of pixels (eg. 1)", Names3);
std::string strAoi3 = value3.toStdString();
std::istringstream stm3;
stm3.str(strAoi3);
DEPTH3=::atof(strAoi3.c_str());
//Gaurd height input
QStringList Names4("1");
QString value4 = QInputDialog::getItem(Service<DesktopServices>()->getMainWidget(),
"Input the size of the guard height", "Input the size of the guard height in terms of the number of pixels (eg. 1)", Names4);
std::string strAoi4 = value4.toStdString();
std::istringstream stm4;
stm4.str(strAoi4);
stm4 >> DEPTH4;
DEPTH4=::atof(strAoi4.c_str());
//Threshold value input for the morph detector
QStringList Names_m("3");
QString value_m = QInputDialog::getItem(Service<DesktopServices>()->getMainWidget(),
"Input a threshold value for Morph", "Input a threshold", Names_m);
std::string strAoi_m = value_m.toStdString();
std::istringstream stm_m;
stm_m.str(strAoi_m);
int threshold_m = 0;
stm_m >> threshold_m;
//Longitude input value
QStringList Names_x("0");
QString value_x = QInputDialog::getItem(Service<DesktopServices>()->getMainWidget(),"Input a longitude value", "Input a longitudal value", Names_x);
std::string strAoi_x = value_x.toStdString();
std::istringstream stm_x;
stm_x.str(strAoi_x);
long x_geo_code = 0;
stm_x >> x_geo_code;
//Latitude value
QStringList Names_y("0");
QString value_y = QInputDialog::getItem(Service<DesktopServices>()->getMainWidget(),"Input a latitude value", "Input a latitude value", Names_y);
std::string strAoi_y = value_y.toStdString();
std::istringstream stm_y;
stm_y.str(strAoi_y);
long y_geo_code = 0;
stm_y >> y_geo_code;
//Satellite altitude value
QStringList Names_a("0");
QString value_a = QInputDialog::getItem(Service<DesktopServices>()->getMainWidget(),"Input the satellite altitude", "Input a altitude value (km)", Names_a);
std::string strAoi_a = value_a.toStdString();
std::istringstream stm_a;
stm_a.str(strAoi_a);
long altitude = 0;
stm_a >> altitude;
//Satellite velocity value
QStringList Names_v("0");
QString value_v = QInputDialog::getItem(Service<DesktopServices>()->getMainWidget(),"Input the satellite velocity", "Input a velocity value (km/s)", Names_v);
std::string strAoi_v = value_v.toStdString();
std::istringstream stm_v;
stm_v.str(strAoi_v);
double velocity = 0;
stm_v >> velocity;
//Threshold value for radon detector
QStringList Names_e("25.0");
QString value_e = QInputDialog::getItem(Service<DesktopServices>()->getMainWidget(),"Input a threshold value", "Input a threshold (eg. 25)", Names_e);
std::string strAoi_e = value_e.toStdString();
std::istringstream stm_e;
stm_e.str(strAoi_e);
double long threshold27 = 0.0;
stm_e >> threshold27;
//Window size around each target AOI
QStringList Names_z("100");
QString value_z = QInputDialog::getItem(Service<DesktopServices>()->getMainWidget(),"Input a window pixels value", "Window size around each target AOI", Names_z);
std::string strAoi_z = value_z.toStdString();
std::istringstream stm_z;
stm_z.str(strAoi_z);
int DEPTH11 = 100;
stm_z >> DEPTH11;
//Starting of loop
//Main for loop to loop around the entire input raster
for (int row = 0; row < rowSize9; ++row)
{
//Once clicked on the cancel button, the operation will be aborted
if (isAborted())
{
std::string msg = getName() + " has been aborted.";
pStep->finalize(Message::Abort, msg);
if (pProgress != NULL)
{
pProgress->updateProgress(msg, 0, ABORT);
}
return false;
}
//If the input raster has not been accessed properly, ABORT.
if (!pAcc.isValid())
{
std::string msg = "Unable to access the cube data.";
pStep->finalize(Message::Failure, msg);
if (pProgress != NULL)
{
pProgress->updateProgress(msg, 0, ERRORS);
}
return false;
}
//This updates the progress bar as each row has been iterated
if (pProgress != NULL)
{
pProgress->updateProgress("Calculating statistics", row * 100 / pDesc->getRowCount(), NORMAL);
}
//This is the internal loop that iterates through the columns
for (int col = 0; col < colSize9; ++col)
{
pAcc->toPixel(row,col); //Data accessor for the particular pixel value of th einput raster
if (pPoints == NULL || pPoints->getPixel(col, row)) //If an AOI point has been detected, the inside operations will performed
{
count_vijay+=1; //Counter that keep a tab of the number of AOI points
int startRow = max(row-DEPTH11,zero); //The start row of the square window of each AOI
int endRow = min(row+DEPTH11,rowSize9-1); //The end row of the square window of each AOI
int startCol=max(zero,col-DEPTH11); //The start column of the square window of each AOI
int endCol=min(col+DEPTH11, colSize9-1); //The end column of the square window of each AOI
int depth_row=endRow-startRow+1; //The number of pixels of the actual window height, since at the edges of the image, a smaller window size needs to be taken
int depth_col=endCol-startCol+1; //The number of pixels of the window width.
//The above two variables will be used to find the mean of all the pixels in the input raster. This is used to mask the regions of the target ships with the mean value.
unsigned int total5_1=0; //The variable will store the sum of all the pixels in the input raster
int count5_1 = 0; //This variable will count the total number of pixels in the input raster
//Initiating the 2D dynamic table that will hold the pixel values of the input raster
double **res=0;
res=new double *[depth_row];
for(int i=0; i<depth_row; i++)
{
res[i]=new double[depth_col];
}
for(int k=startRow; k<endRow+1; k++)
{
for(int l=startCol; l<endCol+1; l++)
{
pAcc->toPixel(k,l);
double pixel=pAcc->getColumnAsDouble();
total5_1+=pixel;
count5_1+=1;
res[k-startRow][l-startCol]=pixel; //The pixel locations need to be converted from the image location to the table location
}
}
double mean5 = total5_1/count5_1; //Mean value that will be used to mask the target ship pixels
//Initiating a 2D dynmaic table that will hold the outout pixel values that will be processed from the priliminary CFAR algorithm
double **res1=0;
res1=new double *[depth_row];
for(int i=0; i<depth_row; i++)
{
res1[i]=new double[depth_col];
}
for(int k=startRow; k<endRow+1; k++)
{
for(int l=startCol; l<endCol+1; l++)
{
res1[k-startRow][l-startCol]=0.0; //The pixel values in this table are initiated to a "0" value first
}
}
//Within in the square window, the local statistics will have to be calculated for each pixel in the window, to detect any large pixel values relative to the environment.
//For this purpose, a smaller window is initiated. The variables below are used in that smaller window.
int eastCol = 0; //Right side of the smaller window
int northRow = 0; //Top side of the window
int westCol = 0; // Left side of the window
int southRow = 0; //Down side of the window
double zstatistic = 0; //This is the z-statistic that will be used to calculate the gaussian distribution based threshold
double total = 0.0; //The sum total pixel value of the smaller window
double total_sum = 0.0; //The sum total sqaure pixel value of the smaller window (for standard deviation calculation)
double mean = 0.0; //The mean of the local window
double std = 0.0; //The standard deviation of the local window
int prevCol = 0; //The previous column to the current one
int prevRow = 0; //the previous row to the current one
int nextCol = 0; //the next col to the current one
int nextRow = 0; //the next row to the current one
int DEPTH = 10; //This is the size of the window
int count=0; //Count keeps tab of the total number of pixels in the window
double threshold = 3.0; //This is the fixed threshold value that will be compared against the z-statistic of the local window
//CFAR algorithm
//This is the for loop for the calculations related to the localized window
for (int row = startRow; row < endRow+1; ++row)
{
if (isAborted())
{
std::string msg = getName() + " has been aborted.";
pStep->finalize(Message::Abort, msg);
if (pProgress != NULL)
{
pProgress->updateProgress(msg, 0, ABORT);
}
return false;
}
if (!pAcc.isValid())
{
std::string msg = "Unable to access the cube data.";
pStep->finalize(Message::Failure, msg);
if (pProgress != NULL)
{
pProgress->updateProgress(msg, 0, ERRORS);
}
return false;
}
if (pProgress != NULL)
{
pProgress->updateProgress("Calculating statistics", 0.3*row * 100 / pDesc->getRowCount(), NORMAL);
}
for (int col = startCol; col < endCol+1; ++col)
{
//THESE VALUES WERE CHANGED FROM ROWSIZE9 AND 0 TO STARTCOL, ENDCOL
westCol=max(col-DEPTH,startCol);
northRow=max(row-DEPTH,startRow);
eastCol=min(endCol,col+DEPTH);
southRow=min(endRow,row+DEPTH);
prevCol=max(col-1,startCol);
prevRow=max(row-1,startRow);
nextCol=min(col+1,endCol);
nextRow=min(row+1,endRow);
pAcc2->toPixel(northRow,westCol);
for(int row1=northRow; row1 < southRow+1; ++row1)
{
for (int col1=westCol; col1 < eastCol+1; ++col1)
{
//If the pixel is at the gaurd window, then do not take any readings of the pixel value
if(((col1==prevCol)&&(row1==prevRow)) ||
((col1==col)&&(row1==prevRow)) ||
((col1==nextCol)&&(row1==prevRow)) ||
((col1==prevCol)&&(row1==row)) ||
((col1==nextCol)&&(row1==row)) ||
((col1==prevCol)&&(row1==nextRow)) ||
((col1==col)&&(row1==nextRow)) ||
((col1==nextCol)&&(row1==nextRow)) ||
((col1==col)&&(row1==row)))
{
continue;
}
//If pixel is outside the gaurd region, then take readings of the pixel value
else
{
updateStatistics16(pAcc2->getColumnAsDouble(), total, total_sum, count);
}
pAcc2->nextColumn();
}
pAcc2->nextRow();
}
//Calculate the local mean and standard deviation of the local window
mean = total / count;
std = sqrt((total_sum / (1.0*count)) - (total*total/(1.0*count*count)));
pAcc2->toPixel(row,col);
zstatistic = (pAcc2->getColumnAsDouble()-mean)/std;
//If the z statistic of the local window is greater than the set threshold that the user inputted, then the pixel is a target.
//Hence, in the secondary binary 2D table, it has a 255 value.
//If the z statistic is less than the set threshold, the pixel is not a target. Hence, it will has a 0 value in the binary output.
if(zstatistic>threshold)
{
res1[row-startRow][col-startCol]+=255.0;
}
else
{
res1[row-startRow][col-startCol]+=0.0;
}
//All the variables are reset to zero for the next pixel reading
total = 0.0;
total_sum = 0.0;
mean = 0.0;
std = 0.0;
count=0;
}
}
//In thi for loop, if the binary 2D table has a value greater than 0, then it will take the mean value of the whole input raster. This masks the target ships.
for (int row = startRow; row < endRow+1; ++row)
{
for (int col = startCol; col < endCol+1; ++col)
{
if(res1[row-startRow][col-startCol]!=0)
{
res[row-startRow][col-startCol]=mean5;
}
else
continue;
}
}
//Radon Algorithm
int n = floor(depth_row/2+0.5); //Finds the mid-point of the raster height (for origin alligning)
int m = floor(depth_col/2+0.5); //Find the mid-point of the raster width
int rowsize_sq=depth_row*depth_row;
int colsize_sq=depth_col*depth_col;
int rhomax = ceil(sqrt((rowsize_sq*1.0)+(colsize_sq*1.0)));
int rc = floor(rhomax/2+0.5);
const int mt = 180;
//All the below variables are used in the radon transform
double costheta= 0.0;
double sintheta=0.0;
double a = 0.0;
double b=0.0;
int rho=0;
int ymax=0;
int ymin=0;
int xmax=0;
int xmin=0;
double x=0.0;
double y=0.0;
int xfloor;
int yfloor;
double xup=0.0;
double yup=0.0;
double xlow=0.0;
double ylow=0.0;
//The 2d dynamic table below will be used to store the radon transform
double **res3=0;
res3=new double *[rhomax+1];
for(int i=0; i<rhomax+1; i++)
{res3[i]=new double[mt];}
for(int k=0; k<rhomax+1; k++)
{
for(int l=0; l<180; l++)
res3[k][l]=0.0;
}
//This is the other 2d dynamic table taht will be used to store the radon transform
double **res7=0;
res7=new double *[rhomax+1];
for(int i=0; i<rhomax+1; i++)
{res7[i]=new double[mt];}
for(int k=0; k<rhomax+1; k++)
{
for(int l=0; l<180; l++)
res7[k][l]=0.0;
}
for(int t=1; t<46; t++)
{
if (pProgress != NULL)
{
pProgress->updateProgress("Calculating statistics", 30+0.25*0.2*t*100 / 45, NORMAL);
}
costheta=cos(1.0*t*pi10/180.0);
sintheta=sin(1.0*t*pi10/180.0);
a=-costheta/sintheta;
for(int r=1; r<rhomax+1; r++)
{
rho=r-rc;
b=1.0*rho/sintheta;
int f=floor((-a*m)+b+0.5);
int g=floor(a*m+b+0.5);
ymax=min(f,(n-1));
ymin=max(g,-n);
for(int y=ymin; y<ymax+1; y++)
{
x=1.0*(y-b)/a;
xfloor=floor(x);
xup=x-xfloor;
xlow=1-xup;
x=xfloor;
int l=x;
x=max(static_cast<int>(x),-m);
int q=x;
x=min(static_cast<int>(x),m-2);
int o = x;
double pixel1=res[y+n][o+m];
double pixel2=res[y+n][o+m+1];
res3[rhomax-r][mt-t-1]=res3[rhomax-r][mt-t-1]+xlow*pixel1;
res3[rhomax-r][mt-t-1]=res3[rhomax-r][mt-t-1]+xup*pixel2;
}
}
}
for(int t=46; t<91; t++)
{
if (pProgress != NULL)
{
pProgress->updateProgress("Calculating statistics", 35+0.25*0.2*t*100 / 45, NORMAL);
}
costheta=cos(1.0*t*pi10/180.0);
sintheta=sin(1.0*t*pi10/180.0);
a=-costheta/sintheta;
for(int r=1; r<rhomax+1; r++)
{
rho=r-rc;
b=rho/sintheta;
int f=floor(1.0*(-n-b)/a+0.5);
int g=floor(1.0*(n-b)/a+0.5);
xmax=min(f,m-1);
xmin=max(g,-m);
for(int x=xmin; x<xmax+1; x++)
{
y=a*x+b;
yfloor=floor(y);
yup=y-yfloor;
ylow=1-yup;
y=yfloor;
int l=y;
y=max(static_cast<int>(y),-n);
int q=y;
y=min(static_cast<int>(y),n-2);
int p = y;
double pixel1=res[p+n][x+m];
double pixel2=res[p+n+1][x+m];
res3[rhomax-r][mt-t-1]=res3[rhomax-r][mt-t-1]+xlow*pixel1;
res3[rhomax-r][mt-t-1]=res3[rhomax-r][mt-t-1]+xup*pixel2;
}
}
}
for(int t=91; t<136; t++)
{
if (pProgress != NULL)
{
pProgress->updateProgress("Calculating statistics", 40+0.25*0.2*t*100 / 45, NORMAL);
}
costheta=cos(1.0*t*pi10/180.0);
sintheta=sin(1.0*t*pi10/180.0);
a=-costheta/sintheta;
for(int r=1; r<rhomax+1; r++)
{
rho=r-rc;
b=rho/sintheta;
int f=floor((n-b)/a+0.5);
int g=floor((-n-b)/a+0.5);
xmax=min(f,m-1);
xmin=max(g,-m);
for(int x=xmin; x<xmax+1; x++)
{
y=a*x+b;
yfloor=floor(y);
yup=y-yfloor;
ylow=1-yup;
y=yfloor;
int l=y;
y=max(static_cast<int>(y),-n);
int q=y;
y=min(static_cast<int>(y),n-2);
int f=y;
double pixel1=res[f+n][x+m];
double pixel2=res[f+n+1][x+m];
res3[rhomax-r][mt-t-1]=res3[rhomax-r][mt-t-1]+xlow*pixel1;
res3[rhomax-r][mt-t-1]=res3[rhomax-r][mt-t-1]+xup*pixel2;
}
}
}
for(int t=136; t<180; t++)
{
if (pProgress != NULL)
{
pProgress->updateProgress("Calculating statistics", 45+0.25*0.2*t*100 / 45, NORMAL);
}
costheta=cos(t*pi10/180.0);
sintheta=sin(t*pi10/180.0);
a=-costheta/sintheta;
for(int r=1; r<rhomax+1; r++)
{
rho=r-rc;
b=rho/sintheta;
int f=floor(1.0*(-a*m)+b+0.5);
int g=floor(a*m+b+0.5);
ymax=min(g,n-1);
ymin=max(f,-n);
for(int y=ymin; y<ymax+1; y++)
{
x=1.0*(y-b)/a;
xfloor=floor(x);
xup=x-xfloor;
xlow=1-xup;
x=xfloor;
int l=x;
x=max(static_cast<int>(x),-m);
int q=x;
x=min(static_cast<int>(x),m-2);
int w=x;
double pixel1=res[y+n][w+m];
double pixel2=res[y+n][w+m+1];
res3[rhomax-r][mt-t-1]=res3[rhomax-r][mt-t-1]+xlow*pixel1;
res3[rhomax-r][mt-t-1]=res3[rhomax-r][mt-t-1]+xup*pixel2;
}
}
}
int r=0;
int rhooffset=floor((rhomax-depth_col)/2+0.5);
for(int x=1; x<depth_col+1; x++)
{
r=x+rhooffset;
r=rhomax-r+1;
for(int y=1; y<depth_row+1; y++)
{
double pixel=res[y-1][x-1];
res3[r-1][179]=res3[r-1][179]+pixel;
}
}
int rhoaxis=rhomax+1;
int cool = -10000;
int cool2 = 10000;
int rollcol=0;
int colcol=0;
for(int row=0; row<rhoaxis; row++)
{
for(int col=0; col<180; col++)
{
if (res3[row][col]>cool)
{
cool=res3[row][col];
}
if(res3[row][col]<cool2)
{
cool2=res3[row][col];
colcol=col;
rollcol=row;
}
}
}
//This the final loop that scales down the values and stores them in res7.
for(int row=0; row<rhoaxis; row++)
{
for(int col=0; col<180; col++)
{
res7[row][col]=(res3[row][col])*255.0/cool;
}
}
//K distribution algorithm
//A 2D table that stores the values that are manipulated from the input raster
double **res4=0;
res4=new double *[depth_row];
for(int i=0; i<depth_row; i++)
{res4[i]=new double[depth_col];}
for(int k=0; k<depth_row; k++)
{
for(int l=0; l<depth_col; l++)
{
res4[k][l]=0.0;
}
}
//Values that are used to calculate the local region of the K distribution algorithm
int eastCol_k = 0;
int northRow_k = 0;
int westCol_k = 0;
int southRow_k = 0;
double zstatistic_k = 0;
double total_k = 0.0;
double total_sum_k = 0.0;
double mean_k = 0.0;
double std_k = 0.0;
int prevCol_k = 0;
int prevRow_k = 0;
int nextCol_k = 0;
int nextRow_k = 0;
int count_k=0;
double long threshold_k = 100000.0;
double look_table1[24][6];
for(int i=0; i<24; i++)
{
for(int j=0; j<3; j++)
{
look_table1[i][j]=0.0;
}
}
//Look-up table with the threshold values
if (PFA_k==0.0000001)
{
look_table1[0][0]=1.0;
look_table1[0][1]=5.0;
look_table1[0][2]=32.3372530103729330;
look_table1[1][0]=1.0;
look_table1[1][1]=10.0;
look_table1[1][2]=25.0723580041031010;
look_table1[2][0]=1.0;
look_table1[2][1]=15.0;
look_table1[2][2]=22.3991160013551250;
look_table1[3][0]=1.0;
look_table1[3][1]=20.0;
look_table1[3][2]=20.9821949998985920;
look_table1[4][1]=1.0;
look_table1[4][2]=40.0;
look_table1[5][3]=18.7055519975583020;
look_table1[5][1]=1.0;
look_table1[5][2]=90.0;
look_table1[5][3]=18.7055519975583020;
look_table1[6][0]=2.0;
look_table1[6][1]=5.0;
look_table1[6][2]=20.2619339991581950;
look_table1[7][0]=2.0;
look_table1[7][1]=10.0;
look_table1[7][2]=15.4860609951617470;
look_table1[8][0]=2.0;
look_table1[8][1]=15.0;
look_table1[8][2]=13.7276789964777210;
look_table1[9][0]=2.0;
look_table1[9][1]=20.0;
look_table1[9][2]=12.7942589971762930;
look_table1[10][0]=2.0;
look_table1[10][1]=40.0;
look_table1[10][2]=11.2895769983023970;
look_table1[11][0]=2.0;
look_table1[11][1]=90.0;
look_table1[11][2]=10.3695259989909640;
look_table1[12][0]=3.0;
look_table1[12][1]=5.0;
look_table1[12][2]=15.9102209948443050;
look_table1[13][0]=3.0;
look_table1[13][1]=10.0;
look_table1[13][2]=12.0443629977375150;
look_table1[14][0]=3.0;
look_table1[14][1]=15.0;
look_table1[14][2]=10.6203179988032710;
look_table1[15][0]=3.0;
look_table1[15][1]=20.0;
look_table1[15][2]=9.8635499993696367;
look_table1[16][0]=3.0;
look_table1[16][1]=40.0;
look_table1[16][2]=8.6407550002847771;
look_table1[17][0]=3.0;
look_table1[17][1]=90.0;
look_table1[17][2]=7.8893780007488568;
look_table1[18][0]=4.0;
look_table1[18][1]=5.0;
look_table1[18][2]=13.6166519965608130;
look_table1[19][0]=4.0;
look_table1[19][1]=10.0;
look_table1[19][2]=10.2336029990926890;
look_table1[20][0]=4.0;
look_table1[20][1]=15.0;
look_table1[20][2]=10.6203179988032710;
look_table1[21][0]=4.0;
look_table1[21][1]=20.0;
look_table1[21][2]=8.9868610000257512;
look_table1[22][0]=4.0;
look_table1[22][1]=40.0;
look_table1[22][2]=7.2502150006595159;
look_table1[23][0]=4.0;
look_table1[23][1]=90.0;
look_table1[23][2]=6.5879140005669408;
}
if (PFA_k==0.00000001)
{
look_table1[0][0]=1.0;
look_table1[0][1]=5.0;
look_table1[0][2]=20.0000019988889410;
look_table1[1][0]=1.0;
look_table1[1][1]=10.0;
look_table1[1][2]=20.0000019988889410;
look_table1[2][0]=1.0;
look_table1[2][1]=15.0;
look_table1[2][2]=20.0000019988889410;
look_table1[3][0]=1.0;
look_table1[3][1]=20.0;
look_table1[3][2]=20.0000019988889410;
look_table1[4][1]=1.0;
look_table1[4][2]=40.0;
look_table1[5][3]=20.0000019988889410;
look_table1[5][1]=1.0;
look_table1[5][2]=90.0;
look_table1[5][3]=20.0000019988889410;
look_table1[6][0]=2.0;
look_table1[6][1]=5.0;
look_table1[6][2]=18.3243529971664460;
look_table1[7][0]=2.0;
look_table1[7][1]=10.0;
look_table1[7][2]=18.3243529971664460;
look_table1[8][0]=2.0;
look_table1[8][1]=15.0;
look_table1[8][2]=16.0869139948664570;
look_table1[9][0]=2.0;
look_table1[9][1]=20.0;
look_table1[9][2]=14.8998299956004820;