高斯背景建模C#版本

來源:互聯網
上載者:User

using System;
using System.Collections.Generic;
using System.Text;
using Emgu.Util;
using System.Runtime.InteropServices;
using Emgu.CV.Structure;

namespace Emgu.CV
{
   /// <summary>
   /// Background statistics model
   /// </summary>
   public class BackgroundStatisticsModel : UnmanagedObject
   {
      /// <summary>
      /// Create a BGStatModel
      /// </summary>
      /// <param name="image">The image used for initiating the statistic model</param>
      /// <param name="type">The type of the statistics model</param>
      public BackgroundStatisticsModel(Image<Bgr, Byte> image, Emgu.CV.CvEnum.BG_STAT_TYPE type)
      {
         _ptr = (type == Emgu.CV.CvEnum.BG_STAT_TYPE.FGD_STAT_MODEL) ?
            CvInvoke.cvCreateFGDStatModel(image, IntPtr.Zero)
            : CvInvoke.cvCreateGaussianBGModel(image, IntPtr.Zero);
      }

      /// <summary>
      /// Create a forground statistic model using the given parameters
      /// </summary>
      /// <param name="image">The image used for initiating the statistic model</param>
      /// <param name="parameters">FGDStatModel</param>
      public BackgroundStatisticsModel(Image<Bgr, Byte> image, ref MCvFGDStatModelParams parameters)
      {
         _ptr = CvInvoke.cvCreateFGDStatModel(image, ref parameters);
      }

      private delegate int UpdateFunctionDelagate(IntPtr img, IntPtr statModel);
      private UpdateFunctionDelagate updateFunction;

      /// <summary>
      /// Update the statistic model
      /// </summary>
      /// <param name="image"></param>
      public void Update(Image<Bgr, Byte> image)
      {
         if (updateFunction == null)
         {
            updateFunction = (UpdateFunctionDelagate)Marshal.GetDelegateForFunctionPointer(MCvBGStatModel.CvUpdateBGStatModel, typeof(UpdateFunctionDelagate));
         }
         updateFunction(image.Ptr, _ptr);
      }

      /// <summary>
      /// Get the MCvBGStatModel structure
      /// </summary>
      public MCvBGStatModel MCvBGStatModel
      {
         get
         {
            return (MCvBGStatModel)Marshal.PtrToStructure(_ptr, typeof(MCvBGStatModel));
         }
      }

      /// <summary>
      /// Get a copy of the current background
      /// </summary>
      public Image<Bgr, Byte> Background
      {
         get
         {
            MCvBGStatModel statModel = MCvBGStatModel;
            System.Drawing.Size size = CvInvoke.cvGetSize(statModel.background);
            Image<Bgr, Byte> res = new Image<Bgr, byte>(size.Width, size.Height);
            CvInvoke.cvCopy(statModel.background, res.Ptr, IntPtr.Zero);
            return res;
         }
      }

      /// <summary>
      /// Get a copy of the mask for the current forground
      /// </summary>
      public Image<Gray, Byte> Foreground
      {
         get
         {
            MCvBGStatModel statModel = MCvBGStatModel;
            System.Drawing.Size size = CvInvoke.cvGetSize(statModel.background);
            Image<Gray, Byte> res = new Image<Gray, byte>(size.Width, size.Height);
            CvInvoke.cvCopy(statModel.foreground, res.Ptr, IntPtr.Zero);
            return res;
         }
      }

      private delegate void ReleaseFunction(ref IntPtr ptr);

      /// <summary>
      /// Release the BGStatModel and all the unmanaged memory associate with it
      /// </summary>
      protected override void DisposeObject()
      {
         ReleaseFunction releaseFunction = (ReleaseFunction)Marshal.GetDelegateForFunctionPointer(MCvBGStatModel.CvReleaseBGStatModel, typeof(ReleaseFunction));
         releaseFunction(ref _ptr);
      }
   }
}

namespace Emgu.CV.CvEnum
{
   /// <summary>
   /// The type of BGStatModel
   /// </summary>
   public enum BG_STAT_TYPE
   {
      /// <summary>
      ///
      /// </summary>
      FGD_STAT_MODEL,
      /// <summary>
      /// Gaussian background model
      /// </summary>
      GAUSSIAN_BG_MODEL
   }
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.