Skip to content

Commit 45d36c1

Browse files
committed
Merge branch 'BlobImprovement'
2 parents 328636c + 9d7c138 commit 45d36c1

File tree

5 files changed

+200
-42
lines changed

5 files changed

+200
-42
lines changed

nuget/OpenCvSharp-AnyCPU.nuspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<package xmlns="http://schemas.microsoft.com/packaging/2011/08/nuspec.xsd">
33
<metadata>
44
<id>OpenCvSharp-AnyCPU</id>
5-
<version>2.4.8.20140607</version>
5+
<version>2.4.8.20140609</version>
66
<title>OpenCvSharp</title>
77
<authors>shimat</authors>
88
<licenseUrl>http://opensource.org/licenses/BSD-3-Clause</licenseUrl>

src/OpenCvSharp.Blob/CvBlob.cs

Lines changed: 48 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ namespace OpenCvSharp.Blob
66
/// <summary>
77
/// Struct that contain information about one blob.
88
/// </summary>
9-
public class CvBlob
9+
[Serializable]
10+
public class CvBlob : ICloneable
1011
{
1112
#region Init
1213
/// <summary>
@@ -154,11 +155,11 @@ public CvRect Rect
154155
/// <summary>
155156
/// Hu moment 1.
156157
/// </summary>
157-
public double P1;
158+
public double P1 { get; set; }
158159
/// <summary>
159160
/// Hu moment 2.
160161
/// </summary>
161-
public double P2;
162+
public double P2 { get; set; }
162163

163164
/// <summary>
164165
/// Contour
@@ -250,5 +251,49 @@ public void SetMoments()
250251
}
251252
#endregion
252253
#endregion
254+
255+
#region ICloneable
256+
257+
/// <summary>
258+
///
259+
/// </summary>
260+
/// <returns></returns>
261+
public CvBlob Clone()
262+
{
263+
return new CvBlob
264+
{
265+
Area = Area,
266+
CentralMoments = CentralMoments,
267+
Centroid = Centroid,
268+
Contour = Contour.Clone(),
269+
InternalContours = new List<CvContourChainCode>(InternalContours),
270+
Label = Label,
271+
M00 = M00,
272+
M01 = M01,
273+
M02 = M02,
274+
M10 = M10,
275+
M11 = M11,
276+
M20 = M20,
277+
MaxX = MaxX,
278+
MaxY = MaxY,
279+
MinX = MinX,
280+
MinY = MinY,
281+
N02 = N02,
282+
N11 = N11,
283+
N20 = N20,
284+
P1 = P1,
285+
P2 = P2,
286+
U02 = U02,
287+
U11 = U11,
288+
U20 = U20,
289+
};
290+
}
291+
292+
object ICloneable.Clone()
293+
{
294+
return Clone();
295+
}
296+
297+
#endregion
253298
}
254299
}

src/OpenCvSharp.Blob/CvBlobs.cs

