legacy_Tracker.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. using OpenCVForUnity.CoreModule;
  2. using OpenCVForUnity.UtilsModule;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Runtime.InteropServices;
  6. namespace OpenCVForUnity.TrackingModule
  7. {
  8. // C++: class Tracker
  9. /**
  10. * Base abstract class for the long-term tracker:
  11. */
  12. public class legacy_Tracker : Algorithm
  13. {
  14. protected override void Dispose(bool disposing)
  15. {
  16. try
  17. {
  18. if (disposing)
  19. {
  20. }
  21. if (IsEnabledDispose)
  22. {
  23. if (nativeObj != IntPtr.Zero)
  24. tracking_legacy_1Tracker_delete(nativeObj);
  25. nativeObj = IntPtr.Zero;
  26. }
  27. }
  28. finally
  29. {
  30. base.Dispose(disposing);
  31. }
  32. }
  33. protected internal legacy_Tracker(IntPtr addr) : base(addr) { }
  34. // internal usage only
  35. public static new legacy_Tracker __fromPtr__(IntPtr addr) { return new legacy_Tracker(addr); }
  36. //
  37. // C++: bool cv::legacy::Tracker::init(Mat image, Rect2d boundingBox)
  38. //
  39. /**
  40. * Initialize the tracker with a known bounding box that surrounded the target
  41. * param image The initial frame
  42. * param boundingBox The initial bounding box
  43. *
  44. * return True if initialization went succesfully, false otherwise
  45. */
  46. public bool init(Mat image, Rect2d boundingBox)
  47. {
  48. ThrowIfDisposed();
  49. if (image != null) image.ThrowIfDisposed();
  50. return tracking_legacy_1Tracker_init_10(nativeObj, image.nativeObj, boundingBox.x, boundingBox.y, boundingBox.width, boundingBox.height);
  51. }
  52. //
  53. // C++: bool cv::legacy::Tracker::update(Mat image, Rect2d& boundingBox)
  54. //
  55. /**
  56. * Update the tracker, find the new most likely bounding box for the target
  57. * param image The current frame
  58. * param boundingBox The bounding box that represent the new target location, if true was returned, not
  59. * modified otherwise
  60. *
  61. * return True means that target was located and false means that tracker cannot locate target in
  62. * current frame. Note, that latter *does not* imply that tracker has failed, maybe target is indeed
  63. * missing from the frame (say, out of sight)
  64. */
  65. public bool update(Mat image, Rect2d boundingBox)
  66. {
  67. ThrowIfDisposed();
  68. if (image != null) image.ThrowIfDisposed();
  69. double[] boundingBox_out = new double[4];
  70. bool retVal = tracking_legacy_1Tracker_update_10(nativeObj, image.nativeObj, boundingBox_out);
  71. if (boundingBox != null) { boundingBox.x = boundingBox_out[0]; boundingBox.y = boundingBox_out[1]; boundingBox.width = boundingBox_out[2]; boundingBox.height = boundingBox_out[3]; }
  72. return retVal;
  73. }
  74. #if (UNITY_IOS || UNITY_WEBGL) && !UNITY_EDITOR
  75. const string LIBNAME = "__Internal";
  76. #else
  77. const string LIBNAME = "opencvforunity";
  78. #endif
  79. // C++: bool cv::legacy::Tracker::init(Mat image, Rect2d boundingBox)
  80. [DllImport(LIBNAME)]
  81. [return: MarshalAs(UnmanagedType.U1)]
  82. private static extern bool tracking_legacy_1Tracker_init_10(IntPtr nativeObj, IntPtr image_nativeObj, double boundingBox_x, double boundingBox_y, double boundingBox_width, double boundingBox_height);
  83. // C++: bool cv::legacy::Tracker::update(Mat image, Rect2d& boundingBox)
  84. [DllImport(LIBNAME)]
  85. [return: MarshalAs(UnmanagedType.U1)]
  86. private static extern bool tracking_legacy_1Tracker_update_10(IntPtr nativeObj, IntPtr image_nativeObj, double[] boundingBox_out);
  87. // native support for java finalize()
  88. [DllImport(LIBNAME)]
  89. private static extern void tracking_legacy_1Tracker_delete(IntPtr nativeObj);
  90. }
  91. }