惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

N
Netflix TechBlog - Medium
J
Java Code Geeks
爱范儿
爱范儿
雷峰网
雷峰网
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
博客园 - 三生石上(FineUI控件)
H
Hackread – Cybersecurity News, Data Breaches, AI and More
B
Blog RSS Feed
Google DeepMind News
Google DeepMind News
Jina AI
Jina AI
The GitHub Blog
The GitHub Blog
I
InfoQ
月光博客
月光博客
博客园 - 聂微东
博客园 - Franky
The Cloudflare Blog
阮一峰的网络日志
阮一峰的网络日志
博客园_首页
G
Google Developers Blog
Blog — PlanetScale
Blog — PlanetScale
L
LangChain Blog
罗磊的独立博客
Apple Machine Learning Research
Apple Machine Learning Research

博客园 - zengqs

很久没有登录了 交警手势信号图解(新)----07年10月1日全国施行 在线幻灯片Slide Show System汇总 - PPT的替代品 转载:高速交警的忠告(为了安全,走高速必看) 转帖:如何替换系统自带的记事本(notepad.exe) 转摘:PCA算法 Haar分类器,ObjectMarker程序源代码 OpenCV训练分类器 - zengqs - 博客园 摘自:Emgu.CV项目,An object recognizer using PCA (Principle Components Analysis) VisualSVN1.6.1破解 视频监控和行为理解的网络资源 高斯背景建模 C#定义一个模板类 转帖:人脸检测概述 人脸检测的算法 - zengqs - 博客园 C#转换为灰度图的算法 一个人脸检测器 测试用是Windows Live Writer写日志 转帖:关于computer vision的会议及vision guys (zz)
高斯背景建模C#版本
zengqs · 2009-02-04 · via 博客园 - zengqs

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
   }
}