-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCell_Segmentation.cpp
More file actions
2204 lines (2046 loc) · 84.7 KB
/
Cell_Segmentation.cpp
File metadata and controls
2204 lines (2046 loc) · 84.7 KB
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
/* cellSegmentation.cpp
* It aims to automatically segment cells;
* 2014-10-12 :by Xiang Li (lindbergh.li@gmail.com);
*/
#ifndef __CELLSEGMENTATION_PLUGIN_H__
#define __CELLSEGMENTATION_PLUGIN_H__
#pragma region "includes and constants"
#include <QtGui>
#include <v3d_interface.h>
#include <sstream>
#include <math.h>
#include <iostream>
#include <string>
#include "v3d_message.h"
#include "cellSegmentation_plugin.h"
#include <vector>
#include <cassert>
#include <math.h>
#include "string"
#include "sstream"
#include <iostream>
#include <cstdlib>
#include <fstream>
#include <algorithm>
#include <basic_landmark.h>
#include "compute_win_pca.h"
#include "convert_type2uint8.h"
#include <time.h>
using namespace std;
const int const_length_histogram = 256;
const double const_max_voxelValue = 255;
const int const_count_neighbors = 26; //27 directions -1;
const double default_threshold_global = 20; //a small enough value for the last resort;
const int default_threshold_regionSize = 8; //cube of 2 voxels length;
const double const_infinitesimal = 0.000000001;
#define INF 1E9
#define NINF -1E9
#define PI 3.14159265
enum enum_shape_t { sphere, cube };
#pragma endregion
#pragma region "dialogInitialization"
// This is the dialog window run when the code is executed. *
class dialogRun :public QDialog
{
Q_OBJECT
public:
// This is the creation of the initial dialog box upon the start of the program call
dialogRun(V3DPluginCallback2 &V3DPluginCallback2_currentCallback, QWidget *QWidget_parent, int int_channelDim)
{
//channel;
QStringList QStringList_channel_items;
if (int_channelDim == 1) //if number of channels specified is 1, append "1" to the string list
{
QStringList_channel_items << "1";
}
else if (int_channelDim == 3)
{
QStringList_channel_items << "1 - red";
QStringList_channel_items << "2 - green";
QStringList_channel_items << "3 - blue";
}
else // adds a list of numbers from 1 to the specified number of channels
{
for (int i = 1; i <= int_channelDim; i++)
{
QStringList_channel_items << QString().setNum(i);
}
}
QComboBox_channel_selection = new QComboBox();
QComboBox_channel_selection->addItems(QStringList_channel_items);
if (QStringList_channel_items.size() > 1)
{
QComboBox_channel_selection->setCurrentIndex(0);
}
QGroupBox *QGroupBox_channel_main = new QGroupBox("Color channel");
QGroupBox_channel_main->setStyle(new QWindowsStyle());
QGridLayout *QGridLayout_channel_main = new QGridLayout();
QGroupBox_channel_main->setStyle(new QWindowsStyle());
QGridLayout_channel_main->addWidget(QComboBox_channel_selection, 1, 1, 1, 1);
QGroupBox_channel_main->setLayout(QGridLayout_channel_main);
//exemplar;
QGroupBox *QGroupBox_exemplar_main = new QGroupBox("Exemplar definition");
QGridLayout *QGridLayout_exemplar_main = new QGridLayout();
QLabel* QLabel_exemplar_maxMovement1 = new QLabel(QObject::tr("Max movement from\nmass center"));
QLineEdit_exemplar_maxMovement1 = new QLineEdit("3", QWidget_parent);
QLabel* QLabel_exemplar_maxMovement2 = new QLabel(QObject::tr("Max movement from\nmarker position"));
QLineEdit_exemplar_maxMovement2 = new QLineEdit("8", QWidget_parent);
QGridLayout_exemplar_main->addWidget(QLabel_exemplar_maxMovement1, 1, 1, 1, 1);
QGridLayout_exemplar_main->addWidget(QLineEdit_exemplar_maxMovement1, 1, 2, 1, 1);
QGridLayout_exemplar_main->addWidget(QLabel_exemplar_maxMovement2, 1, 3, 1, 1);
QGridLayout_exemplar_main->addWidget(QLineEdit_exemplar_maxMovement2, 1, 4, 1, 1);
QGroupBox_exemplar_main->setLayout(QGridLayout_exemplar_main);
//shape;
QGroupBox *QGroupBox_shape_main = new QGroupBox("Geometry stat");
QGridLayout *QGridLayout_shape_main = new QGridLayout();
QRadioButton_shape_sphere = new QRadioButton("sphere-like", QWidget_parent);
QRadioButton_shape_sphere->setChecked(true);
QRadioButton_shape_cube = new QRadioButton("cube-like", QWidget_parent);
QRadioButton_shape_cube->setChecked(false);
QGridLayout_shape_main->addWidget(QRadioButton_shape_sphere, 1, 1, 1, 1);
QGridLayout_shape_main->addWidget(QRadioButton_shape_cube, 1, 2, 1, 1);
QLabel* QLabel_shape_delta = new QLabel(QObject::tr("max anisotropic\ndeviation:"));
QLineEdit_Shape_delta = new QLineEdit("1", QWidget_parent);
QGridLayout_shape_main->addWidget(QLabel_shape_delta, 1, 3, 1, 1);
QGridLayout_shape_main->addWidget(QLineEdit_Shape_delta, 1, 4, 1, 1);
QLabel* QLabel_shape_thresholdRegionSize = new QLabel(QObject::tr("Min region size\nvs. exemplar ratio:"));
QLineEdit_shape_thresholdRegionSize = new QLineEdit("0.1", QWidget_parent);
QLabel* QLabel_shape_uThresholdRegionSize = new QLabel(QObject::tr("Max region size\nvs. exemplar ratio:"));
QLineEdit_shape_uThresholdRegionSize = new QLineEdit("20", QWidget_parent);
QGridLayout_shape_main->addWidget(QLabel_shape_thresholdRegionSize, 2, 1, 1, 1);
QGridLayout_shape_main->addWidget(QLineEdit_shape_thresholdRegionSize, 2, 2, 1, 1);
QGridLayout_shape_main->addWidget(QLabel_shape_uThresholdRegionSize, 2, 3, 1, 1);
QGridLayout_shape_main->addWidget(QLineEdit_shape_uThresholdRegionSize, 2, 4, 1, 1);
QGroupBox_shape_main->setLayout(QGridLayout_shape_main);
//control;
QPushButton *QPushButton_control_start = new QPushButton(QObject::tr("Run"));
QPushButton *QPushButton_control_close = new QPushButton(QObject::tr("Close"));
QWidget* QWidget_control_bar = new QWidget();
QGridLayout* QGridLayout_control_bar = new QGridLayout();
QGridLayout_control_bar->addWidget(QPushButton_control_start, 1, 1, 1, 1);
QGridLayout_control_bar->addWidget(QPushButton_control_close, 1, 2, 1, 1);
QWidget_control_bar->setLayout(QGridLayout_control_bar);
//main panel;
QGridLayout *QGridLayout_main = new QGridLayout();
QGridLayout_main->addWidget(QGroupBox_channel_main);
QGridLayout_main->addWidget(QGroupBox_shape_main);
QGridLayout_main->addWidget(QGroupBox_exemplar_main);
QGridLayout_main->addWidget(QWidget_control_bar);
setLayout(QGridLayout_main);
setWindowTitle(QString("cellSegmentation: quickFind"));
//event evoking;
connect(QPushButton_control_start, SIGNAL(clicked()), this, SLOT(_slot_start()));
connect(QPushButton_control_close, SIGNAL(clicked()), this, SLOT(reject()));
update();
}
~dialogRun() {}
QComboBox* QComboBox_channel_selection;
V3DLONG channel_idx_selection;
QLineEdit* QLineEdit_Shape_delta;
QLineEdit* QLineEdit_shape_thresholdRegionSize;
QLineEdit* QLineEdit_shape_uThresholdRegionSize;
QLineEdit* QLineEdit_exemplar_maxMovement1;
QLineEdit* QLineEdit_exemplar_maxMovement2;
QRadioButton* QRadioButton_shape_sphere;
QRadioButton* QRadioButton_shape_cube;
enum_shape_t shape_type_selection;
double shape_para_delta;
double shape_multiplier_thresholdRegionSize;
double shape_multiplier_uThresholdRegionSize;
V3DLONG exemplar_maxMovement1;
V3DLONG exemplar_maxMovement2;
public slots:
//////////////////////////////?
void _slot_start()
{
channel_idx_selection = QComboBox_channel_selection->currentIndex() + 1;
shape_para_delta = this->QLineEdit_Shape_delta->text().toDouble();
shape_multiplier_thresholdRegionSize = this->QLineEdit_shape_thresholdRegionSize->text().toDouble();
shape_multiplier_uThresholdRegionSize = this->QLineEdit_shape_uThresholdRegionSize->text().toDouble();
exemplar_maxMovement1 = this->QLineEdit_exemplar_maxMovement1->text().toUInt();
exemplar_maxMovement2 = this->QLineEdit_exemplar_maxMovement2->text().toUInt();
if (this->QRadioButton_shape_sphere->isChecked())
{
this->shape_type_selection = sphere;
}
else if (this->QRadioButton_shape_cube->isChecked())
{
this->shape_type_selection = cube;
}
accept();
}
};
#pragma endregion
class cellSegmentation :public QObject, public V3DPluginInterface2_1
{
public:
#pragma region "class: class_segmentationMain"
class class_segmentationMain
{
#pragma region "class member"
public:
struct double3D // Stores x, y, z - used to store 3D coordinates
{
double x; double y; double z;
double3D(double _x = 0, double _y = 0, double _z = 0) { x = _x; y = _y; z = _z; }
};
struct long3D
{
V3DLONG x;
V3DLONG y;
V3DLONG z;
long3D(V3DLONG _x = 0, V3DLONG _y = 0, V3DLONG _z = 0) { x = _x; y = _y; z = _z; }
};
//constant
vector<V3DLONG> poss_neighborRelative; // positions of adjacent points in 1D coordinates
vector<double3D> point_neighborRelative; // positions of adjacent points in 3D coordinates
vector<vector<V3DLONG> > colors_simpleTable;
//Input or directly derived;
bool is_initialized;
unsigned char* Image1D_page;
unsigned char* Image1D_mask;
unsigned char*** Image3D_page;
V3DLONG dim_X;
V3DLONG dim_Y;
V3DLONG dim_Z;
V3DLONG size_page;
V3DLONG size_page3;
V3DLONG offset_channel;
V3DLONG offset_Z;
V3DLONG offset_Y;
int idx_channel;
int idx_shape;
double threshold_deltaShapeStat;
double multiplier_thresholdRegionSize;
double multiplier_uThresholdRegionSize;
V3DLONG max_movment1;
V3DLONG max_movment2;
QString name_currentWindow;
//Exemplar (or learn from it);
unsigned char* Image1D_exemplar;
//segmentation;
vector<vector<V3DLONG> > possVct_segmentationResult;
//vector<vector<V3DLONG> > possVct_segmentationResultMerged;
vector<vector<V3DLONG> > possVct_seed;
unsigned char* Image1D_segmentationResult;
LandmarkList LandmarkList_segmentationResult;
vector<V3DLONG> poss_segmentationResultCenter;
//vector<V3DLONG> poss_segmentationResultCenterMerged;
#pragma endregion
class_segmentationMain() { is_initialized = false; }
~class_segmentationMain() {}
///////////////////////////////////////////////////////////////////
/// Execution of Cell Counting Algorithm ///
/// Algorithm is run after the dialogRun code, so most of the ///
/// included variables are obtained through user determination. ///
///////////////////////////////////////////////////////////////////
// *Note: "idx" likely stands for "index"
#pragma region "control-run"
bool control_run(unsigned char* _Image1D_original, V3DLONG _dim_X, V3DLONG _dim_Y, V3DLONG _dim_Z,
int _idx_channel, LandmarkList _LandmarkList_exemplar, int _idx_shape, double _threshold_deltaShapeStat,
double _multiplier_thresholdRegionSize, double _multiplier_uThresholdRegionSize, QString _name_currentWindow,
V3DLONG _maxMovement1, V3DLONG _maxMovement2)
{
//if (!this->is_initialized) // Temporally solution for the "parameter window not popped up" problem;
{ // unecessary block braces, used only because of commented out code above
// This block saves image data to the static variables
this->dim_X = _dim_X; this->dim_Y = _dim_Y; this->dim_Z = _dim_Z;
this->idx_channel = _idx_channel; // Index channel from dialog box
this->size_page = dim_X * dim_Y * dim_Z;
this->size_page3 = this->size_page + this->size_page + this->size_page;
this->offset_channel = (idx_channel - 1)*size_page; // position where channel begins being stored within Image1D_page
this->offset_Z = dim_X * dim_Y; // used for 1D-to-3D representation conversions
this->offset_Y = dim_X; // used for 1D-to-3D representation conversions
this->Image1D_page = memory_allocate_uchar1D(this->size_page);
this->Image1D_mask = memory_allocate_uchar1D(this->size_page);
this->Image3D_page = memory_allocate_uchar3D(this->dim_Y, this->dim_X, this->dim_Z); //tricky!
this->Image1D_segmentationResult = memory_allocate_uchar1D(this->size_page3);
this->Image1D_exemplar = memory_allocate_uchar1D(this->size_page3);
/* Loads the channel from the original image into the single-channel Image1d_page, as well as a 3D representation and an array that stores 3 copies of Image1d_page. For example, if the selected channel is the blue channel, then only the blue channel from the original image is stored in Image1d_page and the other structures. */
for (V3DLONG i = 0; i < this->size_page; i++)
{
this->Image1D_page[i] = _Image1D_original[i + offset_channel];
vector<V3DLONG> xyz_i = this->index2Coordinate(i);
// Image1D_page is stored as a 3D image
this->Image3D_page[xyz_i[2]][xyz_i[1]][xyz_i[0]] = this->Image1D_page[i];
// Image1D_page is stored 3 times in a row into Image1D_segmentationResult
this->Image1D_segmentationResult[i] = this->Image1D_page[i];
this->Image1D_segmentationResult[i + size_page] = this->Image1D_page[i];
this->Image1D_segmentationResult[i + size_page + size_page] = this->Image1D_page[i];
}
// This block saves user-defined parameters obtained through the dialog box into the static variables.
memset(this->Image1D_mask, const_max_voxelValue, this->size_page); // Sets all values in Image1D_mask to const_max_voxelValue (255)
this->idx_shape = _idx_shape;
this->categorizeVoxelsByValue();
this->threshold_deltaShapeStat = _threshold_deltaShapeStat;
this->multiplier_thresholdRegionSize = _multiplier_thresholdRegionSize;
this->multiplier_uThresholdRegionSize = _multiplier_uThresholdRegionSize;
this->name_currentWindow = _name_currentWindow;
this->max_movment1 = _maxMovement1 * _maxMovement1;
this->max_movment2 = _maxMovement2 * _maxMovement2;
this->is_initialized = false;
} // unecessary block braces, used only because of commented out code above
// Declaration of local variables, initialization of some constant global variables
vector<double> thresholds_valueChangeRatio;
vector<V3DLONG> thresholds_voxelValue;
vector<V3DLONG> thresholds_regionSize;
vector<V3DLONG> uThresholds_regionSize;
vector<V3DLONG> thresholds_radius;
this->initializeConstants();
vector<vector<V3DLONG> > possVct_exemplarRegion;
vector<vector<vector<double> > > valueVctVct_exemplarShapeStat;
vector<V3DLONG> poss_exemplar = landMarkList2IndexList(_LandmarkList_exemplar); // sends LandmarkList to vector poss_exemplar, where each element in the vector corresponds to the 1D coordinate representation of the location
V3DLONG count_exemplar = poss_exemplar.size();
vector<V3DLONG> poss_exemplarNew;
// for each landmark, do the following
for (V3DLONG idx_exemplar = 0; idx_exemplar < count_exemplar; idx_exemplar++)
{
V3DLONG pos_exemplar = poss_exemplar[idx_exemplar];
if (this->Image1D_mask[pos_exemplar] < 1) { continue; } // skip if the value at the index of a possible "exemplar" is 0 (or negative?) All values should be at 255 though... (line ~289)
V3DLONG value_exemplar = this->Image1D_page[pos_exemplar]; // value at the position of the current landmark
V3DLONG count_step = (value_exemplar - default_threshold_global); // value - 20
V3DLONG pos_massCenterOld = -1;
V3DLONG pos_massCenterNew = 0;
vector<V3DLONG> poss_exemplarRegionNew;
vector<V3DLONG> poss_exemplarRegionOld;
V3DLONG idx_step = 0;
double value_centerMovement2 = 0;
for (idx_step = 0; idx_step < count_step; idx_step++)
{
V3DLONG threshold_exemplarRegion = value_exemplar - idx_step;
poss_exemplarRegionNew = this->regionGrowOnPos(pos_exemplar, threshold_exemplarRegion, INF, this->size_page / 1000, this->Image1D_mask);
this->poss2Image1D(poss_exemplarRegionNew, this->Image1D_mask, const_max_voxelValue); // *
if (poss_exemplarRegionNew.size() < default_threshold_regionSize) { break; }
pos_massCenterNew = this->getCenterByMass(poss_exemplarRegionNew);
double value_centerMovement1 = this->getEuclideanDistance2(pos_massCenterOld, pos_massCenterNew);
value_centerMovement2 = this->getEuclideanDistance2(pos_exemplar, pos_massCenterNew);
// if the maximum movement of the center of mass from the old mass center or the position of the landmark is larger than the user-defined "max movement from mass center" or "max movement from marker position", then break.
if (value_centerMovement1 > max_movment1) { break; }
if (value_centerMovement2 > max_movment2) { break; }
// otherwise, update the center of mass and exemplar region (ugly way to handle this).
pos_massCenterOld = pos_massCenterNew;
poss_exemplarRegionOld = poss_exemplarRegionNew;
}
if ((idx_step < 1) || (value_centerMovement2 > max_movment2) ||
(poss_exemplarRegionOld.size() < default_threshold_regionSize)) //try again using loosened criteria;
{
for (idx_step = 0; idx_step < count_step; idx_step++)
{
V3DLONG threshold_exemplarRegion = value_exemplar - idx_step;
poss_exemplarRegionNew = this->regionGrowOnPos(pos_exemplar, threshold_exemplarRegion, INF, this->size_page / 1000, this->Image1D_mask);
this->poss2Image1D(poss_exemplarRegionNew, this->Image1D_mask, const_max_voxelValue);
if (poss_exemplarRegionNew.size() < default_threshold_regionSize) { break; }
pos_massCenterNew = this->getCenterByMass(poss_exemplarRegionNew);
double value_centerMovement1 = this->getEuclideanDistance2(pos_massCenterOld, pos_massCenterNew);
value_centerMovement2 = this->getEuclideanDistance2(pos_exemplar, pos_massCenterNew);
if (value_centerMovement1 > (max_movment1 * 9)) { break; } // these are the only changes from last block
if (value_centerMovement2 > (max_movment2 * 9)) { break; } // these are the only changes from last block
pos_massCenterOld = pos_massCenterNew;
poss_exemplarRegionOld = poss_exemplarRegionNew;
}
}
if (idx_step < 1) { continue; } //failed;
if (value_centerMovement2 > (max_movment2 * 4)) { continue; } //failed;
if (poss_exemplarRegionOld.size() < default_threshold_regionSize) { continue; } //failed;
if (poss_exemplarRegionOld.size() > (this->size_page / 1000)) { continue; } //failed;
// Block used for updating variables to correspond with properties related to the new region shape information
vector<V3DLONG> boundBox_exemplarRegion = this->getBoundBox(poss_exemplarRegionOld);
V3DLONG radius_exemplarRegion = getMinDimension(boundBox_exemplarRegion) / 2;
vector<V3DLONG> xyz_exemplarRegionCenter = this->index2Coordinate(pos_massCenterOld);
vector<vector<double> > valuesVct_shapeStatExemplarRegion = this->getShapeStat(xyz_exemplarRegionCenter[0], xyz_exemplarRegionCenter[1], xyz_exemplarRegionCenter[2], radius_exemplarRegion);
if (valuesVct_shapeStatExemplarRegion.empty()) { continue; } //failed;
this->poss2Image1D(poss_exemplarRegionOld, this->Image1D_mask, 0);
possVct_exemplarRegion.push_back(poss_exemplarRegionOld);
poss_exemplarNew.push_back(pos_massCenterOld);
V3DLONG min_exemplarRegionValue = this->getMin(poss_exemplarRegionOld);
V3DLONG threshold_exemplarRegionValue = value_exemplar - idx_step;
thresholds_valueChangeRatio.push_back((double)(min_exemplarRegionValue - threshold_exemplarRegionValue) / (double)min_exemplarRegionValue);
thresholds_voxelValue.push_back(threshold_exemplarRegionValue);
V3DLONG count_voxel = poss_exemplarRegionOld.size();
V3DLONG size_upper = count_voxel * this->multiplier_uThresholdRegionSize;
V3DLONG size_lower = count_voxel * this->multiplier_thresholdRegionSize;
if (size_lower < default_threshold_regionSize) { size_lower = default_threshold_regionSize; }
thresholds_regionSize.push_back(size_lower);
uThresholds_regionSize.push_back(size_upper);
valueVctVct_exemplarShapeStat.push_back(valuesVct_shapeStatExemplarRegion);
thresholds_radius.push_back(radius_exemplarRegion);
}
// Haven't looked in depth, but seems to be updating variables keeping track of future examples to look at
if (possVct_exemplarRegion.empty()) { return false; }
poss_exemplar.clear();
poss_exemplar = poss_exemplarNew;
count_exemplar = poss_exemplar.size();
memset(this->Image1D_exemplar, 0, this->size_page3);
this->possVct2Image1DC(possVct_exemplarRegion, this->Image1D_exemplar);
vector<V3DLONG> mapping_exemplar = this->sort(thresholds_voxelValue); // in ascending order;
V3DLONG count_seedCategory = this->possVct_seed.size();
unsigned char ** masks_page = this->memory_allocate_uchar2D(count_exemplar, this->size_page);
for (V3DLONG idx_exemplar = 0; idx_exemplar < count_exemplar; idx_exemplar++)
{
memset(masks_page[idx_exemplar], const_max_voxelValue, this->size_page);
for (V3DLONG i = 0; i < this->size_page; i++)
{
if (this->Image1D_mask[i] < 1) { masks_page[idx_exemplar][i] = 0; }
}
}
for (V3DLONG idx_seedCategoy = 0; idx_seedCategoy < count_seedCategory; idx_seedCategoy++)
{
V3DLONG count_seed = this->possVct_seed[idx_seedCategoy].size();
cout << "at value: " << (const_max_voxelValue - idx_seedCategoy) << ", totally: " << count_seed << " seeds;" << endl;
for (V3DLONG idx_seed = 0; idx_seed < count_seed; idx_seed++)
{
V3DLONG pos_seed = this->possVct_seed[idx_seedCategoy][idx_seed];
for (V3DLONG idx_exemplar = 0; idx_exemplar < count_exemplar; idx_exemplar++)
{
if (masks_page[idx_exemplar][pos_seed] < 1) { continue; }
V3DLONG value_seed = this->Image1D_page[pos_seed];
V3DLONG idx_exemplarMapped = mapping_exemplar[idx_exemplar];
V3DLONG threshold_backgroundValue = thresholds_voxelValue[idx_exemplarMapped];
double threshold_valueChangeRatio = thresholds_valueChangeRatio[idx_exemplarMapped];
V3DLONG threshold_regionSize = thresholds_regionSize[idx_exemplarMapped];
if (value_seed < threshold_backgroundValue) { break; }
V3DLONG uThreshold_regionSize = uThresholds_regionSize[idx_exemplarMapped];
V3DLONG threshold_radius = thresholds_radius[idx_exemplarMapped];
vector<V3DLONG> poss_region = this->regionGrowOnPos(pos_seed, threshold_backgroundValue, threshold_valueChangeRatio, uThreshold_regionSize, masks_page[idx_exemplar]);
V3DLONG count_voxel = poss_region.size();
if (count_voxel > uThreshold_regionSize) { continue; }
else if (count_voxel < threshold_regionSize)
{
for (V3DLONG idx_exemplar = 0; idx_exemplar < count_exemplar; idx_exemplar++)
{
this->poss2Image1D(poss_region, masks_page[idx_exemplar], 0);
}
break;
}
vector<V3DLONG> boundBox_region = this->getBoundBox(poss_region);
V3DLONG size_radius = this->getMinDimension(boundBox_region) / 2;
if (size_radius < (threshold_radius*this->multiplier_thresholdRegionSize))
{
for (V3DLONG idx_exemplar = 0; idx_exemplar < count_exemplar; idx_exemplar++)
{
this->poss2Image1D(poss_region, masks_page[idx_exemplar], 0);
}
break;
}
else if (size_radius > (threshold_radius*this->multiplier_uThresholdRegionSize)) { continue; }
V3DLONG pos_center = this->getCenterByMass(poss_region);
vector<V3DLONG> xyz_center = this->index2Coordinate(pos_center);
V3DLONG x = V3DLONG(xyz_center[0]); V3DLONG y = V3DLONG(xyz_center[1]); V3DLONG z = V3DLONG(xyz_center[2]);
vector<vector<double> > valuesVct_regionShapeStat = this->getShapeStat(x, y, z, size_radius); //consisted of 3 vectors with length 4;
if (valuesVct_regionShapeStat.empty())
{
for (V3DLONG idx_exemplar = 0; idx_exemplar < count_exemplar; idx_exemplar++)
{
this->poss2Image1D(poss_region, masks_page[idx_exemplar], 0);
}
break;
}
vector<double> values_PC1 = valuesVct_regionShapeStat[0]; vector<double> values_PC2 = valuesVct_regionShapeStat[1]; vector<double> values_PC3 = valuesVct_regionShapeStat[2];
bool is_passedShapeTest = true;
for (int m = 0; m < 4; m++)
{
double value_anisotropy = valueVctVct_exemplarShapeStat[idx_exemplarMapped][0][m];
if (fabs(values_PC1[m] - value_anisotropy) > (this->threshold_deltaShapeStat*value_anisotropy))
{
is_passedShapeTest = false; break;
}
value_anisotropy = valueVctVct_exemplarShapeStat[idx_exemplarMapped][1][m];
if (fabs(values_PC2[m] - value_anisotropy) > (this->threshold_deltaShapeStat*value_anisotropy))
{
is_passedShapeTest = false; break;
}
value_anisotropy = valueVctVct_exemplarShapeStat[idx_exemplarMapped][2][m];
if (fabs(values_PC3[m] - value_anisotropy) > (this->threshold_deltaShapeStat*value_anisotropy))
{
is_passedShapeTest = false; break;
}
}
if (is_passedShapeTest)
{
this->possVct_segmentationResult.push_back(poss_region);
this->poss_segmentationResultCenter.push_back(pos_center);
for (V3DLONG idx_exemplar = 0; idx_exemplar < count_exemplar; idx_exemplar++)
{
this->poss2Image1D(poss_region, masks_page[idx_exemplar], 0);
}
for (V3DLONG i = 0; i < count_voxel; i++)
{
vector<V3DLONG> xyz_i = this->index2Coordinate(poss_region[i]);
this->Image3D_page[xyz_i[2]][xyz_i[1]][xyz_i[0]] = 0;
}
break;
}
}
}
}
//memset(this->Image1D_mask, const_max_voxelValue, this->size_page);
this->possVct_segmentationResult = this->mergePossVector(possVct_exemplarRegion, this->possVct_segmentationResult);
this->possVct2Image1D(this->possVct_segmentationResult, this->Image1D_mask, 0);
this->poss_segmentationResultCenter = this->mergePoss(poss_exemplar, this->poss_segmentationResultCenter);
this->LandmarkList_segmentationResult = this->poss2LandMarkList(this->poss_segmentationResultCenter);
//QString filename="quickfind_test.v3draw";
//simple_saveimage_wrapper(this->_V3DPluginCallback2_currentCallback,filename.toAscii(),this->Image1D_segmentationResult,this->dim_X,this->dim_Y,this->dim_Z);
this->possVct2Image1DC(this->possVct_segmentationResult, this->Image1D_segmentationResult);
this->memory_free_uchar2D(masks_page, count_exemplar);
return true;
} // End of control_run
#pragma endregion
#pragma region "regionGrow"
void categorizeVoxelsByValue() //will only consider voxels with value higher than threshold_global;
{
this->possVct_seed.clear(); // sets vector to size 0
vector<V3DLONG> poss_empty(0, 0); // creates a vector of size 0 with length 0
for (V3DLONG i = default_threshold_global; i < const_length_histogram; i++) // for i = 20 to i < 256
{
this->possVct_seed.push_back(poss_empty); // adds (256-20) empty spaces to possVct_seed
}
for (V3DLONG i = 0; i < this->size_page; i++)
{
V3DLONG value_i = this->Image1D_page[i];
if (value_i > default_threshold_global) // 20
{
V3DLONG offset_i = const_max_voxelValue - value_i; // 255
this->possVct_seed[offset_i].push_back(i); // adds the location of the offset to the position coinciding with the offset?
}
}
}
// Grows out from the start node out to any neighbors if their value is greater than the threshold voxel value and the difference of the start node and the neighbor node is less than the specified threshold ratio. Continues until no more neighbors exist.
vector<V3DLONG> regionGrowOnPos(V3DLONG _pos_seed, V3DLONG _threshold_voxelValue, double _threshold_valueChangeRatio,
V3DLONG _uThreshold_regionSize, unsigned char* _mask_input)
{
vector<V3DLONG> poss_result;
vector<V3DLONG> poss_growing;
poss_growing.push_back(_pos_seed);
poss_result.push_back(_pos_seed);
V3DLONG count_voxel = 1;
_mask_input[_pos_seed] = 0; //scooped;
V3DLONG min_voxelValue = this->Image1D_page[_pos_seed];
while (true)
{
if (poss_growing.empty()) //growing complete;
{
return poss_result;
}
V3DLONG pos_current = poss_growing.back(); //stores last value in poss_growing
poss_growing.pop_back(); // removes last value in poss_growing
vector<V3DLONG> xyz_current = this->index2Coordinate(pos_current);
for (int j = 0; j < const_count_neighbors; j++) // 26 (27 neighbors - 1)
{
// if the jth neighbor is out of range of the graph, do nothing.
if (((xyz_current[0] + point_neighborRelative[j].x) < 0) || ((xyz_current[0] + point_neighborRelative[j].x) >= this->dim_X) || ((xyz_current[1] + point_neighborRelative[j].y) < 0) || ((xyz_current[1] + point_neighborRelative[j].y) >= this->dim_Y) || ((xyz_current[2] + point_neighborRelative[j].z) < 0) || ((xyz_current[2] + point_neighborRelative[j].z) >= this->dim_Z))
{
// invalid anyway;
}
else
{
V3DLONG pos_neighbor = pos_current + poss_neighborRelative[j];
// This was literally just checked, but using the 3D coordinates... should suffice to only check one
if (this->checkValidity(pos_neighbor)) //prevent it from going out of bounds;
{
if (_mask_input[pos_neighbor] > 0) //available only;
{
V3DLONG value_neighbor = this->Image1D_page[pos_neighbor];
if ((value_neighbor > _threshold_voxelValue) && ((min_voxelValue - value_neighbor) < (min_voxelValue*_threshold_valueChangeRatio)))
{
_mask_input[pos_neighbor] = 0; //scooped;
poss_growing.push_back(pos_neighbor);
poss_result.push_back(pos_neighbor);
count_voxel++;
if (value_neighbor < min_voxelValue) { min_voxelValue = value_neighbor; }
if (count_voxel > (_uThreshold_regionSize + 2)) //too large;
{
return poss_result;
}
}
}
}
}
}
}
}
#pragma endregion
#pragma region "utility functions"
static void Image3D2Image1D(int*** Image3D_input, unsigned char* Image1D_output, const int size_X, const int size_Y, const int size_Z)
{
int tmp_value = 0;
int tmp_idx = 0;
for (int z = 0; z < size_Z; z++)
{
for (int x = 0; x < size_X; x++)
{
for (int y = 0; y < size_Y; y++)
{
tmp_value = Image3D_input[z][x][y];
tmp_idx = class_segmentationMain::coordinate2Index(x, y, z, size_X, size_X*size_Y);
Image1D_output[tmp_idx] = tmp_value;
}
}
}
return;
}
void Image1D2Image3D(const unsigned char* Image1D_input, double*** Image3D_output, const int dim_X, const int dim_Y, const int dim_Z)
{
vector<V3DLONG> vct_coordinate;
int count_page = dim_X * dim_Y*dim_Z;
for (int i = 0; i < count_page; i++)
{
vct_coordinate = index2Coordinate(i);
Image3D_output[vct_coordinate[2]][vct_coordinate[0]][vct_coordinate[1]] = Image1D_input[i];
}
return;
}
bool checkValidity(V3DLONG idx_input)
{
if ((idx_input >= 0) && (idx_input < this->size_page))
{
return true;
}
else
{
return false;
}
}
vector<vector<V3DLONG> > mergePossVector(vector<vector<V3DLONG> > vctList_input1, vector<vector<V3DLONG> > vctList_input2) //vctList_input2 will be appended to vctList_input1;
{
vector<vector<V3DLONG> > vctList_result = vctList_input1;
vector<V3DLONG> vct_tmp;
V3DLONG count_region2 = vctList_input2.size();
for (int i = 0; i < count_region2; i++)
{
vct_tmp = vctList_input2[i];
vctList_result.push_back(vct_tmp);
}
return vctList_result;
}
vector<V3DLONG> mergePoss(vector<V3DLONG> poss_input1, vector<V3DLONG> poss_input2)
{
vector<V3DLONG> poss_result = poss_input1;
V3DLONG count_pos = poss_input2.size();
for (int i = 0; i < count_pos; i++)
{
poss_result.push_back(poss_input2[i]);
}
return poss_result;
}
LandmarkList poss2LandMarkList(vector<V3DLONG> vct_index)
{
LandmarkList LandmarkList_result;
LocationSimple Landmark_tmp;
for (int i = 0; i < vct_index.size(); i++)
{
Landmark_tmp = index2LandMark(vct_index[i]);
LandmarkList_result.push_back(Landmark_tmp);
}
return LandmarkList_result;
}
LocationSimple index2LandMark(V3DLONG idx_Input)
{
vector<V3DLONG> vct_coordinate = index2Coordinate(idx_Input);
V3DLONG x = vct_coordinate[0] + 1;
V3DLONG y = vct_coordinate[1] + 1;
V3DLONG z = vct_coordinate[2] + 1;
LocationSimple LocationSimple_result(x, y, z);
return LocationSimple_result;
}
vector<V3DLONG> landMarkList2IndexList(LandmarkList LandmarkList_input) // Sends LandmarkList to vector
{
vector<V3DLONG> vct_result;
for (V3DLONG idx_input = 0; idx_input < LandmarkList_input.count(); idx_input++)
{
vct_result.push_back(landMark2Index(LandmarkList_input.at(idx_input)));
}
return vct_result;
}
V3DLONG landMark2Index(LocationSimple Landmark_input)
{
float x = 0;
float y = 0;
float z = 0;
Landmark_input.getCoord(x, y, z);
return (coordinate2Index(x - 1, y - 1, z - 1));
}
vector<V3DLONG> index2Coordinate(V3DLONG idx)
{
vector<V3DLONG> vct_result(3, -1);
vct_result[2] = floor((double)idx / (double)offset_Z);
vct_result[1] = floor((double)(idx - vct_result[2] * offset_Z) / (double)offset_Y);
vct_result[0] = idx - vct_result[2] * offset_Z - vct_result[1] * offset_Y;
return vct_result;
}
static vector<V3DLONG> index2Coordinate(V3DLONG idx, V3DLONG offset_Y, V3DLONG offset_Z)
{
vector<V3DLONG> vct_result(3, -1);
vct_result[2] = floor((double)idx / (double)offset_Z);
vct_result[1] = floor((double)(idx - vct_result[2] * offset_Z) / (double)offset_Y);
vct_result[0] = idx - vct_result[2] * offset_Z - vct_result[1] * offset_Y;
return vct_result;
}
V3DLONG coordinate2Index(V3DLONG x, V3DLONG y, V3DLONG z)
{
return z * this->offset_Z + y * this->offset_Y + x;
}
static V3DLONG coordinate2Index(V3DLONG x, V3DLONG y, V3DLONG z, V3DLONG offset_Y, V3DLONG offset_Z)
{
return z * offset_Z + y * offset_Y + x;
}
void initializeConstants()
{
// General question: are these required to be preceded by "this->"?
this->poss_neighborRelative.clear();
this->point_neighborRelative.clear();
this->colors_simpleTable.clear();
double3D point_neighbor;
for (V3DLONG z = -1; z <= 1; z++)
{
for (V3DLONG y = -1; y <= 1; y++)
{
for (V3DLONG x = -1; x <= 1; x++)
{
if (x == 0 && y == 0 && z == 0)
{
//that's itself;
}
else
{
this->poss_neighborRelative.push_back(z * this->offset_Z + y * this->offset_Y + x);
point_neighbor.x = x; point_neighbor.y = y; point_neighbor.z = z;
this->point_neighborRelative.push_back(point_neighbor);
}
}
}
}
vector<V3DLONG> color_tmp(3, 0); color_tmp[0] = 255; color_tmp[1] = 0; color_tmp[2] = 0;
this->colors_simpleTable.push_back(color_tmp);
color_tmp[0] = 0; color_tmp[1] = 255; color_tmp[2] = 0;
this->colors_simpleTable.push_back(color_tmp);
color_tmp[0] = 0; color_tmp[1] = 0; color_tmp[2] = 255;
this->colors_simpleTable.push_back(color_tmp);
color_tmp[0] = 255; color_tmp[1] = 255; color_tmp[2] = 0;
this->colors_simpleTable.push_back(color_tmp);
color_tmp[0] = 0; color_tmp[1] = 255; color_tmp[2] = 255;
this->colors_simpleTable.push_back(color_tmp);
color_tmp[0] = 255; color_tmp[1] = 0; color_tmp[2] = 255;
this->colors_simpleTable.push_back(color_tmp);
color_tmp[0] = 255; color_tmp[1] = 128; color_tmp[2] = 0;
this->colors_simpleTable.push_back(color_tmp);
color_tmp[0] = 128; color_tmp[1] = 255; color_tmp[2] = 0;
this->colors_simpleTable.push_back(color_tmp);
color_tmp[0] = 0; color_tmp[1] = 128; color_tmp[2] = 255;
this->colors_simpleTable.push_back(color_tmp);
color_tmp[0] = 255; color_tmp[1] = 255; color_tmp[2] = 128;
this->colors_simpleTable.push_back(color_tmp);
color_tmp[0] = 128; color_tmp[1] = 255; color_tmp[2] = 255;
this->colors_simpleTable.push_back(color_tmp);
color_tmp[0] = 255; color_tmp[1] = 128; color_tmp[2] = 255;
this->colors_simpleTable.push_back(color_tmp);
}
void possVct2Image1DC(vector<vector<V3DLONG> > possVct_input, unsigned char* Image1D_input)
{
vector<V3DLONG> color_input(3, 0);
for (int i = 0; i < possVct_input.size(); i++)
{
int idx_color = i % 12;
color_input[0] = colors_simpleTable[idx_color][0];
color_input[1] = colors_simpleTable[idx_color][1];
color_input[2] = colors_simpleTable[idx_color][2];
poss2Image1DC(Image1D_input, possVct_input[i], color_input);
}
}
void poss2Image1D(vector<V3DLONG> poss_input, unsigned char* Image1D_input, V3DLONG value_input)
{
V3DLONG size_input = poss_input.size();
for (int i = 0; i < size_input; i++) { Image1D_input[poss_input[i]] = value_input; }
}
void poss2Image1D(vector<V3DLONG> poss_input, V3DLONG* Image1D_input, V3DLONG value_input)
{
V3DLONG size_input = poss_input.size();
for (int i = 0; i < size_input; i++) { Image1D_input[poss_input[i]] = value_input; }
}
void possVct2Image1D(vector<vector<V3DLONG> > possVct_input, unsigned char* Image1D_input, V3DLONG value_input)
{
V3DLONG count_region = possVct_input.size();
for (V3DLONG i = 0; i < count_region; i++) { poss2Image1D(possVct_input[i], Image1D_input, value_input); }
}
void poss2Image1DC(unsigned char* Image1D_input, vector<V3DLONG> poss_input, vector<V3DLONG> color_input)
{
for (int i = 0; i < poss_input.size(); i++)
{
if (this->checkValidity(poss_input[i]))
{
Image1D_input[poss_input[i]] = color_input[0];
Image1D_input[poss_input[i] + this->size_page] = color_input[1];
Image1D_input[poss_input[i] + this->size_page + this->size_page] = color_input[2];
}
}
}
static void neuronTree2LandmarkList(const NeuronTree & NeuronTree_input, LandmarkList & LandmarkList_output)
{
LocationSimple LocationSimple_temp(0, 0, 0);
for (V3DLONG i = 0; i < NeuronTree_input.listNeuron.size(); i++)
{
LocationSimple_temp.x = NeuronTree_input.listNeuron.at(i).x;
LocationSimple_temp.y = NeuronTree_input.listNeuron.at(i).y;
LocationSimple_temp.z = NeuronTree_input.listNeuron.at(i).z;
LandmarkList_output.append(LocationSimple_temp);
}
}
static V3DLONG vctContains(vector<V3DLONG> vct_input, V3DLONG idx_input)
{
for (int i = 0; i < vct_input.size(); i++)
{
if (vct_input[i] == idx_input)
{
return i;
}
}
return -1;
}
#pragma endregion
#pragma region "sorting and comparison"
double getMax(vector<double> values_input)
{
double max_result = -INF;
for (std::vector<double>::iterator it = values_input.begin(); it != values_input.end(); ++it)
{
if (max_result < *it) { max_result = *it; }
}
return max_result;
}
double getMax(vector<V3DLONG> poss_input)
{
double max_result = -INF;
V3DLONG count_input = poss_input.size();
for (V3DLONG i = 0; i < count_input; i++)
{
double value_i = this->Image1D_page[poss_input[i]];
if (max_result < value_i) { max_result = value_i; }
}
return max_result;
}
double getMin(vector<double> values_input)
{
double min_result = -INF;
for (std::vector<double>::iterator it = values_input.begin(); it != values_input.end(); ++it)
{
if (min_result < *it) { min_result = *it; }
}
return min_result;
}
double getMin(vector<V3DLONG> poss_input)
{
double min_result = INF;
V3DLONG count_input = poss_input.size();
for (V3DLONG i = 0; i < count_input; i++)
{
double value_i = this->Image1D_page[poss_input[i]];
if (min_result > value_i) { min_result = value_i; }
}
return min_result;
}
vector<V3DLONG> sort(vector<V3DLONG> values_input)
{
V3DLONG count_input = values_input.size();
vector<V3DLONG> mapping_result;
for (V3DLONG i = 0; i < count_input; i++)
{
V3DLONG value_i = values_input[i];
V3DLONG count_greater = 0;
for (V3DLONG j = 0; j < count_input; j++)
{
V3DLONG value_j = values_input[j];
if (value_i < value_j) { count_greater++; }
if ((value_i == value_j) && (j > i)) { count_greater++; }
}
mapping_result.push_back(count_greater);
}
return mapping_result;
}
void swap(V3DLONG& x, V3DLONG& y)
{
V3DLONG tmp = x; x = y; y = tmp;
}
#pragma endregion
#pragma region "geometry property"
static vector<V3DLONG> getOffset(const V3DLONG dim_X, const V3DLONG dim_Y, const V3DLONG dim_Z)
{
vector<V3DLONG> size_result(2, 0);
size_result[0] = dim_X * dim_Y;
size_result[1] = dim_X;
return size_result;
}
double getEuclideanDistance2(V3DLONG pos_input1, V3DLONG pos_input2)
{
if ((pos_input1 < 0) || (pos_input2 < 0)) { return 0; }
double result = 0;
vector<V3DLONG> vct_xyz1 = this->index2Coordinate(pos_input1);
vector<V3DLONG> vct_xyz2 = this->index2Coordinate(pos_input2);
result += (vct_xyz1[0] - vct_xyz2[0])*(vct_xyz1[0] - vct_xyz2[0]);
result += (vct_xyz1[1] - vct_xyz2[1])*(vct_xyz1[1] - vct_xyz2[1]);
result += (vct_xyz1[2] - vct_xyz2[2])*(vct_xyz1[2] - vct_xyz2[2]);
return result;
}
double getEuclideanDistance(V3DLONG pos_input1, V3DLONG pos_input2)
{
if ((pos_input1 < 0) || (pos_input2 < 0)) { return 0; }
double result = 0;
vector<V3DLONG> vct_xyz1 = this->index2Coordinate(pos_input1);
vector<V3DLONG> vct_xyz2 = this->index2Coordinate(pos_input2);
result += (vct_xyz1[0] - vct_xyz2[0])*(vct_xyz1[0] - vct_xyz2[0]);
result += (vct_xyz1[1] - vct_xyz2[1])*(vct_xyz1[1] - vct_xyz2[1]);
result += (vct_xyz1[2] - vct_xyz2[2])*(vct_xyz1[2] - vct_xyz2[2]);
result = sqrt(result);
return result;
}
static double getEuclideanDistance(double3D point_input1, double3D point_input2)
{
double result = 0;
result += (point_input1.x - point_input2.x)*(point_input1.x - point_input2.x);
result += (point_input1.y - point_input2.y)*(point_input1.y - point_input2.y);
result += (point_input1.z - point_input2.z)*(point_input1.z - point_input2.z);
result = sqrt(result);
return result;
}
void centralizeRegion(const vector<V3DLONG> poss_input, const V3DLONG size_X, const V3DLONG size_Y, const V3DLONG size_Z,
const V3DLONG min_X, const V3DLONG min_Y, const V3DLONG min_Z, unsigned char* Image1D_output)
{
V3DLONG pos_voxel = 0;
V3DLONG pos_centralized = 0;
V3DLONG x = 0;
V3DLONG y = 0;
V3DLONG z = 0;
vector<V3DLONG> xyz_voxel(0, 0);
V3DLONG count_voxel = poss_input.size();
V3DLONG size_region = size_X * size_Y*size_Z;
for (int i = 0; i < size_region; i++)
{
Image1D_output[i] = 0;
}
for (V3DLONG idx_voxel = 0; idx_voxel < count_voxel; idx_voxel++)
{
pos_voxel = poss_input[idx_voxel];
xyz_voxel = this->index2Coordinate(pos_voxel);
x = xyz_voxel[0] - min_X;
y = xyz_voxel[1] - min_Y;
z = xyz_voxel[2] - min_Z;
pos_centralized = class_segmentationMain::coordinate2Index(x, y, z, size_X, size_X*size_Y);
Image1D_output[pos_centralized] = (int)this->Image1D_page[pos_voxel];
}
return;
}
void centralizeRegion(const vector<V3DLONG> poss_input, const V3DLONG size_X, const V3DLONG size_Y, const V3DLONG size_Z,
const V3DLONG min_X, const V3DLONG min_Y, const V3DLONG min_Z, double*** Image3D_output)
{
V3DLONG pos_voxel = 0;
vector<V3DLONG> xyz_voxel(0, 0);
V3DLONG x = 0;
V3DLONG y = 0;
V3DLONG z = 0;
V3DLONG count_voxel = poss_input.size();
for (z = 0; z < size_Z; z++)
{