C# 运动检测

C# 运动检测,c#,motion-detection,C#,Motion Detection,我真的无法理解这一点,所以我希望有人能帮我一把^^ 我正试图通过我的网络摄像头检测C#中的运动 到目前为止,我已经尝试了多个库(一个RGE库),但是失败了,因为我不知道如何使用它 起初,我只是想将当前帧的像素与上一帧的像素进行比较,但结果证明,这就像是完全的s**t:I 现在,我的网络摄像头每次从网络摄像头中获取图片时都会运行一个事件“webcam_ImageCaptured”(网络摄像头图像捕获),速度大约为5-10 fps 但是我找不到一个简单的方法从这两张图片中得到区别,或者至少找不到一个

我真的无法理解这一点,所以我希望有人能帮我一把^^

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


到目前为止,我已经尝试了多个库(一个RGE库),但是失败了,因为我不知道如何使用它

起初,我只是想将当前帧的像素与上一帧的像素进行比较,但结果证明,这就像是完全的s**t:I

现在,我的网络摄像头每次从网络摄像头中获取图片时都会运行一个事件“webcam_ImageCaptured”(网络摄像头图像捕获),速度大约为5-10 fps

但是我找不到一个简单的方法从这两张图片中得到区别,或者至少找不到一个合适的方法


有没有人知道我如何做到这一点,尽管这很简单(尽可能)

运动检测是一个复杂的问题,需要大量的计算能力

首先尝试限制要检测的内容。随着复杂性的增加:您是否希望检测是否存在运动?是否要检测运动量?是否要检测图像的哪些区域正在实际移动

我想你只是想知道什么时候发生了变化:

  • 相互减去相邻帧
  • 计算所有像素差的所有平方和
  • 除以像素数
  • 观看网络摄像头流的号码。它会有一定的地面噪音,当有东西移动时会显著上升
  • 尝试将颜色限制在某个特定的通道内,这可能会有所改善

使用您提到的库让运动检测工作起来很简单。下面是一个AForge(版本2.2.4)示例。它适用于视频文件,但您可以轻松地将其调整为网络摄像头事件

Johannes的观点是正确的,但我认为使用这些库可以简化理解基本图像处理的过程

我的应用程序在带有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);
}

“但是失败了,因为我不知道如何使用它。”=>你有没有经历过这个:是的,我已经尝试过了,它给了我一个错误。这实际上给了我一些非常好的结果!)但我发现移动摄像机会把事情搞砸。但是当它静止不动时,它现在可以完美地检测运动^^^然而,我仍然想知道在位图图像显示在表单上之前,如何将检测到的区域应用于位图图像。行
HighlightMotionRegions=true
解决了这一问题。如果愿意,可以关闭它并自己渲染自定义覆盖。这些基本算法适用于固定摄像机。移动摄像机使用更复杂的算法。请记住,上面的代码只是强调运动。它不跟踪对象。您需要自己编写代码来将blob标识帧到帧关联起来。啊,这太棒了。昨天我让它在街上做间谍,效果还不错。但我发现这有点过分乐观,尤其是在刮风的时候:我用不同的阈值、抑制噪声、MinoObjects Swidth和MinoObjects Swidth来改变灵敏度和对象大小;be var reader=new aforme.Video.FFMPEG.VideoFileReader();-否则您将得到一个null ref异常?