如何使用EMGUCV实现简单的运动检测方法?我搜索了适用的示例,但我发现的唯一解决方案实现起来太复杂了。
有没有办法可以实现一种简单的方法来检测运动,以便在我的应用程序中触发某些东西?
答案 0 :(得分:3)
这是第三个新框架,由前两个框架之间的差异组成。使用侵蚀和阈值来获得一个框架,白色代表运动部分,黑色代表空间的其余部分。
这是一段代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Emgu.CV;
using Emgu.CV.UI;
using Emgu.CV.CvEnum;
using Emgu.CV.Structure;
using System.Diagnostics;
using System.IO;
using System.Data.SqlClient;
using System.Data.SqlServerCe;
using System.Drawing.Imaging;
namespace ptuxiakh___
{
public partial class Form1 : Form
{
Capture _capture = new Capture();
Capture capture2 = new Capture();
Image<Bgr, Byte> FirstImage = new Image<Bgr, Byte>(640, 480);
Image<Bgr, Byte> RealTimeImage = new Image<Bgr, Byte>(640, 480);
Image<Gray, Byte> des = new Image<Gray, Byte>(640, 480);
Image<Gray, Byte> thres = new Image<Gray, Byte>(640, 480);
Image<Gray, Byte> eroded = new Image<Gray, Byte>(640, 480);
bool baground = false;
private void Background()
{
try{
FirstImage = _capture.QueryFrame();
background = true;
}
catch(Exception e)
{
baground = false;
}
}
private void Tracking(object sender, EventArgs e)
{
if (baground == true)
{
RealTimeImage = capture2.QueryFrame();
CvInvoke.cvAbsDiff(FirstImage.Convert<Gray, Byte>(), RealTimeImage.Convert<Gray, Byte>(), des);
CvInvoke.cvThreshold(des, thres, 20, 255, THRESH.CV_THRESH_BINARY);
CvInvoke.cvErode(thres, eroded, IntPtr.Zero, 2);
}
else
{
Background(); // At first trying to get a static frame for
// abstraction with real time frame
}
}
private void StartButton_Click(object sender, EventArgs e)
{
Application.Idle += new EventHandler(Tracking);
}
private void Stopbutton_Click(object sender, EventArgs e)
{
Application.Idle -= new EventHandler(Tracking);
}
//侵蚀将是你的耻辱