Skip to content

Commit

Permalink
Merge branch 'BlobImprovement'
Browse files Browse the repository at this point in the history
  • Loading branch information
shimat committed Jun 9, 2014
2 parents 328636c + 9d7c138 commit 45d36c1
Show file tree
Hide file tree
Showing 5 changed files with 200 additions and 42 deletions.
2 changes: 1 addition & 1 deletion nuget/OpenCvSharp-AnyCPU.nuspec
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<package xmlns="http://schemas.microsoft.com/packaging/2011/08/nuspec.xsd">
<metadata>
<id>OpenCvSharp-AnyCPU</id>
<version>2.4.8.20140607</version>
<version>2.4.8.20140609</version>
<title>OpenCvSharp</title>
<authors>shimat</authors>
<licenseUrl>http://opensource.org/licenses/BSD-3-Clause</licenseUrl>
Expand Down
51 changes: 48 additions & 3 deletions src/OpenCvSharp.Blob/CvBlob.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ namespace OpenCvSharp.Blob
/// <summary>
/// Struct that contain information about one blob.
/// </summary>
public class CvBlob
[Serializable]
public class CvBlob : ICloneable
{
#region Init
/// <summary>
Expand Down Expand Up @@ -154,11 +155,11 @@ public CvRect Rect
/// <summary>
/// Hu moment 1.
/// </summary>
public double P1;
public double P1 { get; set; }
/// <summary>
/// Hu moment 2.
/// </summary>
public double P2;
public double P2 { get; set; }

/// <summary>
/// Contour
Expand Down Expand Up @@ -250,5 +251,49 @@ public void SetMoments()
}
#endregion
#endregion

#region ICloneable

/// <summary>
///
/// </summary>
/// <returns></returns>
public CvBlob Clone()
{
return new CvBlob
{
Area = Area,
CentralMoments = CentralMoments,
Centroid = Centroid,
Contour = Contour.Clone(),
InternalContours = new List<CvContourChainCode>(InternalContours),
Label = Label,
M00 = M00,
M01 = M01,
M02 = M02,
M10 = M10,
M11 = M11,
M20 = M20,
MaxX = MaxX,
MaxY = MaxY,
MinX = MinX,
MinY = MinY,
N02 = N02,
N11 = N11,
N20 = N20,
P1 = P1,
P2 = P2,
U02 = U02,
U11 = U11,
U20 = U20,
};
}

object ICloneable.Clone()
{
return Clone();
}

