Browse Source

Added TrackerMedianFlow and TrackerBoosting.

pull/42/head
Canming Huang 8 years ago
parent
commit
fbe7337825
  1. 1
      Emgu.CV.Contrib/Emgu.CV.Contrib.projitems
  2. 176
      Emgu.CV.Contrib/Tracking/Tracker.cs
  3. 42
      Emgu.CV.Extern/tracking/tracking_c.cpp
  4. 8
      Emgu.CV.Extern/tracking/tracking_c.h
  5. 1
      Emgu.CV/Emgu.CV.projitems

1
Emgu.CV.Contrib/Emgu.CV.Contrib.projitems

@ -17,7 +17,6 @@
<Compile Include="$(MSBuildThisFileDirectory)BgSegm\*.cs" />
<Compile Include="$(MSBuildThisFileDirectory)XImgproc\*.cs" />
<Compile Include="$(MSBuildThisFileDirectory)XPhoto\*.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Dnn\*.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Tracking\*.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Plot\*.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Aruco\*.cs" />

176
Emgu.CV.Contrib/Tracking/Tracker.cs

@ -17,81 +17,117 @@ using System.Drawing;
namespace Emgu.CV.Tracking
{
/// <summary>
/// Long-term tracker
/// </summary>
public abstract class Tracker : UnmanagedObject
{
private IntPtr _trackerPtr;
/*
/// <summary>
/// Creates a tracker by its name.
/// </summary>
/// <param name="trackerType">Tracker type, The following detector types are supported: "MIL" – TrackerMIL; "BOOSTING" – TrackerBoosting</param>
public Tracker(String trackerType)
{
using (CvString trackerTypeStr = new CvString(trackerType))
_ptr = ContribInvoke.cveTrackerCreate(trackerTypeStr);
}*/
/// <summary>
/// Initialize the tracker with a know bounding box that surrounding the target.
/// </summary>
/// <param name="image">The initial frame</param>
/// <param name="boundingBox">The initial bounding box</param>
/// <returns></returns>
public bool Init(Mat image, Rectangle boundingBox)
{
return ContribInvoke.cveTrackerInit(_trackerPtr, image, ref boundingBox);
}
/// <summary>
/// Update the tracker, find the new most likely bounding box for the target.
/// </summary>
/// <param name="image">The current frame</param>
/// <param name="boundingBox">The bounding box that represent the new target location, if true was returned, not modified otherwise</param>
/// <returns>True means that target was located and false means that tracker cannot locate target in current frame. Note, that latter does not imply that tracker has failed, maybe target is indeed missing from the frame (say, out of sight)</returns>
public bool Update(Mat image, out Rectangle boundingBox)
{
boundingBox = new Rectangle();
return ContribInvoke.cveTrackerUpdate(_trackerPtr, image, ref boundingBox);
}
/// <summary>
/// Release the unmanaged memory associated with this tracker
/// </summary>
protected override void DisposeObject()
{
public class TrackerBoosting : Tracker
{
/// <summary>
///
/// </summary>
/// <param name="numClassifiers">The number of classifiers to use in a OnlineBoosting algorithm</param>
/// <param name="samplerOverlap">Search region parameters to use in a OnlineBoosting algorithm</param>
/// <param name="samplerSearchFactor">search region parameters to use in a OnlineBoosting algorithm</param>
/// <param name="iterationInit">The initial iterations</param>
/// <param name="featureSetNumFeatures">Number of features, a good value would be 10*numClassifiers + iterationInit</param>
public TrackerBoosting(int numClassifiers = 100, float samplerOverlap = 0.99f, float samplerSearchFactor = 1.8f, int iterationInit = 50, int featureSetNumFeatures = 100*10+50)
{
ContribInvoke.cveTrackerBoostingCreate(numClassifiers, samplerOverlap, samplerSearchFactor, iterationInit, featureSetNumFeatures, ref _trackerPtr);
}
protected override void DisposeObject()
{
base.DisposeObject();
ContribInvoke.cveTrackerBoostingRelease(ref _ptr);
}
}
public class TrackerMedianFlow : Tracker
{
public TrackerMedianFlow(Params parameters)
{
ContribInvoke.cveTrackerMedianFlowCreate(ref parameters, ref _trackerPtr);
}
protected override void DisposeObject()
{
base.DisposeObject();
ContribInvoke.cveTrackerMedianFlowRelease(ref _ptr);
}
}
/// <summary>
/// Long-term tracker
/// </summary>
public abstract class Tracker : UnmanagedObject
{
protected IntPtr _trackerPtr;
/// <summary>
/// Initialize the tracker with a know bounding box that surrounding the target.
/// </summary>
/// <param name="image">The initial frame</param>
/// <param name="boundingBox">The initial bounding box</param>
/// <returns></returns>
public bool Init(Mat image, Rectangle boundingBox)
{
return ContribInvoke.cveTrackerInit(_trackerPtr, image, ref boundingBox);
}
/// <summary>
/// Update the tracker, find the new most likely bounding box for the target.
/// </summary>
/// <param name="image">The current frame</param>
/// <param name="boundingBox">The bounding box that represent the new target location, if true was returned, not modified otherwise</param>
/// <returns>True means that target was located and false means that tracker cannot locate target in current frame. Note, that latter does not imply that tracker has failed, maybe target is indeed missing from the frame (say, out of sight)</returns>
public bool Update(Mat image, out Rectangle boundingBox)
{
boundingBox = new Rectangle();
return ContribInvoke.cveTrackerUpdate(_trackerPtr, image, ref boundingBox);
}
/// <summary>
/// Release the unmanaged memory associated with this tracker
/// </summary>
protected override void DisposeObject()
{
_trackerPtr = IntPtr.Zero;
/*
if (_ptr != IntPtr.Zero)
ContribInvoke.cveTrackerRelease(ref _ptr);*/
}
}
}
}
}
namespace Emgu.CV
{
public static partial class ContribInvoke
{
[DllImport(CvInvoke.ExternLibrary, CallingConvention = CvInvoke.CvCallingConvention)]
internal static extern IntPtr cveTrackerCreate(IntPtr trackerType);
[DllImport(CvInvoke.ExternLibrary, CallingConvention = CvInvoke.CvCallingConvention)]
[return:MarshalAs(CvInvoke.BoolMarshalType)]
internal static extern bool cveTrackerInit(IntPtr tracker, IntPtr image, ref Rectangle boundingBox);
[DllImport(CvInvoke.ExternLibrary, CallingConvention = CvInvoke.CvCallingConvention)]
[return: MarshalAs(CvInvoke.BoolMarshalType)]
internal static extern bool cveTrackerUpdate(IntPtr tracker, IntPtr image, ref Rectangle boundingBox);
[DllImport(CvInvoke.ExternLibrary, CallingConvention = CvInvoke.CvCallingConvention)]
internal static extern void cveTrackerRelease(ref IntPtr tracker);
}
public static partial class ContribInvoke
{
//[DllImport(CvInvoke.ExternLibrary, CallingConvention = CvInvoke.CvCallingConvention)]
//internal static extern IntPtr cveTrackerCreate(IntPtr trackerType);
[DllImport(CvInvoke.ExternLibrary, CallingConvention = CvInvoke.CvCallingConvention)]
[return: MarshalAs(CvInvoke.BoolMarshalType)]
internal static extern bool cveTrackerInit(IntPtr tracker, IntPtr image, ref Rectangle boundingBox);
[DllImport(CvInvoke.ExternLibrary, CallingConvention = CvInvoke.CvCallingConvention)]
[return: MarshalAs(CvInvoke.BoolMarshalType)]
internal static extern bool cveTrackerUpdate(IntPtr tracker, IntPtr image, ref Rectangle boundingBox);
//[DllImport(CvInvoke.ExternLibrary, CallingConvention = CvInvoke.CvCallingConvention)]
//internal static extern void cveTrackerRelease(ref IntPtr tracker);
[DllImport(CvInvoke.ExternLibrary, CallingConvention = CvInvoke.CvCallingConvention)]
internal static extern IntPtr cveTrackerMedianFlowCreate(int pointsInGrid, ref Size winSize, int maxLevel, ref CvTermCriteria termCriteria, ref Size winSizeNCC, double maxMedianLengthOfDisplacementDifference, ref IntPtr tracker);
[DllImport(CvInvoke.ExternLibrary, CallingConvention = CvInvoke.CvCallingConvention)]
internal static extern void cveTrackerMedianFlowRelease(ref IntPtr tracker);
[DllImport(CvInvoke.ExternLibrary, CallingConvention = CvInvoke.CvCallingConvention)]
internal static extern IntPtr cveTrackerBoostingCreate(int numClassifiers, float samplerOverlap, float samplerSearchFactor, int iterationInit, int featureSetNumFeatures, ref IntPtr tracker);
[DllImport(CvInvoke.ExternLibrary, CallingConvention = CvInvoke.CvCallingConvention)]
internal static extern void cveTrackerBoostingRelease(ref IntPtr tracker);
}
}
#endif

