【问题标题】:Motion Detection运动检测
【发布时间】:2013-07-11 15:12:19
【问题描述】:

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

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

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

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

现在,我的网络摄像头每次拍摄来自网络摄像头的图片时都会运行一个事件“webcam_ImageCaptured”,大概是 5-10 fps。

但我找不到一种简单的方法来从这两个图像中获取差异,或者至少找不到合适的方法。

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

【问题讨论】:

标签: c# motion-detection


【解决方案1】:

运动检测是一件复杂的事情,需要大量的计算能力。

尝试限制您首先要检测的内容。随着复杂性的增加:你想检测是否有运动吗?你想检测多少运动?是否要检测图像的哪些区域实际在移动?

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

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

【讨论】:

    【解决方案2】:

    使用您提到的库让运动检测工作是微不足道的。以下是 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 标识逐帧关联。
    • 啊,太好了。我昨天在街上监视它,它工作正常。但我发现它有点过分敏感,尤其是在刮风的时候:我
    • 尝试使用 DifferenceThreshold、SuppressNoise、MinObjectsWidth 和 MinObjectsWidth 来改变灵敏度和对象大小。
    • 不应该行 AForge.Video.FFMPEG.VideoFileReader reader = null;是 var reader = new AForge.Video.FFMPEG.VideoFileReader(); - 否则你会得到一个空引用异常?
    猜你喜欢
    • 2013-10-08
    • 2012-11-15
    • 1970-01-01
    • 1970-01-01
    • 2021-10-27
    • 2018-11-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多