简体   繁体   English

如何将帧馈入运动检测器对象AForge.net?

[英]How to feed frames into motion detector object AForge.net?

I'm trying to get a simple motion detection program working using the AForge.net framework. 我正在尝试使用AForge.net框架来运行一个简单的运动检测程序。 On the AForge website is an example of such a program, but it's quite vague: 在AForge网站上就是这样一个程序的例子,但是它很模糊:

    // create motion detector
MotionDetector detector = new MotionDetector(
    new SimpleBackgroundModelingDetector( ),
    new MotionAreaHighlighting( ) );

// continuously feed video frames to motion detector
while ( ... )
{
    // process new video frame and check motion level
    if ( detector.ProcessFrame( videoFrame ) > 0.02 )
    {
        // ring alarm or do something else
    }
}

I need some help with the condition for the while loop, as I can't find a solution for how to feed video frames into the MotionDetector object. 我需要一些有关while循环条件的帮助,因为找不到如何将视频帧馈入MotionDetector对象的解决方案。

Thanks. 谢谢。

You'll want to take advantage of AForge's DirectShow VideoInputDevice. 您将要利用AForge的DirectShow VideoInputDevice。 And instead of a while loop you will have a NewFrame event to control the motion detector. 而不是while循环,您将拥有一个NewFrame事件来控制运动检测器。

First you'll need the references: 首先,您需要参考:

using AForge.Video.DirectShow;
using AForge.Video;
using AForge.Vision.Motion;
using System.Drawing;

Next you will need to get your capture device eg your webcam and add a new frame event handler to the NewFrame event for the device: 接下来,您将需要获取捕获设备(例如,网络摄像头),并将新的框架事件处理程序添加到该设备的NewFrame事件:

Cameras = new FilterInfoCollection(FilterCategory.VideoInputDevice);
VideoCaptureDevice Camera = new VideoCaptureDevice(Cameras[0].MonikerString);
Camera.NewFrame += new NewFrameEventHandler(ProcessNewFrame);

Now you can implement the NewFrameEventHandler however you choose: 现在,您可以选择实现NewFrameEventHandler:

private void ProcessNewFrame(object sender, NewFrameEventArgs eventArgs)
{
    Bitmap frame = (Bitmap) eventArgs.Frame.Clone();
    if (detector.ProcessFrame(frame) > 0.02)
    {
        // ring alarm or do somethng else
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM