@@ -25,12 +25,12 @@ namespace OpenCvSharp.Blob
25
25
/// <summary>
26
26
/// Blob set
27
27
/// </summary>
28
- public class CvBlobs : Dictionary < int , CvBlob >
28
+ public class CvBlobs : Dictionary < int , CvBlob > , ICloneable
29
29
{
30
30
/// <summary>
31
31
/// Label values
32
32
/// </summary>
33
- public LabelData Labels { get ; protected set ; }
33
+ public LabelData Labels { get ; set ; }
34
34
35
35
/// <summary>
36
36
/// Constructor (init only)
@@ -39,6 +39,26 @@ public CvBlobs()
39
39
{
40
40
}
41
41
42
+ /// <summary>
43
+ /// Constructor (copy)
44
+ /// </summary>
45
+ public CvBlobs ( IEnumerable < KeyValuePair < int , CvBlob > > blobData , int [ , ] labelData )
46
+ {
47
+ foreach ( KeyValuePair < int , CvBlob > pair in blobData )
48
+ {
49
+ Add ( pair . Key , pair . Value ) ;
50
+ }
51
+ Labels = new LabelData ( labelData ) ;
52
+ }
53
+
54
+ /// <summary>
55
+ /// Constructor (copy)
56
+ /// </summary>
57
+ public CvBlobs ( IEnumerable < KeyValuePair < int , CvBlob > > blobData , LabelData labelData )
58
+ : this ( blobData , labelData . Values )
59
+ {
60
+ }
61
+
42
62
/// <summary>
43
63
/// Constructor (init and cvLabel)
44
64
/// </summary>
@@ -373,24 +393,26 @@ public void UpdateTracks(CvTracks tracks, double thDistance, int thInactive, int
373
393
i ++ ;
374
394
}
375
395
376
- int maxTrackID = 0 ;
396
+ int maxTrackId = 0 ;
377
397
int j = 0 ;
378
398
foreach ( CvTrack track in tracks . Values )
379
399
{
380
400
close . AT [ j ] = 0 ;
381
- close . AT [ j ] = track . Id ;
382
- if ( track . Id > maxTrackID )
383
- maxTrackID = track . Id ;
401
+ close . IT [ j ] = track . Id ;
402
+ if ( track . Id > maxTrackId )
403
+ maxTrackId = track . Id ;
384
404
j ++ ;
385
405
}
386
406
387
- // Proximity matrix calculation and "used blob" list inicialization :
407
+ // Proximity matrix calculation and "used blob" list initialization :
388
408
for ( i = 0 ; i < nBlobs ; i ++ )
389
409
{
390
410
for ( j = 0 ; j < nTracks ; j ++ )
391
411
{
392
- CvBlob b = this [ close . IB [ i ] ] ;
393
- CvTrack t = tracks [ close . IT [ j ] ] ;
412
+ int ib = close . IB [ i ] ;
413
+ int it = close . IT [ j ] ;
414
+ CvBlob b = this [ ib ] ;
415
+ CvTrack t = tracks [ it ] ;
394
416
close [ i , j ] = ( DistantBlobTrack ( b , t ) < thDistance ) ? 1 : 0 ;
395
417
if ( close [ i , j ] < thDistance )
396
418
{
@@ -426,11 +448,12 @@ public void UpdateTracks(CvTracks tracks, double thDistance, int thInactive, int
426
448
//cout << *B(i) << endl;
427
449
428
450
// New track.
429
- maxTrackID ++ ;
430
- CvBlob blob = this [ i + 1 ] ;
451
+ maxTrackId ++ ;
452
+ int ib = close . IB [ i ] ;
453
+ CvBlob blob = this [ ib ] ;
431
454
CvTrack track = new CvTrack
432
455
{
433
- Id = maxTrackID ,
456
+ Id = maxTrackId ,
434
457
Label = blob . Label ,
435
458
MinX = blob . MinX ,
436
459
MinY = blob . MinY ,
@@ -441,7 +464,8 @@ public void UpdateTracks(CvTracks tracks, double thDistance, int thInactive, int
441
464
Active = 0 ,
442
465
Inactive = 0 ,
443
466
} ;
444
- tracks [ maxTrackID ] = track ;
467
+ tracks [ maxTrackId ] = track ;
468
+ //maxTrackId++;
445
469
}
446
470
}
447
471
@@ -451,8 +475,11 @@ public void UpdateTracks(CvTracks tracks, double thDistance, int thInactive, int
451
475
int c = close . AT [ j ] ;
452
476
if ( c != 0 )
453
477
{
454
- List < CvTrack > tt = new List < CvTrack > { tracks [ j ] } ;
455
- List < CvBlob > bb = new List < CvBlob > ( ) ;
478
+ var tt = new List < CvTrack > ( ) ;
479
+ var bb = new List < CvBlob > ( ) ;
480
+
481
+ int it = close . IT [ j ] ;
482
+ tt . Add ( tracks [ it ] ) ;
456
483
457
484
GetClusterForTrack ( j , close , nBlobs , nTracks , this , tracks , bb , tt ) ;
458
485
@@ -482,7 +509,8 @@ public void UpdateTracks(CvTracks tracks, double thDistance, int thInactive, int
482
509
}
483
510
484
511
if ( blob == null || track == null )
485
- throw new NotSupportedException ( ) ;
512
+ //throw new Exception();
513
+ continue ;
486
514
487
515
// Update track
488
516
track . Label = blob . Label ;
@@ -591,15 +619,57 @@ private double DistantBlobTrack(CvBlob b, CvTrack t)
591
619
return Math . Min ( d1 , d2 ) ;
592
620
}
593
621
622
+ private static void GetClusterForBlob ( int blobPos , ProximityMatrix close ,
623
+ int nBlobs , int nTracks , CvBlobs blobs , CvTracks tracks ,
624
+ List < CvBlob > bb , List < CvTrack > tt )
625
+ {
626
+ retry :
627
+ var retryList = new List < int > ( ) ;
628
+
629
+ for ( int j = 0 ; j < nTracks ; j ++ )
630
+ {
631
+ if ( close [ blobPos , j ] != 0 )
632
+ {
633
+ int it = close . IT [ j ] ;
634
+ tt . Add ( tracks [ it ] ) ;
635
+
636
+ int c = close . AT [ j ] ;
637
+
638
+ close [ blobPos , j ] = 0 ;
639
+ close . AB [ blobPos ] -- ;
640
+ close . AT [ j ] -- ;
641
+
642
+ if ( c > 1 )
643
+ {
644
+ retryList . Add ( j ) ;
645
+ //GetClusterForTrack(j, close, nBlobs, nTracks, blobs, tracks, bb, tt);
646
+ }
647
+ }
648
+ }
649
+
650
+ if ( retryList . Count > 0 )
651
+ {
652
+ foreach ( int j in retryList )
653
+ {
654
+ GetClusterForTrack ( j , close , nBlobs , nTracks , blobs , tracks , bb , tt ) ;
655
+ }
656
+ goto retry ;
657
+ }
658
+ }
659
+
594
660
private static void GetClusterForTrack ( int trackPos , ProximityMatrix close ,
595
661
int nBlobs , int nTracks , CvBlobs blobs ,
596
662
CvTracks tracks , List < CvBlob > bb , List < CvTrack > tt )
597
663
{
664
+ retry :
665
+ var retryList = new List < int > ( ) ;
666
+
598
667
for ( int i = 0 ; i < nBlobs ; i ++ )
599
668
{
600
669
if ( close [ i , trackPos ] != 0 )
601
670
{
602
- bb . Add ( blobs [ i ] ) ;
671
+ int ib = close . IB [ i ] ;
672
+ bb . Add ( blobs [ ib ] ) ;
603
673
604
674
int c = close . AB [ i ] ;
605
675
@@ -609,37 +679,48 @@ private static void GetClusterForTrack(int trackPos, ProximityMatrix close,
609
679
610
680
if ( c > 1 )
611
681
{
612
- GetClusterForBlob ( i , close , nBlobs , nTracks , blobs , tracks , bb , tt ) ;
682
+ retryList . Add ( i ) ;
683
+ //GetClusterForBlob(i, close, nBlobs, nTracks, blobs, tracks, bb, tt);
613
684
}
614
685
}
615
686
}
616
- }
617
687
618
- private static void GetClusterForBlob ( int blobPos , ProximityMatrix close ,
619
- int nBlobs , int nTracks , CvBlobs blobs , CvTracks tracks ,
620
- List < CvBlob > bb , List < CvTrack > tt )
621
- {
622
- for ( int j = 0 ; j < nTracks ; j ++ )
688
+ if ( retryList . Count > 0 )
623
689
{
624
- if ( close [ blobPos , j ] != 0 )
690
+ foreach ( int i in retryList )
625
691
{
626
- tt . Add ( tracks [ j ] ) ;
692
+ GetClusterForBlob ( i , close , nBlobs , nTracks , blobs , tracks , bb , tt ) ;
693
+ }
694
+ goto retry ;
695
+ }
696
+ }
627
697
628
- int c = close . AT [ j ] ;
698
+ #endregion
629
699
630
- close [ blobPos , j ] = 0 ;
631
- close . AB [ blobPos ] -- ;
632
- close . AT [ j ] -- ;
700
+ #endregion
633
701
634
- if ( c > 1 )
635
- {
636
- GetClusterForTrack ( j , close , nBlobs , nTracks , blobs , tracks , bb , tt ) ;
637
- }
638
- }
702
+ #region ICloneable
703
+
704
+ /// <summary>
705
+ ///
706
+ /// </summary>
707
+ /// <returns></returns>
708
+ public CvBlobs Clone ( )
709
+ {
710
+ var newBlobs = new CvBlobs ( ) ;
711
+ foreach ( KeyValuePair < int , CvBlob > pair in this )
712
+ {
713
+ newBlobs . Add ( pair . Key , pair . Value ) ;
639
714
}
715
+ newBlobs . Labels = Labels . Clone ( ) ;
716
+
717
+ return newBlobs ;
640
718
}
641
719
642
- #endregion
720
+ object ICloneable . Clone ( )
721
+ {
722
+ return Clone ( ) ;
723
+ }
643
724
644
725
#endregion
645
726
}
0 commit comments