高斯背景建模C#版本
2009-02-04 16:39
351 查看
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
}
}
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
}
}
相关文章推荐
- 混合高斯背景建模原理及实现(C# )
- OpenCV2.2 和 2.4.4 的 cvSetCaptureProperty 和 CvGaussBGModel (高斯背景建模)版本间差异
- opencv高斯背景建模
- 混合高斯背景建模
- 单高斯背景建模opencv仿真
- 高斯混合背景建模
- 高斯混合背景建模的改进方案
- 高斯背景建模学习 icvUpdateGaussianBGModel()
- 运动目标的背景建模-混合高斯背景建模和KNN模型建模的OpenCV代码实现
- 运动检测与跟踪_混合高斯背景建模
- 单高斯背景建模原理及代码实现
- 混合高斯背景建模
- 背景建模--高斯混合模型
- 混合高斯背景建模
- 单高斯背景建模MATLAB仿真
- 高斯背景建模
- 高斯背景建模学习 icvUpdateFullWindow()
- 混合高斯背景建模——opencv
- 将用于视频运动检测的单高斯背景建模算法移植于YUV颜色空间
- 【OpenCV】高斯混合背景建模