运动检测

Jaz*_*rix 11 c# motion-detection

我真的无法理解这一点,所以我希望有人可以给我一点手^^

我正试图通过我的网络摄像头检测C#中的运动.

到目前为止,我已经尝试了多个库(AForge Lib),但失败了,因为我不明白如何使用它.

起初我只是想比较当前帧与最后一帧的像素,但结果却像是完全一样:I

现在,我的网络摄像头每次从网络摄像头运行一个事件"webcam_ImageCaptured",就像5-10 fps.

但我找不到一种简单的方法来区分这两个图像,或者至少是一些有效的东西.

有没有人知道如何做到这一点相当简单(尽可能这样)?

Rah*_*han 11

使用您提到的库来使运动检测工作是微不足道的.以下是AForge(2.2.4版)示例.它适用于视频文件,但您可以轻松地将其适应网络摄像头事件.

约翰内斯是对的,但我认为玩这些库可以简化理解基本图像处理的方法.

我的应用程序在一台非常快的SSD机器上以120FPS处理720p视频,在我的开发笔记本电脑上处理大约50FPS.

public static void Main()
{    
    float motionLevel = 0F;
    System.Drawing.Bitmap bitmap = null;
    AForge.Video.FFMPEG.VideoFileReader reader = null;
    AForge.Vision.Motion.MotionDetector motionDetector = null;    

    motionDetector = GetDefaultMotionDetector();

    reader.Open(@"C:\Temp.wmv");

    while (true)
    {
        bitmap = reader.ReadVideoFrame();
        if (bitmap == null) break;

        // motionLevel will indicate the amount of motion as a percentage.
        motionLevel = motionDetector.ProcessFrame(bitmap);

        // You can also access the detected motion blobs as follows:
        // ((AForge.Vision.Motion.BlobCountingObjectsProcessing) motionDetector.Processor).ObjectRectangles [i]...
    }

    reader.Close();
}

// Play around with this function to tweak results.
public static AForge.Vision.Motion.MotionDetector GetDefaultMotionDetector ()
{
    AForge.Vision.Motion.IMotionDetector detector = null;
    AForge.Vision.Motion.IMotionProcessing processor = null;
    AForge.Vision.Motion.MotionDetector motionDetector = null;

    //detector = new AForge.Vision.Motion.TwoFramesDifferenceDetector()
    //{
    //  DifferenceThreshold = 15,
    //  SuppressNoise = true
    //};

    //detector = new AForge.Vision.Motion.CustomFrameDifferenceDetector()
    //{
    //  DifferenceThreshold = 15,
    //  KeepObjectsEdges = true,
    //  SuppressNoise = true
    //};

    detector = new AForge.Vision.Motion.SimpleBackgroundModelingDetector()
    {
        DifferenceThreshold = 10,
        FramesPerBackgroundUpdate = 10,
        KeepObjectsEdges = true,
        MillisecondsPerBackgroundUpdate = 0,
        SuppressNoise = true
    };

    //processor = new AForge.Vision.Motion.GridMotionAreaProcessing()
    //{
    //  HighlightColor = System.Drawing.Color.Red,
    //  HighlightMotionGrid = true,
    //  GridWidth = 100,
    //  GridHeight = 100,
    //  MotionAmountToHighlight = 100F
    //};

    processor = new AForge.Vision.Motion.BlobCountingObjectsProcessing()
    {
        HighlightColor = System.Drawing.Color.Red,
        HighlightMotionRegions = true,
        MinObjectsHeight = 10,
        MinObjectsWidth = 10
    };

    motionDetector = new AForge.Vision.Motion.MotionDetector(detector, processor);

    return (motionDetector);
}
Run Code Online (Sandbox Code Playgroud)