本文介绍了查找放置在公共区域上的所需对象的示例视频(黑白).如果不能,请在Aforge SDK上进行模式设置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我使用的是AForge.net框架,其中的静态相机在空旷的空间拍摄,能够检测到扔在那儿的有害物品,也能够检测到火灾.如何保存背景清晰的参考图像?从样本视频流图像?有人可以帮我吗?



Hi all,

I''m using AForge.net framework, with a static camera shoots on a empty space and able to detect unwanted stuffs being thrown there and also able to detect fire. How can i save a reference image of a clear background & stream images from the sample video? Can anyone please help me in this?



// Fire Alert System
// Copyright © Andrew Kirillov, 2008
// [E-Mail Removed]

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;

using AForge;
using AForge.Robotics.Lego;
using AForge.Video;
using AForge.Video.DirectShow;
using AForge.Imaging;
using AForge.Imaging.Filters;


namespace FireAlertSystem
{
    public partial class MainForm : Form
    {

        //add secondForm = Security into MainForm
        LegoCamera.SecurityPanel secondForm = new LegoCamera.SecurityPanel();
        //create a sound player
        private System.Media.SoundPlayer myPlayer;

        // statistics length
        private const int statLength = 15;
        // current statistics index
        private int statIndex = 0;
        // ready statistics values
        private int statReady = 0;
        // statistics array
        private int[] statCount = new int[statLength];
        private FilterInfoCollection videoDevices;

        // background thread and objects used for synchronization
        private Thread thread = null;
        private AutoResetEvent needToDriveEvent = null;
        private bool needToExit = false;

        // motors'' power to set
        private float panMotorPower  = 0;
        private float tiltMotorPower = 0;

        private float lastPanMotorPower  = 0;
        private float lastTiltMotorPower = 0;

        // image processing stuff
        ColorFiltering colorFilter = new ColorFiltering( );
        GrayscaleBT709 grayscaleFilter = new GrayscaleBT709( );
        BlobCounter blobCounter = new BlobCounter( );

        // Form contructor
        public MainForm( )
        {
            InitializeComponent( );

            // configure blob counter
            blobCounter.MinWidth  = 25;
            blobCounter.MinHeight = 25;
            blobCounter.FilterBlobs = true;
            blobCounter.ObjectsOrder = ObjectsOrder.Size;

            // collect cameras list
            try
            {
                // enumerate video devices
                videoDevices = new FilterInfoCollection( FilterCategory.VideoInputDevice );

                if ( videoDevices.Count == 0 )
                    throw new ApplicationException( );

                // add all devices to combo
                foreach ( FilterInfo device in videoDevices )
                {
                    camerasCombo.Items.Add( device.Name );
                }

                camerasCombo.SelectedIndex = 0;
            }
            catch ( ApplicationException )
            {
                camerasCombo.Items.Add( "No local capture devices" );
                videoDevices = null;
            }

            // enable controls, which allow to connect (disconnected state yet)
            EnableConnectionControls( false );

            predefinedColorsCombo.SelectedIndex = 0;
        }

        // Application form is going to close
        private void MainForm_FormClosing( object sender, FormClosingEventArgs e )
        {
            Disconnect( );
        }

        // On "Connect" button click
        private void connectButton_Click( object sender, EventArgs e )
        {
            if ( Connect( ) )
            {
                EnableConnectionControls( true );
            }
        }

        // On "Disconnect" button click
        private void disconnectButton_Click( object sender, EventArgs e )
        {
            Disconnect( );
            EnableConnectionControls( false );
        }

        // Enable/disable connection controls
        private void EnableConnectionControls( bool enable )
        {
            camerasCombo.Enabled = ( ( !enable ) && ( videoDevices != null ) );
            connectButton.Enabled = ( ( !enable ) && ( videoDevices != null ) );

            disconnectButton.Enabled = enable;
            panController.Enabled = enable;
            tiltController.Enabled = enable;
        }

