2013年2月21日 星期四

【AForge.NET】移動資訊偵測 Motion Detection


移動資訊偵測器(Motion Detector)

  1. CustomFrameDifferenceDetector
  2. SimpleBackgroundModelingDetector
  3. TwoFramesDifferenceDetector


【方法Methods】
.ProcessFrame()
.Reset()

【參數Properties】
.MotionLevel(移動能量)
.MotionFrame(偵測區域)

移動資訊處理方式(Motion Processing)

  1. BlobCountingObjectsProcessing
  2. GridMotionAreaProcessing
  3. MotionAreaHighlighting
  4. MotionBorderHighlighting

【方法Methods】
.ProcessFrame()
.Reset()

【參數Properties】
.HighlightColor(標記顏色,初始設定紅色)
.MinObjectsHeight(偵測物件最小高度,初始設定16)
.MinObjectsWidth(偵測物件最小寬度,初始設定16)
.ObjectsCount(偵測物件數量)

以下兩張圖是使用AForge所做出來的移動偵測,不管是灰階、二值化或彩色影像都可以執行




using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Imaging;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using AForge;
using AForge.Video;
using AForge.Video.VFW;
using AForge.Video.DirectShow;
using AForge.Imaging;
using AForge.Imaging.Filters;
using AForge.Vision.Motion;

namespace MarcCS_TEST
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            InitializeMotionDetector();//移動資訊偵測器初始化
        }

        //設定偵測器與處理器是哪幾種方法
        CustomFrameDifferenceDetector CFDdetector = new CustomFrameDifferenceDetector();
        SimpleBackgroundModelingDetector SBMdetector = new SimpleBackgroundModelingDetector();
        TwoFramesDifferenceDetector TFDdetector = new TwoFramesDifferenceDetector();
        BlobCountingObjectsProcessing BCOprocessing = new BlobCountingObjectsProcessing();
        GridMotionAreaProcessing GMAprocessing = new GridMotionAreaProcessing();
        MotionAreaHighlighting MAHprocessing = new MotionAreaHighlighting();
        MotionBorderHighlighting MBHprocessing = new MotionBorderHighlighting();
        //建立3個偵測器
        MotionDetector MarcDetector1 = null;
        MotionDetector MarcDetector2 = null;
        MotionDetector MarcDetector3 = null;

        public void InitializeMotionDetector()
        {
            //移動資訊以藍色顯示
            BCOprocessing.HighlightColor = Color.Blue;
            BCOprocessing.MinObjectsHeight = 16;
            BCOprocessing.MinObjectsWidth = 16;
            MarcDetector1 = new MotionDetector(CFDdetector, BCOprocessing);
            MarcDetector2 = new MotionDetector(SBMdetector, BCOprocessing);
            MarcDetector3 = new MotionDetector(TFDdetector, BCOprocessing);
        }

        public FilterInfoCollection USB_Webcams = null;
        public VideoCaptureDevice Cam = null;

        private void MarcGrayScale(ref Bitmap SrcImg)
        {
            Grayscale grayscaleFilter = new Grayscale(0.299, 0.587, 0.114);
            SrcImg = grayscaleFilter.Apply(SrcImg);
        }
        private void MarcCustomFrameDifferenceDetector(ref Bitmap SrcImg)
        {

            if (MarcDetector1.ProcessFrame(SrcImg) > 0.2)
            {                        
            }       
        }
        private void MarcSimpleBackgroundModelingDetector(ref Bitmap SrcImg)
        {
            if (MarcDetector2.ProcessFrame(SrcImg) > 0.2)
            {
            }
        }
        private void MarcTwoFramesDifferenceDetector(ref Bitmap SrcImg)
        {
            if (MarcDetector3.ProcessFrame(SrcImg) > 0.2)
            {
            }
        }
  
        private void bt_start_Click(object sender, EventArgs e)
        {
            Cam.Start();
        }

        private void bt_stop_Click(object sender, EventArgs e)
        {
            if (Cam.IsRunning)
            {
                Cam.Stop();
            }
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            USB_Webcams = new FilterInfoCollection(FilterCategory.VideoInputDevice);
            Cam = new VideoCaptureDevice(USB_Webcams[0].MonikerString);
            Cam.NewFrame += new NewFrameEventHandler(Cam_NewFrame);
            
        }
        void Cam_NewFrame(object sender, NewFrameEventArgs eventArgs)
        {
            Bitmap CamImg = (Bitmap)eventArgs.Frame.Clone();
            Bitmap SrcImg = CamImg.Clone() as Bitmap;
         
            //勾選check box執行灰階並二值化
            if (this.checkBox1.Checked == true)
            { 
                MarcGrayScale(ref SrcImg);
                Threshold ThresholdFilter = new Threshold(15);
                SrcImg = ThresholdFilter.Apply(SrcImg);
            }
            //勾選check box開始執行該偵測器
            if (this.checkBox2.Checked == true)
            {        
                MarcCustomFrameDifferenceDetector(ref SrcImg);
            }
            if (this.checkBox3.Checked == true)
            {   
                MarcSimpleBackgroundModelingDetector(ref SrcImg);
            }
            if (this.checkBox4.Checked == true)
            {              
                MarcTwoFramesDifferenceDetector(ref SrcImg);
            }
            pictureBox2.Image = SrcImg;
            pictureBox1.Image = CamImg;
        }

        private void Form1_FormClosed(object sender, FormClosedEventArgs e)
        {
            if (Cam != null)
                if (Cam.IsRunning)
                    Cam.Stop();
        }
    }
}

0 意見:

張貼留言