You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

114 lines
3.5 KiB

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using Emgu.Util;
  5. using System.Runtime.InteropServices;
  6. namespace Emgu.CV
  7. {
  8. /// <summary>
  9. /// Create a background statistics model
  10. /// </summary>
  11. public class BackgroundStatisticsModel : UnmanagedObject
  12. {
  13. /// <summary>
  14. /// Create a BGStatModel
  15. /// </summary>
  16. /// <param name="img">The image used for initiating the statistic model</param>
  17. /// <param name="type">The type of the statistics model</param>
  18. public BackgroundStatisticsModel(Image<Bgr, Byte> img, Emgu.CV.CvEnum.BG_STAT_TYPE type)
  19. {
  20. if (type == Emgu.CV.CvEnum.BG_STAT_TYPE.FGD_STAT_MODEL)
  21. {
  22. _ptr = CvInvoke.cvCreateFGDStatModel(img, IntPtr.Zero);
  23. }
  24. else
  25. {
  26. _ptr = CvInvoke.cvCreateGaussianBGModel(img, IntPtr.Zero);
  27. }
  28. }
  29. private delegate int UpdateFunction(IntPtr img, IntPtr statModel);
  30. /// <summary>
  31. /// Update the statistic model
  32. /// </summary>
  33. /// <param name="img"></param>
  34. public void Update(Image<Bgr, Byte> img)
  35. {
  36. MCvBGStatModel model = MCvBGStatModel;
  37. UpdateFunction updateFunction = (UpdateFunction)Marshal.GetDelegateForFunctionPointer(model.CvUpdateBGStatModel, typeof(UpdateFunction));
  38. updateFunction(img.Ptr, _ptr);
  39. }
  40. /// <summary>
  41. /// Get the CvBGStatModel structure
  42. /// </summary>
  43. public MCvBGStatModel MCvBGStatModel
  44. {
  45. get
  46. {
  47. return (MCvBGStatModel)Marshal.PtrToStructure(_ptr, typeof(MCvBGStatModel));
  48. }
  49. }
  50. /// <summary>
  51. /// Get the current background
  52. /// </summary>
  53. public Image<Bgr, Byte> Background
  54. {
  55. get
  56. {
  57. MCvBGStatModel statModel = MCvBGStatModel;
  58. MIplImage iplImg = (MIplImage)Marshal.PtrToStructure(statModel.background, typeof(MIplImage));
  59. Image<Bgr, Byte> res = new Image<Bgr, byte>(iplImg.width, iplImg.height);
  60. CvInvoke.cvCopy(statModel.background, res.Ptr, IntPtr.Zero);
  61. return res;
  62. }
  63. }
  64. /// <summary>
  65. /// Get a mask of the current forground
  66. /// </summary>
  67. public Image<Gray, Byte> Foreground
  68. {
  69. get
  70. {
  71. MCvBGStatModel statModel = MCvBGStatModel;
  72. MIplImage iplImg = (MIplImage)Marshal.PtrToStructure(statModel.foreground, typeof(MIplImage));
  73. Image<Gray, Byte> res = new Image<Gray, byte>(iplImg.width, iplImg.height);
  74. CvInvoke.cvCopy(statModel.foreground, res.Ptr, IntPtr.Zero);
  75. return res;
  76. }
  77. }
  78. private delegate void ReleaseFunction(ref IntPtr ptr);
  79. /// <summary>
  80. /// Release the BGStatModel and all the unmanaged memory associate with it
  81. /// </summary>
  82. protected override void DisposeObject()
  83. {
  84. ReleaseFunction releaseFunction = (ReleaseFunction)Marshal.GetDelegateForFunctionPointer(MCvBGStatModel.CvReleaseBGStatModel, typeof(ReleaseFunction));
  85. releaseFunction(ref _ptr);
  86. }
  87. }
  88. }
  89. namespace Emgu.CV.CvEnum
  90. {
  91. /// <summary>
  92. /// The type of BGStatModel
  93. /// </summary>
  94. public enum BG_STAT_TYPE
  95. {
  96. /// <summary>
  97. ///
  98. /// </summary>
  99. FGD_STAT_MODEL,
  100. /// <summary>
  101. /// Gaussian background model
  102. /// </summary>
  103. GAUSSIAN_BG_MODEL
  104. }
  105. }