        // Predefined detection color has changed
        private void predefinedColorsCombo_SelectedIndexChanged( object sender, EventArgs e )
        {
            bool enableCustomConfiguration = ( predefinedColorsCombo.SelectedIndex == predefinedColorsCombo.Items.Count - 1 );

            redMinUpDown.Enabled   = enableCustomConfiguration;
            redMaxUpDown.Enabled   = enableCustomConfiguration;
            greenMinUpDown.Enabled = enableCustomConfiguration;
            greenMaxUpDown.Enabled = enableCustomConfiguration;
            blueMinUpDown.Enabled  = enableCustomConfiguration;
            blueMaxUpDown.Enabled  = enableCustomConfiguration;

            switch ( predefinedColorsCombo.SelectedIndex )
            {
                case 0: // red
                    redMinUpDown.Value   = 140;
                    redMaxUpDown.Value   = 255;
                    greenMinUpDown.Value = 0;
                    greenMaxUpDown.Value = 100;
                    blueMinUpDown.Value  = 0;
                    blueMaxUpDown.Value  = 100;
                    break;

                case 1: // blue
                    redMinUpDown.Value   = 0;
                    redMaxUpDown.Value   = 100;
                    greenMinUpDown.Value = 0;
                    greenMaxUpDown.Value = 100;
                    blueMinUpDown.Value  = 100;
                    blueMaxUpDown.Value  = 255;
                    break;

                default: // custom settings
                    redMinUpDown.Value   = 0;
                    redMaxUpDown.Value   = 255;
                    greenMinUpDown.Value = 0;
                    greenMaxUpDown.Value = 255;
                    blueMinUpDown.Value  = 0;
                    blueMaxUpDown.Value  = 255;
                    break;
            }
        }

        // Minimum red value has changed
        private void redMinUpDown_ValueChanged( object sender, EventArgs e )
        {
            colorFilter.Red = new IntRange(
                Convert.ToInt32( redMinUpDown.Value ), Convert.ToInt32( redMaxUpDown.Value ) );
        }

        // Maximum red value has changed
        private void redMaxUpDown_ValueChanged( object sender, EventArgs e )
        {
            colorFilter.Red = new IntRange(
                Convert.ToInt32( redMinUpDown.Value ), Convert.ToInt32( redMaxUpDown.Value ) );
        }

        // Minimum green value has changed
        private void greenMinUpDown_ValueChanged( object sender, EventArgs e )
        {
            colorFilter.Green = new IntRange(
                Convert.ToInt32( greenMinUpDown.Value ), Convert.ToInt32( greenMaxUpDown.Value ) );
        }

        // Maximum green value has changed
        private void greenMaxUpDown_ValueChanged( object sender, EventArgs e )
        {
            colorFilter.Green = new IntRange(
                Convert.ToInt32( greenMinUpDown.Value ), Convert.ToInt32( greenMaxUpDown.Value ) );
        }

        // Minimum blue value has changed
        private void blueMinUpDown_ValueChanged( object sender, EventArgs e )
        {
            colorFilter.Blue = new IntRange(
                Convert.ToInt32( blueMinUpDown.Value ), Convert.ToInt32( blueMaxUpDown.Value ) );
        }

        // Maximum blue value has changed
        private void blueMaxUpDown_ValueChanged( object sender, EventArgs e )
        {
            colorFilter.Blue = new IntRange(
                Convert.ToInt32( blueMinUpDown.Value ), Convert.ToInt32( blueMaxUpDown.Value ) );
        }

        // Connect to camera
        private bool Connect( )
        {
            // close previous connection if any
            Disconnect( );

            // connect to camera
            VideoCaptureDevice videoSource = new VideoCaptureDevice( videoDevices[camerasCombo.SelectedIndex].MonikerString );
            videoSource.DesiredFrameSize = new Size( 320, 240);
            videoSource.DesiredFrameRate = 15;

            videoSourcePlayer.VideoSource = videoSource;
            videoSourcePlayer.Start( );

            // create event used to signal thread about updates to power state
            needToDriveEvent = new AutoResetEvent( false );
            needToExit = false;

            // create background thread which drives Lego
            thread = new Thread( new ThreadStart( WorkerThread ) );
            thread.Start( );

            return true;
        }

        // Disconnect from camera
        private void Disconnect( )
        {
            if ( thread != null )
            {
                // stop background thread
                needToExit = true;
                needToDriveEvent.Set( );
                thread.Join( );

                needToDriveEvent.Close( );
                needToDriveEvent = null;
                thread = null;
            }

            // stop camera
            videoSourcePlayer.SignalToStop( );
            videoSourcePlayer.WaitForStop( );
        }

