-
Notifications
You must be signed in to change notification settings - Fork 26
/
slic.cpp
1124 lines (990 loc) · 29.9 KB
/
slic.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
// SLIC.cpp: implementation of the SLIC class.
//===========================================================================
// This code implements the zero parameter superpixel segmentation technique
// described in:
//
//
//
// "SLIC Superpixels Compared to State-of-the-art Superpixel Methods"
//
// Radhakrishna Achanta, Appu Shaji, Kevin Smith, Aurelien Lucchi, Pascal Fua,
// and Sabine Susstrunk,
//
// IEEE TPAMI, Volume 34, Issue 11, Pages 2274-2282, November 2012.
//
//
//===========================================================================
// Copyright (c) 2013 Radhakrishna Achanta.
//
// For commercial use please contact the author:
//
// Email: [email protected]
//===========================================================================
//
// Modified by nipan
// Email: [email protected]
//
#include <cfloat>
#include <cmath>
#include <iostream>
#include <fstream>
#include "slic.h"
using namespace cv;
using namespace std;
// For superpixels
const int dx4[4] = {-1, 0, 1, 0};
const int dy4[4] = { 0, -1, 0, 1};
//const int dx8[8] = {-1, -1, 0, 1, 1, 1, 0, -1};
//const int dy8[8] = { 0, -1, -1, -1, 0, 1, 1, 1};
// For supervoxels
const int dx10[10] = {-1, 0, 1, 0, -1, 1, 1, -1, 0, 0};
const int dy10[10] = { 0, -1, 0, 1, -1, -1, 1, 1, 0, 0};
const int dz10[10] = { 0, 0, 0, 0, 0, 0, 0, 0, -1, 1};
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
SLIC::SLIC()
{
m_lvec = NULL;
m_avec = NULL;
m_bvec = NULL;
m_lvecvec = NULL;
m_avecvec = NULL;
m_bvecvec = NULL;
bufferGray = NULL;
bufferRGB = NULL;
}
SLIC::~SLIC()
{
if(m_lvec) delete [] m_lvec;
if(m_avec) delete [] m_avec;
if(m_bvec) delete [] m_bvec;
if(m_lvecvec)
{
for( int d = 0; d < m_depth; d++ ) delete [] m_lvecvec[d];
delete [] m_lvecvec;
}
if(m_avecvec)
{
for( int d = 0; d < m_depth; d++ ) delete [] m_avecvec[d];
delete [] m_avecvec;
}
if(m_bvecvec)
{
for( int d = 0; d < m_depth; d++ ) delete [] m_bvecvec[d];
delete [] m_bvecvec;
}
if (bufferGray) {
delete [] bufferGray;
}
if (bufferRGB) {
delete [] bufferRGB;
}
if (label) {
delete [] label;
}
}
//==============================================================================
/// RGB2XYZ
///
/// sRGB (D65 illuminant assumption) to XYZ conversion
//==============================================================================
void SLIC::RGB2XYZ(
const int& sR,
const int& sG,
const int& sB,
double& X,
double& Y,
double& Z)
{
double R = sR/255.0;
double G = sG/255.0;
double B = sB/255.0;
double r, g, b;
if(R <= 0.04045) r = R/12.92;
else r = pow((R+0.055)/1.055,2.4);
if(G <= 0.04045) g = G/12.92;
else g = pow((G+0.055)/1.055,2.4);
if(B <= 0.04045) b = B/12.92;
else b = pow((B+0.055)/1.055,2.4);
X = r*0.4124564 + g*0.3575761 + b*0.1804375;
Y = r*0.2126729 + g*0.7151522 + b*0.0721750;
Z = r*0.0193339 + g*0.1191920 + b*0.9503041;
}
//===========================================================================
/// RGB2LAB
//===========================================================================
void SLIC::RGB2LAB(const int& sR, const int& sG, const int& sB, double& lval, double& aval, double& bval)
{
//------------------------
// sRGB to XYZ conversion
//------------------------
double X, Y, Z;
RGB2XYZ(sR, sG, sB, X, Y, Z);
//------------------------
// XYZ to LAB conversion
//------------------------
double epsilon = 0.008856; //actual CIE standard
double kappa = 903.3; //actual CIE standard
double Xr = 0.950456; //reference white
double Yr = 1.0; //reference white
double Zr = 1.088754; //reference white
double xr = X/Xr;
double yr = Y/Yr;
double zr = Z/Zr;
double fx, fy, fz;
if(xr > epsilon) fx = pow(xr, 1.0/3.0);
else fx = (kappa*xr + 16.0)/116.0;
if(yr > epsilon) fy = pow(yr, 1.0/3.0);
else fy = (kappa*yr + 16.0)/116.0;
if(zr > epsilon) fz = pow(zr, 1.0/3.0);
else fz = (kappa*zr + 16.0)/116.0;
lval = 116.0*fy-16.0;
aval = 500.0*(fx-fy);
bval = 200.0*(fy-fz);
}
//===========================================================================
/// DoRGBtoLABConversion
///
/// For whole image: overlaoded floating point version
//===========================================================================
void SLIC::DoRGBtoLABConversion(
const unsigned int*& ubuff,
double*& lvec,
double*& avec,
double*& bvec)
{
int sz = m_width*m_height;
lvec = new double[sz];
avec = new double[sz];
bvec = new double[sz];
for( int j = 0; j < sz; j++ )
{
int r = (ubuff[j] >> 16) & 0xFF;
int g = (ubuff[j] >> 8) & 0xFF;
int b = (ubuff[j] ) & 0xFF;
RGB2LAB( r, g, b, lvec[j], avec[j], bvec[j] );
}
}
//===========================================================================
/// DoRGBtoLABConversion
///
/// For whole volume
//===========================================================================
void SLIC::DoRGBtoLABConversion(
const unsigned int**& ubuff,
double**& lvec,
double**& avec,
double**& bvec)
{
int sz = m_width*m_height;
for( int d = 0; d < m_depth; d++ )
{
for( int j = 0; j < sz; j++ )
{
int r = (ubuff[d][j] >> 16) & 0xFF;
int g = (ubuff[d][j] >> 8) & 0xFF;
int b = (ubuff[d][j] ) & 0xFF;
RGB2LAB( r, g, b, lvec[d][j], avec[d][j], bvec[d][j] );
}
}
}
//=================================================================================
/// DrawContoursAroundSegments
///
/// Internal contour drawing option exists. One only needs to comment the if
/// statement inside the loop that looks at neighbourhood.
//=================================================================================
void SLIC::DrawContoursAroundSegments(
unsigned int* ubuff,
const int* labels,
const int& width,
const int& height,
const cv::Scalar& color )
{
const int dx8[8] = {-1, -1, 0, 1, 1, 1, 0, -1};
const int dy8[8] = { 0, -1, -1, -1, 0, 1, 1, 1};
int sz = width*height;
vector<bool> istaken(sz, false);
int mainindex(0);
for( int j = 0; j < height; j++ )
{
for( int k = 0; k < width; k++ )
{
int np(0);
for( int i = 0; i < 8; i++ )
{
int x = k + dx8[i];
int y = j + dy8[i];
if( (x >= 0 && x < width) && (y >= 0 && y < height) )
{
int index = y*width + x;
if( false == istaken[index] )//comment this to obtain internal contours
{
if( labels[mainindex] != labels[index] ) np++;
}
}
}
if( np > 1 )//change to 2 or 3 for thinner lines
{
ubuff[mainindex] = 0;
ubuff[mainindex] |= (int)color.val[2] << 16; // r
ubuff[mainindex] |= (int)color.val[1] << 8; // g
ubuff[mainindex] |= (int)color.val[0];
//ubuff[mainindex] |= 255 << 16; // r
//ubuff[mainindex] |= 0 << 8; // g
//ubuff[mainindex] |= 0;
istaken[mainindex] = true;
}
mainindex++;
}
}
}
void SLIC::DrawContoursAroundSegments(
unsigned char* ubuff,
const int* labels,
const int& width,
const int& height,
const cv::Scalar& color )
{
const int dx8[8] = {-1, -1, 0, 1, 1, 1, 0, -1};
const int dy8[8] = { 0, -1, -1, -1, 0, 1, 1, 1};
int sz = width*height;
vector<bool> istaken(sz, false);
int mainindex(0);
for( int j = 0; j < height; j++ )
{
for( int k = 0; k < width; k++ )
{
int np(0);
for( int i = 0; i < 8; i++ )
{
int x = k + dx8[i];
int y = j + dy8[i];
if( (x >= 0 && x < width) && (y >= 0 && y < height) )
{
int index = y*width + x;
if( false == istaken[index] )//comment this to obtain internal contours
{
if( labels[mainindex] != labels[index] ) np++;
}
}
}
if( np > 1 )//change to 2 or 3 for thinner lines
{
ubuff[mainindex] = (uchar)color.val[0];
istaken[mainindex] = true;
}
mainindex++;
}
}
}
//=================================================================================
/// DrawContoursAroundSegmentsTwoColors
///
/// Internal contour drawing option exists. One only needs to comment the if
/// statement inside the loop that looks at neighbourhood.
//=================================================================================
void SLIC::DrawContoursAroundSegmentsTwoColors(
unsigned int* img,
const int* labels,
const int& width,
const int& height)
{
const int dx[8] = {-1, -1, 0, 1, 1, 1, 0, -1};
const int dy[8] = { 0, -1, -1, -1, 0, 1, 1, 1};
int sz = width*height;
vector<bool> istaken(sz, false);
vector<int> contourx(sz);
vector<int> contoury(sz);
int mainindex(0);
int cind(0);
for( int j = 0; j < height; j++ )
{
for( int k = 0; k < width; k++ )
{
int np(0);
for( int i = 0; i < 8; i++ )
{
int x = k + dx[i];
int y = j + dy[i];
if( (x >= 0 && x < width) && (y >= 0 && y < height) )
{
int index = y*width + x;
//if( false == istaken[index] )//comment this to obtain internal contours
{
if( labels[mainindex] != labels[index] ) np++;
}
}
}
if( np > 1 )
{
contourx[cind] = k;
contoury[cind] = j;
istaken[mainindex] = true;
//img[mainindex] = color;
cind++;
}
mainindex++;
}
}
int numboundpix = cind;//int(contourx.size());
for( int j = 0; j < numboundpix; j++ )
{
int ii = contoury[j]*width + contourx[j];
img[ii] = 0xffffff;
//----------------------------------
// Uncomment this for thicker lines
//----------------------------------
for( int n = 0; n < 8; n++ )
{
int x = contourx[j] + dx[n];
int y = contoury[j] + dy[n];
if( (x >= 0 && x < width) && (y >= 0 && y < height) )
{
int ind = y*width + x;
if(!istaken[ind]) img[ind] = 0;
}
}
}
}
//==============================================================================
/// DetectLabEdges
//==============================================================================
void SLIC::DetectLabEdges(
const double* lvec,
const double* avec,
const double* bvec,
const int& width,
const int& height,
vector<double>& edges)
{
int sz = width*height;
edges.resize(sz,0);
for( int j = 1; j < height-1; j++ )
{
for( int k = 1; k < width-1; k++ )
{
int i = j*width+k;
double dx = (lvec[i-1]-lvec[i+1])*(lvec[i-1]-lvec[i+1]) +
(avec[i-1]-avec[i+1])*(avec[i-1]-avec[i+1]) +
(bvec[i-1]-bvec[i+1])*(bvec[i-1]-bvec[i+1]);
double dy = (lvec[i-width]-lvec[i+width])*(lvec[i-width]-lvec[i+width]) +
(avec[i-width]-avec[i+width])*(avec[i-width]-avec[i+width]) +
(bvec[i-width]-bvec[i+width])*(bvec[i-width]-bvec[i+width]);
//edges[i] = (sqrt(dx) + sqrt(dy));
edges[i] = (dx + dy);
}
}
}
//===========================================================================
/// PerturbSeeds
//===========================================================================
void SLIC::PerturbSeeds(
vector<double>& kseedsl,
vector<double>& kseedsa,
vector<double>& kseedsb,
vector<double>& kseedsx,
vector<double>& kseedsy,
const vector<double>& edges)
{
const int dx8[8] = {-1, -1, 0, 1, 1, 1, 0, -1};
const int dy8[8] = { 0, -1, -1, -1, 0, 1, 1, 1};
int numseeds = kseedsl.size();
for( int n = 0; n < numseeds; n++ )
{
int ox = kseedsx[n];//original x
int oy = kseedsy[n];//original y
int oind = oy*m_width + ox;
int storeind = oind;
for( int i = 0; i < 8; i++ )
{
int nx = ox+dx8[i];//new x
int ny = oy+dy8[i];//new y
if( nx >= 0 && nx < m_width && ny >= 0 && ny < m_height)
{
int nind = ny*m_width + nx;
if( edges[nind] < edges[storeind])
{
storeind = nind;
}
}
}
if(storeind != oind)
{
kseedsx[n] = storeind%m_width;
kseedsy[n] = storeind/m_width;
kseedsl[n] = m_lvec[storeind];
kseedsa[n] = m_avec[storeind];
kseedsb[n] = m_bvec[storeind];
}
}
}
//===========================================================================
/// GetLABXYSeeds_ForGivenStepSize
///
/// The k seed values are taken as uniform spatial pixel samples.
//===========================================================================
void SLIC::GetLABXYSeeds_ForGivenStepSize(
vector<double>& kseedsl,
vector<double>& kseedsa,
vector<double>& kseedsb,
vector<double>& kseedsx,
vector<double>& kseedsy,
const int& STEP,
const bool& perturbseeds,
const vector<double>& edgemag)
{
int numseeds(0);
int n(0);
//int xstrips = m_width/STEP;
//int ystrips = m_height/STEP;
int xstrips = (0.5+double(m_width)/double(STEP));
int ystrips = (0.5+double(m_height)/double(STEP));
int xerr = m_width - STEP*xstrips;
int yerr = m_height - STEP*ystrips;
double xerrperstrip = double(xerr)/double(xstrips);
double yerrperstrip = double(yerr)/double(ystrips);
int xoff = STEP/2;
int yoff = STEP/2;
//-------------------------
numseeds = xstrips*ystrips;
//-------------------------
kseedsl.resize(numseeds);
kseedsa.resize(numseeds);
kseedsb.resize(numseeds);
kseedsx.resize(numseeds);
kseedsy.resize(numseeds);
for( int y = 0; y < ystrips; y++ )
{
int ye = y*yerrperstrip;
for( int x = 0; x < xstrips; x++ )
{
int xe = x*xerrperstrip;
int i = (y*STEP+yoff+ye)*m_width + (x*STEP+xoff+xe);
kseedsl[n] = m_lvec[i];
kseedsa[n] = m_avec[i];
kseedsb[n] = m_bvec[i];
kseedsx[n] = (x*STEP+xoff+xe);
kseedsy[n] = (y*STEP+yoff+ye);
n++;
}
}
if(perturbseeds)
{
PerturbSeeds(kseedsl, kseedsa, kseedsb, kseedsx, kseedsy, edgemag);
}
}
//===========================================================================
/// GetLABXYSeeds_ForGivenK
///
/// The k seed values are taken as uniform spatial pixel samples.
//===========================================================================
void SLIC::GetLABXYSeeds_ForGivenK(
vector<double>& kseedsl,
vector<double>& kseedsa,
vector<double>& kseedsb,
vector<double>& kseedsx,
vector<double>& kseedsy,
const int& K,
const bool& perturbseeds,
const vector<double>& edgemag)
{
int sz = m_width*m_height;
double step = sqrt(double(sz)/double(K));
int T = step;
int xoff = step/2;
int yoff = step/2;
int n(0);int r(0);
for( int y = 0; y < m_height; y++ )
{
int Y = y*step + yoff;
if( Y > m_height-1 ) break;
for( int x = 0; x < m_width; x++ )
{
//int X = x*step + xoff;//square grid
int X = x*step + (xoff<<(r&0x1));//hex grid
if(X > m_width-1) break;
int i = Y*m_width + X;
//_ASSERT(n < K);
//kseedsl[n] = m_lvec[i];
//kseedsa[n] = m_avec[i];
//kseedsb[n] = m_bvec[i];
//kseedsx[n] = X;
//kseedsy[n] = Y;
kseedsl.push_back(m_lvec[i]);
kseedsa.push_back(m_avec[i]);
kseedsb.push_back(m_bvec[i]);
kseedsx.push_back(X);
kseedsy.push_back(Y);
n++;
}
r++;
}
if(perturbseeds)
{
PerturbSeeds(kseedsl, kseedsa, kseedsb, kseedsx, kseedsy, edgemag);
}
}
//===========================================================================
/// PerformSuperpixelSegmentation_VariableSandM
///
/// Magic SLIC - no parameters
///
/// Performs k mean segmentation. It is fast because it looks locally, not
/// over the entire image.
/// This function picks the maximum value of color distance as compact factor
/// M and maximum pixel distance as grid step size S from each cluster (13 April 2011).
/// So no need to input a constant value of M and S. There are two clear
/// advantages:
///
/// [1] The algorithm now better handles both textured and non-textured regions
/// [2] There is not need to set any parameters!!!
///
/// SLICO (or SLIC Zero) dynamically varies only the compactness factor S,
/// not the step size S.
//===========================================================================
void SLIC::PerformSuperpixelSegmentation_VariableSandM(
vector<double>& kseedsl,
vector<double>& kseedsa,
vector<double>& kseedsb,
vector<double>& kseedsx,
vector<double>& kseedsy,
int* klabels,
const int& STEP,
const int& NUMITR)
{
int sz = m_width*m_height;
const int numk = kseedsl.size();
//double cumerr(99999.9);
int numitr(0);
//----------------
int offset = STEP;
if(STEP < 10) offset = STEP*1.5;
//----------------
vector<double> sigmal(numk, 0);
vector<double> sigmaa(numk, 0);
vector<double> sigmab(numk, 0);
vector<double> sigmax(numk, 0);
vector<double> sigmay(numk, 0);
vector<int> clustersize(numk, 0);
vector<double> inv(numk, 0);//to store 1/clustersize[k] values
vector<double> distxy(sz, DBL_MAX);
vector<double> distlab(sz, DBL_MAX);
vector<double> distvec(sz, DBL_MAX);
vector<double> maxlab(numk, 10*10);//THIS IS THE VARIABLE VALUE OF M, just start with 10
vector<double> maxxy(numk, STEP*STEP);//THIS IS THE VARIABLE VALUE OF M, just start with 10
double invxywt = 1.0/(STEP*STEP);//NOTE: this is different from how usual SLIC/LKM works
while( numitr < NUMITR )
{
//------
//cumerr = 0;
numitr++;
//------
distvec.assign(sz, DBL_MAX);
for( int n = 0; n < numk; n++ )
{
int y1 = std::max(0, (int)(kseedsy[n]-offset));
int y2 = std::min(m_height, (int)(kseedsy[n]+offset));
int x1 = std::max(0, (int)(kseedsx[n]-offset));
int x2 = std::min(m_width, (int)(kseedsx[n]+offset));
for( int y = y1; y < y2; y++ )
{
for( int x = x1; x < x2; x++ )
{
int i = y*m_width + x;
_ASSERT( y < m_height && x < m_width && y >= 0 && x >= 0 );
double l = m_lvec[i];
double a = m_avec[i];
double b = m_bvec[i];
distlab[i] = (l - kseedsl[n])*(l - kseedsl[n]) +
(a - kseedsa[n])*(a - kseedsa[n]) +
(b - kseedsb[n])*(b - kseedsb[n]);
distxy[i] = (x - kseedsx[n])*(x - kseedsx[n]) +
(y - kseedsy[n])*(y - kseedsy[n]);
//------------------------------------------------------------------------
double dist = distlab[i]/maxlab[n] + distxy[i]*invxywt;//only varying m, prettier superpixels
//double dist = distlab[i]/maxlab[n] + distxy[i]/maxxy[n];//varying both m and S
//------------------------------------------------------------------------
if( dist < distvec[i] )
{
distvec[i] = dist;
klabels[i] = n;
}
}
}
}
//-----------------------------------------------------------------
// Assign the max color distance for a cluster
//-----------------------------------------------------------------
if(0 == numitr)
{
maxlab.assign(numk,1);
maxxy.assign(numk,1);
}
{for( int i = 0; i < sz; i++ )
{
if(maxlab[klabels[i]] < distlab[i]) maxlab[klabels[i]] = distlab[i];
if(maxxy[klabels[i]] < distxy[i]) maxxy[klabels[i]] = distxy[i];
}}
//-----------------------------------------------------------------
// Recalculate the centroid and store in the seed values
//-----------------------------------------------------------------
sigmal.assign(numk, 0);
sigmaa.assign(numk, 0);
sigmab.assign(numk, 0);
sigmax.assign(numk, 0);
sigmay.assign(numk, 0);
clustersize.assign(numk, 0);
for( int j = 0; j < sz; j++ )
{
int temp = klabels[j];
_ASSERT(klabels[j] >= 0);
sigmal[klabels[j]] += m_lvec[j];
sigmaa[klabels[j]] += m_avec[j];
sigmab[klabels[j]] += m_bvec[j];
sigmax[klabels[j]] += (j%m_width);
sigmay[klabels[j]] += (j/m_width);
clustersize[klabels[j]]++;
}
{for( int k = 0; k < numk; k++ )
{
//_ASSERT(clustersize[k] > 0);
if( clustersize[k] <= 0 ) clustersize[k] = 1;
inv[k] = 1.0/double(clustersize[k]);//computing inverse now to multiply, than divide later
}}
{for( int k = 0; k < numk; k++ )
{
kseedsl[k] = sigmal[k]*inv[k];
kseedsa[k] = sigmaa[k]*inv[k];
kseedsb[k] = sigmab[k]*inv[k];
kseedsx[k] = sigmax[k]*inv[k];
kseedsy[k] = sigmay[k]*inv[k];
}}
}
}
//===========================================================================
/// SaveSuperpixelLabels
///
/// Save labels in raster scan order.
//===========================================================================
void SLIC::SaveSuperpixelLabels(
const int* labels,
const int& width,
const int& height,
const string& filename,
const string& path)
{
int sz = width*height;
char fname[_MAX_FNAME];
char extn[_MAX_FNAME];
_splitpath(filename.c_str(), NULL, NULL, fname, extn);
string temp = fname;
ofstream outfile;
string finalpath = path + temp + string(".dat");
outfile.open(finalpath.c_str(), ios::binary);
for( int i = 0; i < sz; i++ )
{
outfile.write((const char*)&labels[i], sizeof(int));
}
outfile.close();
}
//===========================================================================
/// EnforceLabelConnectivity
///
/// 1. finding an adjacent label for each new component at the start
/// 2. if a certain component is too small, assigning the previously found
/// adjacent label to this component, and not incrementing the label.
//===========================================================================
void SLIC::EnforceLabelConnectivity(
const int* labels,//input labels that need to be corrected to remove stray labels
const int& width,
const int& height,
int* nlabels,//new labels
int& numlabels,//the number of labels changes in the end if segments are removed
const int& K) //the number of superpixels desired by the user
{
// const int dx8[8] = {-1, -1, 0, 1, 1, 1, 0, -1};
// const int dy8[8] = { 0, -1, -1, -1, 0, 1, 1, 1};
const int dx4[4] = {-1, 0, 1, 0};
const int dy4[4] = { 0, -1, 0, 1};
const int sz = width*height;
const int SUPSZ = sz/K;
//nlabels.resize(sz, -1);
for( int i = 0; i < sz; i++ ) nlabels[i] = -1;
int label(0);
int* xvec = new int[sz];
int* yvec = new int[sz];
int oindex(0);
int adjlabel(0);//adjacent label
for( int j = 0; j < height; j++ )
{
for( int k = 0; k < width; k++ )
{
if( 0 > nlabels[oindex] )
{
nlabels[oindex] = label;
//--------------------
// Start a new segment
//--------------------
xvec[0] = k;
yvec[0] = j;
//-------------------------------------------------------
// Quickly find an adjacent label for use later if needed
//-------------------------------------------------------
{for( int n = 0; n < 4; n++ )
{
int x = xvec[0] + dx4[n];
int y = yvec[0] + dy4[n];
if( (x >= 0 && x < width) && (y >= 0 && y < height) )
{
int nindex = y*width + x;
if(nlabels[nindex] >= 0) adjlabel = nlabels[nindex];
}
}}
int count(1);
for( int c = 0; c < count; c++ )
{
for( int n = 0; n < 4; n++ )
{
int x = xvec[c] + dx4[n];
int y = yvec[c] + dy4[n];
if( (x >= 0 && x < width) && (y >= 0 && y < height) )
{
int nindex = y*width + x;
if( 0 > nlabels[nindex] && labels[oindex] == labels[nindex] )
{
xvec[count] = x;
yvec[count] = y;
nlabels[nindex] = label;
count++;
}
}
}
}
//-------------------------------------------------------
// If segment size is less then a limit, assign an
// adjacent label found before, and decrement label count.
//-------------------------------------------------------
if(count <= SUPSZ >> 2)
{
for( int c = 0; c < count; c++ )
{
int ind = yvec[c]*width+xvec[c];
nlabels[ind] = adjlabel;
}
label--;
}
label++;
}
oindex++;
}
}
numlabels = label;
if(xvec) delete [] xvec;
if(yvec) delete [] yvec;
}
//===========================================================================
/// PerformSLICO_ForGivenStepSize
///
/// There is option to save the labels if needed.
//===========================================================================
void SLIC::PerformSLICO_ForGivenStepSize(
const unsigned int* ubuff,
const int width,
const int height,
int* klabels,
int& numlabels,
const int& STEP,
const double& m)
{
vector<double> kseedsl(0);
vector<double> kseedsa(0);
vector<double> kseedsb(0);
vector<double> kseedsx(0);
vector<double> kseedsy(0);
//--------------------------------------------------
m_width = width;
m_height = height;
int sz = m_width*m_height;
//klabels.resize( sz, -1 );
//--------------------------------------------------
//klabels = new int[sz];
for( int s = 0; s < sz; s++ ) klabels[s] = -1;
//--------------------------------------------------
DoRGBtoLABConversion(ubuff, m_lvec, m_avec, m_bvec);
//--------------------------------------------------
bool perturbseeds(true);
vector<double> edgemag(0);
if(perturbseeds) DetectLabEdges(m_lvec, m_avec, m_bvec, m_width, m_height, edgemag);
GetLABXYSeeds_ForGivenStepSize(kseedsl, kseedsa, kseedsb, kseedsx, kseedsy, STEP, perturbseeds, edgemag);
PerformSuperpixelSegmentation_VariableSandM(kseedsl,kseedsa,kseedsb,kseedsx,kseedsy,klabels,STEP,10);
numlabels = kseedsl.size();
int* nlabels = new int[sz];
EnforceLabelConnectivity(klabels, m_width, m_height, nlabels, numlabels, double(sz)/double(STEP*STEP));
{for(int i = 0; i < sz; i++ ) klabels[i] = nlabels[i];}
if(nlabels) delete [] nlabels;
}
//===========================================================================
/// PerformSLICO_ForGivenK
///
/// Zero parameter SLIC algorithm for a given number K of superpixels.
//===========================================================================
void SLIC::PerformSLICO_ForGivenK(
const unsigned int* ubuff,
const int width,
const int height,
int* klabels,
int& numlabels,
const int& K,//required number of superpixels
const double& m)//weight given to spatial distance
{
vector<double> kseedsl(0);
vector<double> kseedsa(0);
vector<double> kseedsb(0);
vector<double> kseedsx(0);
vector<double> kseedsy(0);
//--------------------------------------------------
m_width = width;
m_height = height;
int sz = m_width*m_height;
//--------------------------------------------------
//if(0 == klabels) klabels = new int[sz];
for( int s = 0; s < sz; s++ ) klabels[s] = -1;
//--------------------------------------------------
if(1)//LAB
{
DoRGBtoLABConversion(ubuff, m_lvec, m_avec, m_bvec);
}
else//RGB
{
m_lvec = new double[sz]; m_avec = new double[sz]; m_bvec = new double[sz];
for( int i = 0; i < sz; i++ )
{
m_lvec[i] = ubuff[i] >> 16 & 0xff;
m_avec[i] = ubuff[i] >> 8 & 0xff;
m_bvec[i] = ubuff[i] & 0xff;
}
}
//--------------------------------------------------
bool perturbseeds(true);
vector<double> edgemag(0);
if(perturbseeds) DetectLabEdges(m_lvec, m_avec, m_bvec, m_width, m_height, edgemag);
GetLABXYSeeds_ForGivenK(kseedsl, kseedsa, kseedsb, kseedsx, kseedsy, K, perturbseeds, edgemag);
int STEP = sqrt(double(sz)/double(K)) + 2.0;//adding a small value in the even the STEP size is too small.
//PerformSuperpixelSLIC(kseedsl, kseedsa, kseedsb, kseedsx, kseedsy, klabels, STEP, edgemag, m);
PerformSuperpixelSegmentation_VariableSandM(kseedsl,kseedsa,kseedsb,kseedsx,kseedsy,klabels,STEP,10);
numlabels = kseedsl.size();
int* nlabels = new int[sz];
EnforceLabelConnectivity(klabels, m_width, m_height, nlabels, numlabels, K);
{for(int i = 0; i < sz; i++ ) klabels[i] = nlabels[i];}
if(nlabels) delete [] nlabels;
}
void SLIC::PerformSLICO_ForGivenK(
const unsigned char* ubuff,