-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlabeling.cpp
807 lines (681 loc) · 20.4 KB
/
labeling.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
/*
labeling.cpp
Copyright 2012 Julien Mille ([email protected])
*/
#include "labeling.h"
#include <string.h>
#include <float.h>
#include <stdio.h>
#include <iostream>
#include <list>
#include <vector>
using namespace std;
/**********************************************************************
* CLabeling *
**********************************************************************/
CArray1D<CLabelClassProperties> CLabeling::vectClassesProperties;
CLabeling::CLabeling()
{
// Draw parameters
fDisplayOpacity = 0.5f;
bDisplayRegionPixels = true;
bDisplayBand = false;
bDisplayBoundary = false;
}
bool CLabeling::InitClassesProperties()
{
FILE *pFileConfig;
int iIndexClassMax, iIndexClass, iLine; //, iChar;
char strLine[500], *strId;
pFileConfig = fopen("classes_config.txt", "rb");
if (pFileConfig==NULL)
{
cout<<"ERROR: cannot open classes_config.txt"<<endl;
return false;
}
else
cout<<"Parsing file classes_config.txt..."<<endl;
iLine = 1;
iIndexClassMax = -1;
while (fgets(strLine, 500, pFileConfig)!=NULL)
{
// iChar = 0;
strId = strstr(strLine, "id=");
if (strId!=NULL)
sscanf(strId+3, "%d", &iIndexClass);
if (iIndexClass>iIndexClassMax)
iIndexClassMax = iIndexClass;
iLine++;
}
fclose(pFileConfig);
if (iIndexClassMax<=0)
{
cout<<"ERROR: invalid maximum class index"<<endl;
return false;
}
vectClassesProperties.Init(iIndexClassMax+1);
vectClassesProperties[0].strClassName="Unlabeled";
vectClassesProperties[0].rgbClassColor.Set(0,0,0);
for (iIndexClass=1; iIndexClass<=iIndexClassMax; iIndexClass++)
{
vectClassesProperties[iIndexClass].strClassName="Unknown";
vectClassesProperties[iIndexClass].rgbClassColor.Set(0,0,0);
}
pFileConfig = fopen("classes_config.txt", "rb");
if (pFileConfig==NULL)
{
cout<<"ERROR: cannot reopen classes_config.txt"<<endl;
return false;
}
iLine = 1;
while (fgets(strLine, 500, pFileConfig)!=NULL)
{
// iChar = 0;
strId = strstr(strLine, "id=");
if (strId!=NULL)
{
sscanf(strId+3, "%d", &iIndexClass);
if (iIndexClass>0 && iIndexClass<=iIndexClassMax)
{
CLabelClassProperties *pProp = &(vectClassesProperties[iIndexClass]);
char *strNameLoc;
strNameLoc = strstr(strLine, "name=");
if (strNameLoc!=NULL)
{
if (strNameLoc[5]=='\"')
{
char *strEndQuote;
strNameLoc+=6;
strEndQuote = strstr(strNameLoc,"\"");
if (strEndQuote!=NULL)
{
char strName[500];
strEndQuote[0] = 0;
strcpy(strName, strNameLoc);
pProp->strClassName = strName;
strEndQuote[0] = '\"';
char *strRgbLoc;
strRgbLoc = strstr(strLine, "rgb=");
if (strRgbLoc!=NULL)
{
int iRed, iGreen, iBlue;
sscanf(strRgbLoc+5, "%d,%d,%d", &iRed, &iGreen, &iBlue);
if (iRed>=0 && iRed<=255)
pProp->rgbClassColor.byteRed = (unsigned char)iRed;
else
cout<<"WARNING: line "<<iLine<<": invalid red value in 'rgb' field"<<endl;
if (iGreen>=0 && iGreen<=255)
pProp->rgbClassColor.byteGreen = (unsigned char)iGreen;
else
cout<<"WARNING: line "<<iLine<<": invalid green value in 'rgb' field"<<endl;
if (iBlue>=0 && iBlue<=255)
pProp->rgbClassColor.byteBlue = (unsigned char)iBlue;
else
cout<<"WARNING: line "<<iLine<<": invalid blue value in 'rgb' field"<<endl;
cout<<"Class id="<<iIndexClass<<" name="<<pProp->strClassName<<" rgb=("<<iRed<<","<<iGreen<<","<<iBlue<<")"<<endl;
}
else
cout<<"WARNING: line "<<iLine<<": cannot find 'rgb' field"<<endl;
}
else
cout<<"WARNING: line "<<iLine<<": cannot find ending '\"' for 'name' field"<<endl;
}
else
cout<<"WARNING: line "<<iLine<<": expected '\"' after 'name='"<<endl;
}
else
cout<<"WARNING: line "<<iLine<<": cannot find 'name' field"<<endl;
}
else
cout<<"WARNING: line "<<iLine<<": invalid id="<<iIndexClass<<endl;
}
else {
cout<<"WARNING: line "<<iLine<<": cannot find 'id' field"<<endl;
}
iLine++;
}
fclose(pFileConfig);
return true;
}
void CLabeling::InitCircle(const CCouple<float> &pfCenter, float fRadius, unsigned int uiLabel)
{
CCouple<int> piCenter, p, pmin, pmax;
int iRadius, y2;
if (uiLabel==0)
{
cerr<<"ERROR in CLabeling::InitCircle(...): not for background region"<<endl;
return;
}
EmptyRegion(uiLabel);
piCenter = (CCouple<int>)pfCenter;
iRadius = (int)fRadius;
if (piCenter.y<iRadius)
pmin.y = -piCenter.y;
else pmin.y = -iRadius;
if (piCenter.y+iRadius>=iHeight)
pmax.y = iHeight-piCenter.y-1;
else pmax.y = iRadius;
// Fill circle
for (p.y=pmin.y; p.y<=pmax.y; p.y++)
{
y2 = (int)sqrt((float)(iRadius*iRadius-p.y*p.y));
if (piCenter.x<y2)
pmin.x = -piCenter.x;
else pmin.x = -y2;
if (piCenter.x+y2>=iWidth)
pmax.x = iWidth-piCenter.x-1;
else pmax.x = y2;
for (p.x=pmin.x; p.x<=pmax.x; p.x++)
if (Element(piCenter+p).HasLabel(0)==true)
SetPixelLabel(piCenter + p, uiLabel);
}
}
void CLabeling::InitEllipse(const CCouple<float> &pfCenter, float fRadiusX, float fRadiusY, unsigned int uiLabel)
{
CCouple<int> piCenter, p, pmin, pmax;
int iRadiusX, iRadiusY, y2;
if (uiLabel==0)
{
cerr<<"ERROR in CLabeling::InitCircle(...): not for background region"<<endl;
return;
}
EmptyRegion(uiLabel);
piCenter = (CCouple<int>)pfCenter;
iRadiusX = (int)fRadiusX;
iRadiusY = (int)fRadiusY;
if (piCenter.y<iRadiusY)
pmin.y = -piCenter.y;
else pmin.y = -iRadiusY;
if (piCenter.y+iRadiusY>=iHeight)
pmax.y = iHeight-piCenter.y-1;
else pmax.y = iRadiusY;
// Fill circle
for (p.y=pmin.y; p.y<=pmax.y; p.y++)
{
y2 = (int)sqrt((float)((iRadiusY*iRadiusY-p.y*p.y)*iRadiusX)/(float)iRadiusY);
if (piCenter.x<y2)
pmin.x = -piCenter.x;
else pmin.x = -y2;
if (piCenter.x+y2>=iWidth)
pmax.x = iWidth-piCenter.x-1;
else pmax.x = y2;
for (p.x=pmin.x; p.x<=pmax.x; p.x++)
if (Element(piCenter+p).HasLabel(0)==true)
SetPixelLabel(piCenter + p, uiLabel);
}
}
void CLabeling::InitFromBinaryImage(const cv::Mat &imgInit, unsigned int uiLabel)
{
CCouple<int> p;
CImage2DPixel *pBits;
int iOffsetEndRow;
if (uiLabel==0)
{
cerr<<"ERROR in CLabeling::InitCircle(...): not for background region"<<endl;
return;
}
EmptyRegion(uiLabel);
if (imgInit.GetBitsPerPixel()!=8)
{
cerr<<"ERROR in CLabeling::InitFromBinaryImage(...): input image is not 8-bit."<<endl;
return;
}
if (imgInit.GetWidth()!=iWidth || imgInit.GetHeight()!=iHeight)
{
cerr<<"ERROR in CLabeling::InitFromBinaryImage(...): input image size does not match with region size."<<endl;
return;
}
iOffsetEndRow = imgInit.GetBytesPerLine() - imgInit.GetWidth();
pBits = imgInit.GetBits();
for (p.y=0; p.y<imgInit.GetHeight(); p.y++)
{
for (p.x=0; p.x<imgInit.GetWidth(); p.x++)
{
if (*pBits>127)
SetPixelLabel(p, uiLabel);
pBits++;
}
pBits += iOffsetEndRow;
}
}
void CLabeling::InitFromBinaryImageWithOffset(const cv::Mat &imgInit, const CCouple<int> &piOffset, unsigned int uiLabel)
{
CCouple<int> p, piMin, piMax;
CImage2DPixel *pBits;
int iOffsetEndRow;
if (uiLabel==0)
{
cerr<<"ERROR in CLabeling::InitCircle(...): not for background region"<<endl;
return;
}
EmptyRegion(uiLabel);
if (imgInit.GetBitsPerPixel()!=8)
{
cerr<<"ERROR in CLabeling::InitFromBinaryImageWithOffset(...): input image is not 8-bit."<<endl;
return;
}
iOffsetEndRow = imgInit.GetBytesPerLine() - imgInit.GetWidth();
pBits = imgInit.GetBits();
piMin = -piOffset;
piMax = CCouple<int>(iWidth-1, iHeight-1)-piOffset;
for (p.y=0; p.y<imgInit.GetHeight(); p.y++)
{
for (p.x=0; p.x<imgInit.GetWidth(); p.x++)
{
if (*pBits==255 && p.IsInRange(piMin, piMax))
SetPixelLabel(p + piOffset, uiLabel);
pBits++;
}
pBits += iOffsetEndRow;
}
}
void CLabeling::CopyToBinaryImage(cv::Mat &imgOutput, unsigned int uiLabel) const
{
CCouple<int> p;
CImage2DPixel *pBits;
int iOffsetEndRow;
if (imgOutput.GetBitsPerPixel()!=8 || imgOutput.GetSize()!=GetSize())
{
if (imgOutput.Create(iWidth, iHeight, 8)==false)
return;
}
iOffsetEndRow = imgOutput.GetBytesPerLine() - iWidth;
pBits = imgOutput.GetBits();
for (p.y=0; p.y<iHeight; p.y++)
{
for (p.x=0; p.x<iWidth; p.x++)
{
if (Element(p).HasLabel(uiLabel))
*pBits = 255;
else
*pBits = 0;
pBits++;
}
pBits += iOffsetEndRow;
}
}
/*
void CLabeling::FillHoles(unsigned int uiLabel)
{
cv::Mat imgFill;
CLabelPixel *pPixel;
CImage2DPixel *pBits;
CCouple<int> piCurrent, piStart, piEnd;
int iImageWidth, iImageHeight, iOffsetEndRow, iOffsetEndRowImage;
if (vectRegionsProperties[uiLabel]->iNbPixels==0)
return;
piStart = coupleMax(vectRegionsProperties[uiLabel]->puiLabelMin - CCouple<int>(1), CCouple<int>(0));
piEnd = coupleMin(vectRegionsProperties[uiLabel]->puiLabelMax + CCouple<int>(1), CCouple<int>(iWidth-1, iHeight-1));
iImageWidth = piEnd.x - piStart.x + 1;
iImageHeight = piEnd.y - piStart.y + 1;
imgFill.Create(iImageWidth, iImageHeight, 8);
iOffsetEndRow = iWidth - iImageWidth;
iOffsetEndRowImage = imgFill.GetBytesPerLine() - iImageWidth;
pPixel = this->pElements + GetOffset(piStart);
pBits = imgFill.GetBits();
for (piCurrent.y=piStart.y; piCurrent.y<=piEnd.y; piCurrent.y++)
{
for (piCurrent.x=piStart.x; piCurrent.x<=piEnd.x; piCurrent.x++)
{
if (pPixel->IsInRegion(uiLabel)==true)
*pBits = 255;
else
*pBits = 0;
pPixel++;
pBits++;
}
pPixel += iOffsetEndRow;
pBits += iOffsetEndRowImage;
}
imgFill.RegionFill(CCouple<int>(0), 127);
pBits = imgFill.GetBits();
for (piCurrent.y=piStart.y; piCurrent.y<=piEnd.y; piCurrent.y++)
{
for (piCurrent.x=piStart.x; piCurrent.x<=piEnd.x; piCurrent.x++)
{
if (*pBits==0)
SetPixelLabel(piCurrent, uiLabel);
pBits++;
}
pBits += iOffsetEndRowImage;
}
UpdateAfterEvolution();
}*/
void CLabeling::AddCircle(const CCouple<int> &piCenter, int iRadius, unsigned int uiLabel, bool bReplaceAll)
{
CCouple<int> p, pmin, pmax;
int y2;
if (uiLabel==0)
{
cerr<<"WARNING in CLabeling::AddCircle(...): cannot add pixels in background region"<<endl;
return;
}
if (piCenter.y<iRadius)
pmin.y = -piCenter.y;
else pmin.y = -iRadius;
if (piCenter.y+iRadius>=iHeight)
pmax.y = iHeight-piCenter.y-1;
else pmax.y = iRadius;
// Fill circle
for (p.y=pmin.y; p.y<=pmax.y; p.y++)
{
y2 = (int)sqrt((float)(iRadius*iRadius-p.y*p.y));
if (piCenter.x<y2)
pmin.x = -piCenter.x;
else pmin.x = -y2;
if (piCenter.x+y2>=iWidth)
pmax.x = iWidth-piCenter.x-1;
else pmax.x = y2;
for (p.x=pmin.x; p.x<=pmax.x; p.x++)
if ((bReplaceAll==false && Element(piCenter+p).HasLabel(0)) || bReplaceAll==true)
SetPixelLabel(piCenter + p, uiLabel);
}
}
void CLabeling::RemoveCircle(const CCouple<int> &piCenter, int iRadius, unsigned int uiLabel, bool bRemoveAll)
{
CCouple<int> p, pmin, pmax;
int y2;
if (uiLabel==0)
{
cerr<<"WARNING in CLabeling::removeCircle(...): cannot remove pixels from background region"<<endl;
return;
}
if (piCenter.y<iRadius)
pmin.y = -piCenter.y;
else pmin.y = -iRadius;
if (piCenter.y+iRadius>=iHeight)
pmax.y = iHeight-piCenter.y-1;
else pmax.y = iRadius;
// Fill circle
for (p.y=pmin.y; p.y<=pmax.y; p.y++)
{
y2 = (int)sqrt((float)(iRadius*iRadius-p.y*p.y));
if (piCenter.x<y2)
pmin.x = -piCenter.x;
else pmin.x = -y2;
if (piCenter.x+y2>=iWidth)
pmax.x = iWidth-piCenter.x-1;
else pmax.x = y2;
for (p.x=pmin.x; p.x<=pmax.x; p.x++)
if ((bRemoveAll==false && Element(piCenter + p).HasLabel(uiLabel)) || bRemoveAll==true)
SetPixelLabel(piCenter + p, 0);
}
}
void CLabeling::EmptyRegion(unsigned int uiLabel)
{
CLabelPixel *pPixel;
CCouple<int> piCurrent;
// Set all pixels out of region
pPixel = pElements;
for (piCurrent.y=0; piCurrent.y<iHeight; piCurrent.y++)
{
for (piCurrent.x=0; piCurrent.x<iWidth; piCurrent.x++)
{
if (pPixel->HasLabel(uiLabel))
SetPixelLabel(piCurrent, 0);
pPixel++;
}
}
}
void CLabeling::Empty()
{
CLabelPixel *pPixel;
int i;
// Set all pixels out of region
pPixel = pElements;
for (i=0; i<iSize; i++)
{
pPixel->SetLabel(0);
pPixel++;
}
}
bool CLabeling::IsEmpty()
{
CLabelPixel *pPixel;
// unsigned int uiLabel;
int i;
if (iSize==0)
return true;
// Check all pixels
pPixel = pElements;
for (i=0; i<iSize && pPixel->GetLabel()==0; i++)
pPixel++;
if (i==iSize)
return true;
else
return false;
}
bool CLabeling::InitFromOtherRegion(const CLabeling ®ionOther)
{
// CLabelPixel *pPixel;
// const CLabelPixel *pPixelOther;
// unsigned int uiLabel;
// int i;
if (regionOther.iWidth!=iWidth || regionOther.iHeight!=iHeight)
{
cout<<"ERROR in CLabeling::InitFromOtherRegion(...): regions have different sizes"<<endl;
return false;
}
// Check all pixels
/*
pPixel = pElements;
pPixelOther = regionOther.pElements;
for (i=0; i<iSize; i++)
{
*pPixel = *pPixelOther;
pPixel++;
pPixelOther++;
}*/
memcpy(pElements, regionOther.pElements, iSize*sizeof(CLabelPixel));
return true;
}
void CLabeling::MakeLabelImage(cv::Mat &imgOutput) const
{
CCouple<int> p;
CLabelPixel *pRegionPixel;
CImage2DPixel *pBits;
int iOffsetEndRow;
if (imgOutput.type()!=CV_8UC1 || imgOutput.GetSize()!=GetSize())
{
if (imgOutput.Create(iWidth, iHeight, 8)==false)
return;
}
iOffsetEndRow = imgOutput.GetBytesPerLine() - iWidth;
pBits = imgOutput.GetBits();
pRegionPixel = pElements;
for (p.y=0; p.y<iHeight; p.y++)
{
for (p.x=0; p.x<iWidth; p.x++)
{
*pBits = (unsigned char)(pRegionPixel->GetLabel());
pRegionPixel++;
pBits++;
}
pBits += iOffsetEndRow;
}
}
void CLabeling::MakeLabelImageRGB(cv::Mat &imgOutput) const
{
CCouple<int> p;
CLabelPixel *pRegionPixel;
CImage2DPixel *pBits;
int iOffsetEndRow;
if (imgOutput.GetBitsPerPixel()!=24 || imgOutput.GetSize()!=GetSize())
{
if (imgOutput.Create(iWidth, iHeight, 24)==false)
return;
}
iOffsetEndRow = imgOutput.GetBytesPerLine() - 3*iWidth;
pBits = imgOutput.GetBits();
pRegionPixel = pElements;
for (p.y=0; p.y<iHeight; p.y++)
{
for (p.x=0; p.x<iWidth; p.x++)
{
*((CImage2DByteRGBPixel *)pBits) = vectClassesProperties[pRegionPixel->GetLabel()].rgbClassColor;
pRegionPixel++;
pBits+=3;
}
pBits += iOffsetEndRow;
}
}
void CLabeling::InitFromLabelImage(const cv::Mat &imgLabel)
{
CCouple<int> p;
const CImage2DPixel *pBits;
int iOffsetEndRow;
Empty();
if (imgLabel.GetBitsPerPixel()!=8)
{
cerr<<"ERROR in CLabeling::InitFromLabelImage(...): input image is not 8-bit."<<endl;
return;
}
if (imgLabel.GetWidth()!=iWidth || imgLabel.GetHeight()!=iHeight)
{
cerr<<"ERROR in CLabeling::InitFromLabelImage(...): label image size does not match region size."<<endl;
return;
}
iOffsetEndRow = imgLabel.GetBytesPerLine() - imgLabel.GetWidth();
pBits = imgLabel.GetBits();
for (p.y=0; p.y<imgLabel.GetHeight(); p.y++)
{
for (p.x=0; p.x<imgLabel.GetWidth(); p.x++)
{
if (*pBits!=0)
{
if (*pBits>=vectClassesProperties.GetSize())
{
cerr<<"ERROR in CLabeling::InitFromLabelImage(...): pixel ("<<p.x<<","<<p.y<<") has invalid label "<<(unsigned int)(*pBits)<<"."<<endl;
Empty();
return;
}
else
SetPixelLabel(p, *pBits);
}
pBits++;
}
pBits += iOffsetEndRow;
}
}
void CLabeling::SetPixelLabel(const CCouple<int> &p, unsigned int uiLabelNew)
{
CLabelPixel *pPixel;
pPixel = pElements + GetOffset(p);
pPixel->SetLabel(uiLabelNew);
}
void CLabeling::GetConnectedComponents(CArray2D<int> &arrayCCIndices,
vector<pair<CCouple<int>, CCouple<int> > > &listCCBounds) const
{
unsigned int iCurrentLabel;
CLabelPixel *pPixel, *pPixelNew; //, *pPixelNeighbor;
int *pCCIndex, *pCCIndexNew, *pCCIndexNeighbor;
unsigned int i;
int iNbCC;
CCouple<int> p, seed;
pair<CCouple<int>, CCouple<int> > iMinMaxBound;
vector<CCouple<int> > nouveaux, nouveaux2;
nouveaux.reserve(2*(iWidth+iHeight));
nouveaux2.reserve(2*(iWidth+iHeight));
arrayCCIndices.Init(iWidth, iHeight);
arrayCCIndices.Fill(-1);
listCCBounds.reserve(200);
// borne_inf.init(0, 0);
// borne_sup.init(iWidth-1, iHeight-1);
// Voisinage 2D (4-connexe)
/*
CVecteur<CCouple<int> > voisinage2D(4);
voisinage2D[0].init(-1,0);
voisinage2D[1].init(1,0);
voisinage2D[2].init(0,-1);
voisinage2D[3].init(0,1);
// Offset correspondant au voisinage 2D (4-connexe)
CVecteur<int> offsets_voisinage2D(4);
offsets_voisinage2D[0] = -1;
offsets_voisinage2D[1] = 1;
offsets_voisinage2D[2] = -iWidth;
offsets_voisinage2D[3] = iWidth;
CVecteur<int> offsets_voisinage_image2D(4);
offsets_voisinage_image2D[0] = -1;
offsets_voisinage_image2D[1] = 1;
offsets_voisinage_image2D[2] = -iBytesPerLine;
offsets_voisinage_image2D[3] = iBytesPerLine;
*/
iNbCC = 0;
// iOffsetEndRow = iBytesPerLine - iWidth;
pPixel = pElements;
pCCIndex = arrayCCIndices.GetBuffer();
for (seed.y=0;seed.y<iHeight;seed.y++)
{
for (seed.x=0;seed.x<iWidth;seed.x++)
{
if (pPixel->GetLabel()!=0 && *pCCIndex==-1)
{
// New region
*pCCIndex = iNbCC;
iCurrentLabel = pPixel->GetLabel();
nouveaux.push_back(seed);
iMinMaxBound.first = seed;
iMinMaxBound.second = seed;
while (nouveaux.size()!=0)
{
for (i=0;i<nouveaux.size();i++)
{
p = nouveaux[i];
pPixelNew = pElements + iWidth*p.y + p.x;
pCCIndexNew = arrayCCIndices.GetBuffer() + iWidth*p.y + p.x;
if (p.x>0)
{
pCCIndexNeighbor = pCCIndexNew-1;
if (*pCCIndexNeighbor==-1 && (pPixelNew-1)->GetLabel()==iCurrentLabel)
{
*pCCIndexNeighbor = iNbCC;
nouveaux2.push_back(CCouple<int>(p.x-1, p.y));
if (iMinMaxBound.first.x==p.x) // Decrease minimum x
iMinMaxBound.first.x--;
}
}
if (p.x<iWidth-1)
{
pCCIndexNeighbor = pCCIndexNew+1;
if (*pCCIndexNeighbor==-1 && (pPixelNew+1)->GetLabel()==iCurrentLabel)
{
*pCCIndexNeighbor = iNbCC;
nouveaux2.push_back(CCouple<int>(p.x+1, p.y));
if (iMinMaxBound.second.x==p.x) // Increase maximum x
iMinMaxBound.second.x++;
}
}
if (p.y>0)
{
pCCIndexNeighbor = pCCIndexNew-iWidth;
if (*pCCIndexNeighbor==-1 && (pPixelNew-iWidth)->GetLabel()==iCurrentLabel)
{
*pCCIndexNeighbor = iNbCC;
nouveaux2.push_back(CCouple<int>(p.x, p.y-1));
if (iMinMaxBound.first.y==p.y) // Decrease minimum y
iMinMaxBound.first.y--;
}
}
if (p.y<iHeight-1)
{
pCCIndexNeighbor = pCCIndexNew+iWidth;
if (*pCCIndexNeighbor==-1 && (pPixelNew+iWidth)->GetLabel()==iCurrentLabel)
{
*pCCIndexNeighbor = iNbCC;
nouveaux2.push_back(CCouple<int>(p.x, p.y+1));
if (iMinMaxBound.second.y==p.y) // Increase maximum y
iMinMaxBound.second.y++;
}
}
}
nouveaux = nouveaux2;
nouveaux2.clear();
}
listCCBounds.push_back(iMinMaxBound);
iNbCC++;
}
pPixel++;
pCCIndex++;
}
}
}