        // Pan controller''s position has changed
        private void panController_PositionChanged( float position )
        {
            panMotorPower = -position;
            needToDriveEvent.Set( );
        }

        // Tilt controller''s position has changed
        private void tiltController_PositionChanged( float position )
        {
            tiltMotorPower = -position;
            needToDriveEvent.Set( );
        }

        // Worker thread which is used to set motors'' power
        private void WorkerThread( )
        {
            float newPanMotorPower  = 0;
            float newTiltMotorPower = 0;

            while ( true )
            {
                // wait for events
                needToDriveEvent.WaitOne( );

                // should thread exit?
                if ( needToExit )
                {
                    SetMotorPowers( 0, 0 );
                    break;
                }

                lock ( this )
                {
                    newPanMotorPower  = panMotorPower;
                    newTiltMotorPower = tiltMotorPower;
                }

                SetMotorPowers( newPanMotorPower, newTiltMotorPower );
            }
        }

        // Set pan and tilt motors'' power
        private void SetMotorPowers( float panMotorPower, float tiltMotorPower )
        {
            // control pan motor
            if ( panMotorPower != lastPanMotorPower )
            {
                lastPanMotorPower = panMotorPower;

                int power = (int) ( 5 * panMotorPower + 55 * Math.Sign( panMotorPower ) );

                NXTBrick.MotorState motorsState = new NXTBrick.MotorState( );
                // check if we need to stop
                if ( power == 0 )
                {
                    motorsState.Mode       = NXTBrick.MotorMode.None;
                    motorsState.RunState   = NXTBrick.MotorRunState.Idle;
                }
                else
                {
                    motorsState.Mode       = NXTBrick.MotorMode.On;
                    motorsState.RunState   = NXTBrick.MotorRunState.Running;
                    motorsState.TachoLimit = 0;
                    motorsState.Power      = power;
                    motorsState.TurnRatio  = 80;
                }
            }

            // controls tilt motor
            if ( tiltMotorPower != lastTiltMotorPower )
            {
                lastTiltMotorPower = tiltMotorPower;

                int power = (int) ( 5 * tiltMotorPower + 55 * Math.Sign( tiltMotorPower ) );

                NXTBrick.MotorState motorsState = new NXTBrick.MotorState( );
                // check if we need to stop
                if ( power == 0 )
                {
                    motorsState.Mode      = NXTBrick.MotorMode.None;
                    motorsState.RunState  = NXTBrick.MotorRunState.Idle;
                }
                else
                {
                    motorsState.Mode       = NXTBrick.MotorMode.On;
                    motorsState.RunState   = NXTBrick.MotorRunState.Running;
                    motorsState.TachoLimit = 0;
                    motorsState.Power      = power;
                    motorsState.TurnRatio  = 80;
                }
            }
        }

