2013年2月22日 星期五

【AForge.NET】使用背景相減法偵測移動區塊 Motion Detector


  • 使用背景相減法偵測移動資訊。
  • 在這裡用到CustomFrameDifferenceDetector()當作偵測器
  • 使用BlobCountingObjectsProcessing擷取移動區塊
  • 【應用】對於移動偵測區塊作二值化


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();
        }
    
        Stopwatch MotionUpdate = new Stopwatch();
        CustomFrameDifferenceDetector detector = new CustomFrameDifferenceDetector();
        BlobCountingObjectsProcessing processing = new BlobCountingObjectsProcessing();
        MotionDetector MarcDetector = null;     

        public void InitializeMotionDetector()
        {
            detector.DifferenceThreshold = 65;//移動門檻值
            detector.SuppressNoise = true;
            processing.MinObjectsHeight = 30;//最小偵測框高度
            processing.MinObjectsWidth = 20;//最小偵測框寬度

            MotionUpdate.Reset();
            MotionUpdate.Start();
            MarcDetector = new MotionDetector(detector, processing);
        }

        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 MarcBlobCountingObjectsProcessing(ref Bitmap SrcImg)
        {
            if (MarcDetector.ProcessFrame(SrcImg) > 0.005)
            {
                Rectangle[] ObjectRects = processing.ObjectRectangles;

                foreach (Rectangle rect in ObjectRects)
                {
                    Rectangle ThRect = new Rectangle(rect.X, rect.Y, rect.Width, rect.Height);
                    BitmapData data = SrcImg.LockBits(ThRect, ImageLockMode.ReadWrite,
                        SrcImg.PixelFormat);
                    Threshold filter = new Threshold(30);
                    filter.ApplyInPlace(data);
                    SrcImg.UnlockBits(data);
                }
            }
        }

        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;
            
            //彩色影像轉灰階影像
            MarcGrayScale(ref SrcImg);
            //背景更新時間3000等於3秒
            if (MotionUpdate.Elapsed.TotalMilliseconds >= 3000)
            {
                MotionUpdate.Stop();
                MotionUpdate.Reset();
                MotionUpdate.Start();
                detector.SetBackgroundFrame(SrcImg);
            }      
            //啟動移動偵測
            MarcBlobCountingObjectsProcessing(ref SrcImg);
 
            pictureBox2.Image = SrcImg;
            pictureBox1.Image = CamImg;
        }

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



1 則留言:

  1. 其中 Stopwatch MotionUpdate = new Stopwatch();
    BitmapData data = SrcImg.LockBits(ThRect, ImageLockMode.ReadWrite,
    有誤耶,為何呢?
    分別是 Stopwatch BitmapData ImageLockMode 底下有錯誤的紅波浪線

    回覆刪除