42
Emgu.CV.Extern/tracking/tracking_c.cpp

@ -24,11 +24,53 @@ bool cveTrackerUpdate(cv::Tracker* tracker, cv::Mat* image, CvRect* boundingBox)
*boundingBox = box;
return result;
}
/*
void cveTrackerRelease(cv::Tracker** tracker)
{
delete *tracker;
*tracker = 0;
}
*/
cv::TrackerBoosting* cveTrackerBoostingCreate(int numClassifiers, float samplerOverlap, float samplerSearchFactor, int iterationInit, int featureSetNumFeatures, cv::Tracker** tracker)
{
cv::TrackerBoosting::Params p;
p.numClassifiers = numClassifiers;
p.samplerOverlap = samplerOverlap;
p.samplerSearchFactor = samplerSearchFactor;
p.iterationInit = iterationInit;
p.featureSetNumFeatures = featureSetNumFeatures;
cv::Ptr<cv::TrackerBoosting> ptr = cv::TrackerBoosting::create(p);
ptr.addref();
*tracker = static_cast<cv::Tracker*>(ptr.get());
return ptr.get();
}
void cveTrackerBoostingRelease(cv::TrackerBoosting** tracker)
{
delete *tracker;
*tracker = 0;
}
cv::TrackerMedianFlow* cveTrackerMedianFlowCreate(int pointsInGrid, CvSize* winSize, int maxLevel, CvTermCriteria* termCriteria, CvSize* winSizeNCC, double maxMedianLengthOfDisplacementDifference, cv::Tracker** tracker)
{
cv::TrackerMedianFlow::Params p;
p.pointsInGrid = pointsInGrid;
p.winSize = *winSize;
p.maxLevel = maxLevel;
p.termCriteria = *termCriteria;
p.winSizeNCC = *winSizeNCC;
p.maxMedianLengthOfDisplacementDifference = maxMedianLengthOfDisplacementDifference;
cv::Ptr<cv::TrackerMedianFlow> ptr = cv::TrackerMedianFlow::create(p);
ptr.addref();
*tracker = static_cast<cv::Tracker*>(ptr.get());
return ptr.get();
}
void cveTrackerMedianFlowRelease(cv::TrackerMedianFlow** tracker)
{
delete* tracker;
*tracker = 0;
}
cv::MultiTracker* cveMultiTrackerCreate()
{

8
Emgu.CV.Extern/tracking/tracking_c.h

@ -14,7 +14,13 @@
//CVAPI(cv::Tracker*) cveTrackerCreate(cv::String* trackerType);
CVAPI(bool) cveTrackerInit(cv::Tracker* tracker, cv::Mat* image, CvRect* boundingBox);
CVAPI(bool) cveTrackerUpdate(cv::Tracker* tracker, cv::Mat* image, CvRect* boundingBox);
CVAPI(void) cveTrackerRelease(cv::Tracker** tracker);
//CVAPI(void) cveTrackerRelease(cv::Tracker** tracker);
CVAPI(cv::TrackerBoosting*) cveTrackerBoostingCreate(int numClassifiers, float samplerOverlap, float samplerSearchFactor, int iterationInit, int featureSetNumFeatures, cv::Tracker** tracker);
CVAPI(void) cveTrackerBoostingRelease(cv::TrackerBoosting** tracker);
CVAPI(cv::TrackerMedianFlow*) cveTrackerMedianFlowCreate(int pointsInGrid, CvSize* winSize, int maxLevel, CvTermCriteria* termCriteria, CvSize* winSizeNCC, double maxMedianLengthOfDisplacementDifference, cv::Tracker** tracker);
CVAPI(void) cveTrackerMedianFlowRelease(cv::TrackerMedianFlow** tracker);
CVAPI(cv::MultiTracker*) cveMultiTrackerCreate();
CVAPI(bool) cveMultiTrackerAdd(cv::MultiTracker* multiTracker, cv::Tracker* tracker, cv::Mat* image, CvRect* boundingBox);

1
Emgu.CV/Emgu.CV.projitems

@ -33,6 +33,7 @@
<Compile Include="$(MSBuildThisFileDirectory)Superres\*.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Videostab\*.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Ml\*.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Dnn\*.cs" />
<Compile Include="$(MSBuildThisFileDirectory)PInvoke\CvType\*.cs" />
<Compile Include="$(MSBuildThisFileDirectory)PInvoke\Windows.Store\*.cs" />
<Compile Include="$(MSBuildThisFileDirectory)PInvoke\Android\*.cs" />

Loading…
Cancel
Save