在emgucv上寻找运动检测功能 [英] Looking for a function for motion detection on emgucv

查看:216
本文介绍了在emgucv上寻找运动检测功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是emgu cv的新手;我正在尝试查找进行运动检测的代码。我尝试过:

I am new to emgu cv; I'm trying to find a code that makes motion detection. I tried this:

CvInvoke.cvAbsDiff(frame, _backgroundImage, BgDifference);

...但是我有照明问题。我想将运动的像素变白,然后在其中仅画一个矩形,但是我用白色像素覆盖了更多区域。

... but I have lighting problems. I want to get white the pixels where there was motion, and then draw a rectangle there only one rectangle, but I take more areas with white pixels.

我需要什么去做?我可以尝试另一个功能吗?

What do I need to do? Could I try ananother function?

推荐答案

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

Convert a single frame to grayscale. Convert a new frames from real time into grayscale. Make abstractions between the first frame and new frame from real time. The result of this is a third, new frame comprised of the differences between the first two. Use erosion and thresholding for that to get a frame with white representing the motioned section and black representing the rest of the space.

这里是一段代码:

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

这篇关于在emgucv上寻找运动检测功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