Lines changed: 117 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,12 @@ namespace OpenCvSharp.Blob
2525
/// <summary>
2626
/// Blob set
2727
/// </summary>
28-
public class CvBlobs : Dictionary<int, CvBlob>
28+
public class CvBlobs : Dictionary<int, CvBlob>, ICloneable
2929
{
3030
/// <summary>
3131
/// Label values
3232
/// </summary>
33-
public LabelData Labels { get; protected set; }
33+
public LabelData Labels { get; set; }
3434

3535
/// <summary>
3636
/// Constructor (init only)
@@ -39,6 +39,26 @@ public CvBlobs()
3939
{
4040
}
4141

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+
4262
/// <summary>
4363
/// Constructor (init and cvLabel)
4464
/// </summary>
@@ -373,24 +393,26 @@ public void UpdateTracks(CvTracks tracks, double thDistance, int thInactive, int
373393
i++;
374394
}
375395

376-
int maxTrackID = 0;
396+
int maxTrackId = 0;
377397
int j = 0;
378398
foreach (CvTrack track in tracks.Values)
379399
{
380400
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;
384404
j++;
385405
}
386406

387-
// Proximity matrix calculation and "used blob" list inicialization:
407+
// Proximity matrix calculation and "used blob" list initialization:
388408
for (i = 0; i < nBlobs; i++)
389409
{
390410
for (j = 0; j < nTracks; j++)
391411
{
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];
394416
close[i, j] = (DistantBlobTrack(b, t) < thDistance) ? 1 : 0;
395417
if (close[i, j] < thDistance)
396418
{
@@ -426,11 +448,12 @@ public void UpdateTracks(CvTracks tracks, double thDistance, int thInactive, int
426448
//cout << *B(i) << endl;
427449

428450
// New track.
429-
maxTrackID++;
430-
CvBlob blob = this[i+1];
451+
maxTrackId++;
452+
int ib = close.IB[i];
453+
CvBlob blob = this[ib];
431454
CvTrack track = new CvTrack
432455
{
433-
Id = maxTrackID,
456+
Id = maxTrackId,
434457
Label = blob.Label,
435458
MinX = blob.MinX,
436459
MinY = blob.MinY,
@@ -441,7 +464,8 @@ public void UpdateTracks(CvTracks tracks, double thDistance, int thInactive, int
441464
Active = 0,
442465
Inactive = 0,
443466
};
444-
tracks[maxTrackID] = track;
467+
tracks[maxTrackId] = track;
468+
//maxTrackId++;
445469
}
446470
}
447471

@@ -451,8 +475,11 @@ public void UpdateTracks(CvTracks tracks, double thDistance, int thInactive, int
451475
int c = close.AT[j];
452476
if (c != 0)
453477
{
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]);
456483

457484
GetClusterForTrack(j, close, nBlobs, nTracks, this, tracks, bb, tt);
458485

@@ -482,7 +509,8 @@ public void UpdateTracks(CvTracks tracks, double thDistance, int thInactive, int
482509
}
483510

484511
if (blob == null || track == null)
485-
throw new NotSupportedException();
512+
//throw new Exception();
513+
continue;
486514

487515
// Update track
488516
track.Label = blob.Label;
@@ -591,15 +619,57 @@ private double DistantBlobTrack(CvBlob b, CvTrack t)
591619
return Math.Min(d1, d2);
592620
}
593621

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+
594660
private static void GetClusterForTrack(int trackPos, ProximityMatrix close,
595661
int nBlobs, int nTracks, CvBlobs blobs,
596662
CvTracks tracks, List<CvBlob> bb, List<CvTrack> tt)
597663
{
664+
retry:
665+
var retryList = new List<int>();
666+
598667
for (int i = 0; i < nBlobs; i++)
599668
{
600669
if (close[i, trackPos] != 0)
601670
{
602-
bb.Add(blobs[i]);
671+
int ib = close.IB[i];
672+
bb.Add(blobs[ib]);
603673

604674
int c = close.AB[i];
605675

@@ -609,37 +679,48 @@ private static void GetClusterForTrack(int trackPos, ProximityMatrix close,
609679

610680
if (c > 1)
611681
{
612-
GetClusterForBlob(i, close, nBlobs, nTracks, blobs, tracks, bb, tt);
682+
retryList.Add(i);
683+
//GetClusterForBlob(i, close, nBlobs, nTracks, blobs, tracks, bb, tt);
613684
}
614685
}
615686
}
616-
}
617687

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)
623689
{
624-
if (close[blobPos, j] != 0)
690+
foreach (int i in retryList)
625691
{
626-
tt.Add(tracks[j]);
692+
GetClusterForBlob(i, close, nBlobs, nTracks, blobs, tracks, bb, tt);
693+
}
694+
goto retry;
695+
}
696+
}
627697

628-
int c = close.AT[j];
698+
#endregion
629699

630-
close[blobPos, j] = 0;
631-
close.AB[blobPos]--;
632-
close.AT[j]--;
700+
#endregion
633701

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);
639714
}
715+
newBlobs.Labels = Labels.Clone();
716+
717+
return newBlobs;
640718
}
641719

642-
#endregion
720+
object ICloneable.Clone()
721+
{
722+
return Clone();
723+
}
643724

644725
#endregion
645726
}

src/OpenCvSharp.Blob/CvContourChainCode.cs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ namespace OpenCvSharp.Blob
2424
/// <summary>
2525
///
2626
/// </summary>
27-
public class CvContourChainCode
27+
public class CvContourChainCode : ICloneable
2828
{
2929
/// <summary>
3030
/// Point where contour begin.
@@ -142,5 +142,23 @@ public void Render(IplImage img, CvScalar color)
142142
}
143143
}
144144
#endregion
145+
146+
/// <summary>
147+
///
148+
/// </summary>
149+
/// <returns></returns>
150+
public CvContourChainCode Clone()
151+
{
152+
return new CvContourChainCode
153+
{
154+
ChainCode = new List<CvChainCode>(ChainCode),
155+
StartingPoint = StartingPoint,
156+
};
157+
}
158+
159+
object ICloneable.Clone()
160+
{
161+
return Clone();
162+
}
145163
}
146164
}

src/OpenCvSharp.Blob/LabelData.cs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ namespace OpenCvSharp.Blob
55
/// <summary>
66
/// Label values for each pixel
77
/// </summary>
8-
public class LabelData
8+
public class LabelData : ICloneable
99
{
1010
private CvRect roi;
1111
private int[,] values;
@@ -127,5 +127,19 @@ public void DebugShow()
127127
CvWindow.ShowImages(img);
128128
}
129129
}
130+
131+
/// <summary>
132+
/// Returns deep copied instance of this
133+
/// </summary>
134+
/// <returns></returns>
135+
public LabelData Clone()
136+
{
137+
return new LabelData((int[,])Values.Clone(), roi);
138+
}
139+
140+
object ICloneable.Clone()
141+
{
142+
return Clone();
143+
}
130144
}
131145
}

0 commit comments

Comments
 (0)