        // New video frame has arrived
        private void videoSourcePlayer_NewFrame( object sender, ref Bitmap image )
        {
            if ( detectionCheck.Checked )
            {
                bool showOnlyObjects = onlyObjectsCheck.Checked;

                Bitmap objectsImage = null;

                // color filtering
                if ( showOnlyObjects )
                {
                    objectsImage = image;
                    colorFilter.ApplyInPlace( image );
                }
                else
                {
                    objectsImage = colorFilter.Apply( image );
                }

                // lock image for further processing
                BitmapData objectsData = objectsImage.LockBits( new Rectangle( 0, 0, image.Width, image.Height ),
                    ImageLockMode.ReadOnly, image.PixelFormat );

                // grayscaling
                UnmanagedImage grayImage = grayscaleFilter.Apply( new UnmanagedImage( objectsData ) );

                // unlock image
                objectsImage.UnlockBits( objectsData );

                // locate blobs
                blobCounter.ProcessImage( grayImage );
                Rectangle[] rects = blobCounter.GetObjectsRectangles( );

                if ( rects.Length > 0 )
                {
                    Rectangle objectRect = rects[0];

                    // draw rectangle around detected object
                    Graphics g = Graphics.FromImage( image );

                    using ( Pen pen = new Pen( Color.FromArgb( 16, 255, 160 ), 3 ) )
                    {
                        g.DrawRectangle( pen, objectRect );
                    }

                    g.Dispose( );

                    if ( trackingCheck.Checked )
                    {
                        int objectX = objectRect.X + objectRect.Width / 2 - image.Width / 2;
                        int objectY = image.Height / 2 - ( objectRect.Y + objectRect.Height / 2 );

                        panMotorPower  = (float) -( ( ( Math.Abs( objectX ) > 30 ) ? 0.3 : 0.0 ) * Math.Sign( objectX ) );
                        tiltMotorPower = (float)  ( ( ( Math.Abs( objectY ) > 30 ) ? 0.5 : 0.0 ) * Math.Sign( objectY ) );
                    }
                    else
                    {
                        panMotorPower  = 0;
                        tiltMotorPower = 0;
                    }
                }
                else
                {
                    if ( trackingCheck.Checked )
                    {
                        panMotorPower  = 0;
                        tiltMotorPower = 0;
                    }
                }

                // drive camera''s motors if required
                needToDriveEvent.Set( );

                // free temporary image
                if ( !showOnlyObjects )
                {
                    objectsImage.Dispose( );
                }
                grayImage.Dispose();
            }
            DateTime now = DateTime.Now;
            Graphics c = Graphics.FromImage(image);

            // paint current time
            SolidBrush brush = new SolidBrush(Color.Red);
            c.DrawString(now.ToString(), this.Font, brush, new PointF(10, 10));
            brush.Dispose();
            c.Dispose();

            /*if (predefinedColorsCombo.SelectedIndex==0 && detectionCheck.Checked == true && trackingCheck.Checked == true )

        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void openVideofileusingDirectShowToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (openFileDialog.ShowDialog() == DialogResult.OK)
            {
                Connect();
                EnableConnectionControls(true);
                // create video source
                FileVideoSource fileSource = new FileVideoSource(openFileDialog.FileName);

                // open it
                OpenVideoSource(fileSource);
            }
        }

        // Open video source
        private void OpenVideoSource(IVideoSource source)
        {
            // set busy cursor
            this.Cursor = Cursors.WaitCursor;

            // stop current video source
            videoSourcePlayer.SignalToStop();
            videoSourcePlayer.WaitForStop();

            // start new video source
            videoSourcePlayer.VideoSource = source;
            videoSourcePlayer.Start();

            // reset statistics
            statIndex = statReady = 0;

            // start timer
            timer.Start();

            this.Cursor = Cursors.Default;
        }

        private void timer_Tick(object sender, EventArgs e)
        {
            IVideoSource videoSource = videoSourcePlayer.VideoSource;

            if (videoSource != null)
            {
                // get number of frames for the last second
                statCount[statIndex] = videoSource.FramesReceived;

                // increment indexes
                if (++statIndex >= statLength)
                    statIndex = 0;
                if (statReady < statLength)
                    statReady++;

                float fps = 0;

                // calculate average value
                for (int i = 0; i < statReady; i++)
                {
                    fps += statCount[i];
                }
                fps /= statReady;

                statCount[statIndex] = 0;

                fpsLabel.Text = fps.ToString("F2") + "fps";
            }
        }
    }
}

推荐答案


private void openVideofileusingDirectShowToolStripMenuItem_Click(object sender, EventArgs e)
       {
           if (openFileDialog.ShowDialog() == DialogResult.OK)
           {
               Connect();
               EnableConnectionControls(true);
               // create video source
               FileVideoSource fileSource = new FileVideoSource(openFileDialog.FileName);
               // open it
               OpenVideoSource(fileSource);
           }
       }
private void videoSourcePlayer_NewFrame( object sender, ref Bitmap image )
      {
          if ( detectionCheck.Checked )
          {
              bool showOnlyObjects = onlyObjectsCheck.Checked;
              Bitmap objectsImage = null;
              // color filtering
              if ( showOnlyObjects )
              {
                  objectsImage = image;
                  colorFilter.ApplyInPlace( image );
              }
              else
              {
                  objectsImage = colorFilter.Apply( image );
              }
}


这篇关于查找放置在公共区域上的所需对象的示例视频(黑白).如果不能,请在Aforge SDK上进行模式设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-15 14:14