#endregion
}
}
153 changes: 117 additions & 36 deletions src/OpenCvSharp.Blob/CvBlobs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@ namespace OpenCvSharp.Blob
/// <summary>
/// Blob set
/// </summary>
public class CvBlobs : Dictionary<int, CvBlob>
public class CvBlobs : Dictionary<int, CvBlob>, ICloneable
{
/// <summary>
/// Label values
/// </summary>
public LabelData Labels { get; protected set; }
public LabelData Labels { get; set; }

/// <summary>
/// Constructor (init only)
Expand All @@ -39,6 +39,26 @@ public CvBlobs()
{
}

/// <summary>
/// Constructor (copy)
/// </summary>
public CvBlobs(IEnumerable<KeyValuePair<int, CvBlob>> blobData, int[,] labelData)
{
foreach (KeyValuePair<int, CvBlob> pair in blobData)
{
Add(pair.Key, pair.Value);
}
Labels = new LabelData(labelData);
}

/// <summary>
/// Constructor (copy)
/// </summary>
public CvBlobs(IEnumerable<KeyValuePair<int, CvBlob>> blobData, LabelData labelData)
: this(blobData, labelData.Values)
{
}

/// <summary>
/// Constructor (init and cvLabel)
/// </summary>
Expand Down Expand Up @@ -373,24 +393,26 @@ public void UpdateTracks(CvTracks tracks, double thDistance, int thInactive, int
i++;
}

int maxTrackID = 0;
int maxTrackId = 0;
int j = 0;
foreach (CvTrack track in tracks.Values)
{
close.AT[j] = 0;
close.AT[j] = track.Id;
if (track.Id > maxTrackID)
maxTrackID = track.Id;
close.IT[j] = track.Id;
if (track.Id > maxTrackId)
maxTrackId = track.Id;
j++;
}

// Proximity matrix calculation and "used blob" list inicialization:
// Proximity matrix calculation and "used blob" list initialization:
for (i = 0; i < nBlobs; i++)
{
for (j = 0; j < nTracks; j++)
{
CvBlob b = this[close.IB[i]];
CvTrack t = tracks[close.IT[j]];
int ib = close.IB[i];
int it = close.IT[j];
CvBlob b = this[ib];
CvTrack t = tracks[it];
close[i, j] = (DistantBlobTrack(b, t) < thDistance) ? 1 : 0;
if (close[i, j] < thDistance)
{
Expand Down Expand Up @@ -426,11 +448,12 @@ public void UpdateTracks(CvTracks tracks, double thDistance, int thInactive, int
//cout << *B(i) << endl;

// New track.
maxTrackID++;
CvBlob blob = this[i+1];
maxTrackId++;
int ib = close.IB[i];
CvBlob blob = this[ib];
CvTrack track = new CvTrack
{
Id = maxTrackID,
Id = maxTrackId,
Label = blob.Label,
MinX = blob.MinX,
MinY = blob.MinY,
Expand All @@ -441,7 +464,8 @@ public void UpdateTracks(CvTracks tracks, double thDistance, int thInactive, int
Active = 0,
Inactive = 0,
};
tracks[maxTrackID] = track;
tracks[maxTrackId] = track;
//maxTrackId++;
}
}

Expand All @@ -451,8 +475,11 @@ public void UpdateTracks(CvTracks tracks, double thDistance, int thInactive, int
int c = close.AT[j];
if (c != 0)
{
List<CvTrack> tt = new List<CvTrack> {tracks[j]};
List<CvBlob> bb = new List<CvBlob>();
var tt = new List<CvTrack>();
var bb = new List<CvBlob>();

int it = close.IT[j];
tt.Add(tracks[it]);

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

Expand Down Expand Up @@ -482,7 +509,8 @@ public void UpdateTracks(CvTracks tracks, double thDistance, int thInactive, int
}

if (blob == null || track == null)
throw new NotSupportedException();
//throw new Exception();
continue;

// Update track
track.Label = blob.Label;
Expand Down Expand Up @@ -591,15 +619,57 @@ private double DistantBlobTrack(CvBlob b, CvTrack t)
return Math.Min(d1, d2);
}

private static void GetClusterForBlob(int blobPos, ProximityMatrix close,
int nBlobs, int nTracks, CvBlobs blobs, CvTracks tracks,
List<CvBlob> bb, List<CvTrack> tt)
{
retry:
var retryList = new List<int>();

for (int j = 0; j < nTracks; j++)
{
if (close[blobPos, j] != 0)
{
int it = close.IT[j];
tt.Add(tracks[it]);

int c = close.AT[j];

close[blobPos, j] = 0;
close.AB[blobPos]--;
close.AT[j]--;

if (c > 1)
{
retryList.Add(j);
//GetClusterForTrack(j, close, nBlobs, nTracks, blobs, tracks, bb, tt);
}
}
}

if (retryList.Count > 0)
{
foreach (int j in retryList)
{
GetClusterForTrack(j, close, nBlobs, nTracks, blobs, tracks, bb, tt);
}
goto retry;
}
}

private static void GetClusterForTrack(int trackPos, ProximityMatrix close,
int nBlobs, int nTracks, CvBlobs blobs,
CvTracks tracks, List<CvBlob> bb, List<CvTrack> tt)
{
retry:
var retryList = new List<int>();

for (int i = 0; i < nBlobs; i++)
{
if (close[i, trackPos] != 0)
{
bb.Add(blobs[i]);
int ib = close.IB[i];
bb.Add(blobs[ib]);

int c = close.AB[i];

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

if (c > 1)
{
GetClusterForBlob(i, close, nBlobs, nTracks, blobs, tracks, bb, tt);
retryList.Add(i);
//GetClusterForBlob(i, close, nBlobs, nTracks, blobs, tracks, bb, tt);
}
}
}
}

private static void GetClusterForBlob(int blobPos, ProximityMatrix close,
int nBlobs, int nTracks, CvBlobs blobs, CvTracks tracks,
List<CvBlob> bb, List<CvTrack> tt)
{
for (int j = 0; j < nTracks; j++)
if (retryList.Count > 0)
{
if (close[blobPos, j] != 0)
foreach (int i in retryList)
{
tt.Add(tracks[j]);
GetClusterForBlob(i, close, nBlobs, nTracks, blobs, tracks, bb, tt);
}
goto retry;
}
}

int c = close.AT[j];
#endregion

close[blobPos, j] = 0;
close.AB[blobPos]--;
close.AT[j]--;
#endregion

if (c > 1)
{
GetClusterForTrack(j, close, nBlobs, nTracks, blobs, tracks, bb, tt);
}
}
#region ICloneable

/// <summary>
///
/// </summary>
/// <returns></returns>
public CvBlobs Clone()
{
var newBlobs = new CvBlobs();
foreach (KeyValuePair<int, CvBlob> pair in this)
{
newBlobs.Add(pair.Key, pair.Value);
}
newBlobs.Labels = Labels.Clone();

return newBlobs;
}

#endregion
object ICloneable.Clone()
{
return Clone();
}

#endregion
}
Expand Down
20 changes: 19 additions & 1 deletion src/OpenCvSharp.Blob/CvContourChainCode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ namespace OpenCvSharp.Blob
/// <summary>
///
/// </summary>
public class CvContourChainCode
public class CvContourChainCode : ICloneable
{
/// <summary>
/// Point where contour begin.
Expand Down Expand Up @@ -142,5 +142,23 @@ public void Render(IplImage img, CvScalar color)
}
}
#endregion

/// <summary>
///
/// </summary>
/// <returns></returns>
public CvContourChainCode Clone()
{
return new CvContourChainCode
{
ChainCode = new List<CvChainCode>(ChainCode),
StartingPoint = StartingPoint,
};
}

object ICloneable.Clone()
{
return Clone();
}
}
}
16 changes: 15 additions & 1 deletion src/OpenCvSharp.Blob/LabelData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ namespace OpenCvSharp.Blob
/// <summary>
/// Label values for each pixel
/// </summary>
public class LabelData
public class LabelData : ICloneable
{
private CvRect roi;
private int[,] values;
Expand Down Expand Up @@ -127,5 +127,19 @@ public void DebugShow()
CvWindow.ShowImages(img);
}
}

/// <summary>
/// Returns deep copied instance of this
/// </summary>
/// <returns></returns>
public LabelData Clone()
{
return new LabelData((int[,])Values.Clone(), roi);
}

object ICloneable.Clone()
{
return Clone();
}
}
}

0 comments on commit 45d36c1

Please sign in to comment.