【问题标题】:Looking for a function for motion detection on emgucv在 emgucv 上寻找运动检测功能
【发布时间】:2012-09-25 09:51:24
【问题描述】:

我是 emgu 简历的新手;我正在尝试找到进行运动检测的代码。我试过这个:

CvInvoke.cvAbsDiff(frame, _backgroundImage, BgDifference);

...但我有照明问题。我想把有运动的像素变白,然后在只有一个矩形的地方画一个矩形,但是我用白色像素取更多的区域。

我需要做什么?我可以试试其他功能吗?

【问题讨论】:

标签: c# emgucv motion-detection


【解决方案1】:

将单帧转换为灰度。 将新帧从实时转换为灰度。 在第一帧和新帧之间进行实时抽象。 其结果是由前两者之间的差异组成的第三个新框架。使用腐蚀和阈值处理来获得一个框架,其中白色代表运动部分,黑色代表空间的其余部分。

这是一段代码:

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);
       }

【讨论】:

    【解决方案2】:

    您可以使用MotionHistory 类。 EmguCV 包含一个运动检测示例(如果不再存在,您可以看到它here)。有了这个类,就可以得到一个motionImage,然后只需要统计像素就可以查看最大的区域了。

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多