Click here to Skip to main content
15,885,980 members
Articles / General Programming / Algorithms

Image Tracking and Computer Vision Using Fourier Image Correlation

Rate me:
Please Sign up or sign in to vote.
4.98/5 (27 votes)
23 Apr 2013CPOL15 min read 72.1K   17.2K   90  
How to teach a program to recognize something within a video stream.
///////////////////////////////////////////////////////////////////////////////
//
//  CamCorderUI.cs
//
//  By Philip R. Braica (HoshiKata@aol.com, VeryMadSci@gmail.com)
//
//  Distributed under the The Code Project Open License (CPOL)
//  http://www.codeproject.com/info/cpol10.aspx
///////////////////////////////////////////////////////////////////////////////

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 OpenCvSharp;

namespace OpenCVDemo
{
    /// <summary>
    /// Simple cam corder for testing.
    /// </summary>
    public partial class CamCorderUI : UserControl
    {
        #region Protected objects.
        /// <summary>
        /// Capture device.
        /// </summary>
        protected CvCapture m_capture = null;

        /// <summary>
        /// Locking object.
        /// </summary>
        protected object m_lockObj = new object();

        /// <summary>
        /// Video writer.
        /// </summary>
        protected CvVideoWriter m_vidWriter = null;

        /// <summary>
        /// Back buffer.
        /// </summary>
        protected Bitmap m_backBuffer = null;

        /// <summary>
        /// Front buffer.
        /// </summary>
        protected Bitmap m_frontBuffer = null;
        #endregion

        #region Public Properies: Connected, Recording, FlipVertical, FlipHorizontal
        /// <summary>
        /// Camera is connected.
        /// </summary>
        public bool Connected { get; protected set; }

        /// <summary>
        /// Recording.
        /// </summary>
        public bool Recording 
        { 
            get { return button3.Text == "Stop"; } 
            set { button3.Text = value ? "Stop" : "Record"; }
        }

        /// <summary>
        /// Flip vertical.
        /// </summary>
        public bool FlipVertical 
        { 
            get { return checkBox1.Checked; } 
            set { checkBox1.Checked = value; } 
        }

        /// <summary>
        /// Flip vertical.
        /// </summary>
        public bool FlipHorizontal 
        { 
            get { return checkBox2.Checked; }
            set { checkBox2.Checked = value; } 
        }

        #endregion

        /// <summary>
        /// Constructor.
        /// </summary>
        public CamCorderUI()
        {
            InitializeComponent();
        }

        /// <summary>
        /// Connect
        /// </summary>
        /// <returns></returns>
        public bool Connect()
        {
            return Connect(0);
        }

        /// <summary>
        /// Connect to a specific device.
        /// </summary>
        /// <param name="device"></param>
        /// <returns></returns>
        public bool Connect(int device)
        {
            if (m_capture != null)
            {
                m_capture.Dispose();
                m_capture = null;
            }

            try
            {
                m_capture = CvCapture.FromCamera(device);
                m_capture.FrameWidth = (int)numericUpDown1.Value;
                m_capture.FrameHeight = (int)numericUpDown2.Value;
            }
            catch (Exception)
            {
                return false;
            }

            timer1.Enabled = true;
            Connected = true;
            return true;
        }

        /// <summary>
        /// Disconnect.
        /// </summary>
        /// <returns></returns>
        public bool Disconnect()
        {
            CvCapture tmp = m_capture;

            m_capture = null;
            if (tmp != null)
            {
                if (m_vidWriter != null)
                {
                    lock (m_vidWriter)
                    {
                        CvVideoWriter cvwriter = m_vidWriter;
                        m_vidWriter = null;
                        cvwriter.Dispose();
                    }
                }
                tmp.Dispose();
            }
            Connected = false;
            return false;
        }

        /// <summary>
        /// Connect button.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, EventArgs e)
        {
            if (button1.Text == "Connect")
            {
                timer1.Enabled = false;
                numericUpDown1.Enabled = false;
                numericUpDown2.Enabled = false;
                Connect();
                button1.Text = "Disconnect";
                timer1.Enabled = true;
            }
            else
            {
                timer1.Enabled = false;
                numericUpDown1.Enabled = true;
                numericUpDown2.Enabled = true;
                Disconnect();
                button1.Text = "Connect";
            }
        }

        /// <summary>
        /// Record button.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button3_Click(object sender, EventArgs e)
        {
            if (button3.Text == "Record")
            {
                SaveFileDialog sfd = new SaveFileDialog();
                DialogResult dr = sfd.ShowDialog();
                if ((dr == DialogResult.OK) || (dr == DialogResult.Yes))
                {
                    m_vidWriter = new CvVideoWriter(
                        sfd.FileName,
                         FourCC.DIVX, 
                        10, 
                        new CvSize((int)numericUpDown1.Value, (int)numericUpDown2.Value));
                    button3.Text = "Stop";
                }

            }
            else
            {
                button3.Text = "Record";
            }
        }

        /// <summary>
        /// Timer went off.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void timer1_Tick(object sender, EventArgs e)
        {
            if (!Connected)
            {
                return;
            }

            // Grab capture thread safe.
            CvCapture cap = m_capture;
            if (cap == null)
            {
                return;
            }

            // Grab our potential destinations.

            // Grab the frame.
            cap.GrabFrame();
            IplImage img = cap.RetrieveFrame();
            if (img == null)
            {
                Connected = false;
                m_capture.Dispose();
                m_capture = null;
            }

            if (FlipVertical)
            {
                img.Flip(img, FlipMode.X);
            }
            if (FlipHorizontal)
            {
                img.Flip(img, FlipMode.Y);
            }

            // Push to back buffer.
            // Swap
            lock (m_lockObj)
            {
                if (m_backBuffer == null)
                {
                    m_backBuffer = new System.Drawing.Bitmap(320, 240, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
                }
                using (System.Drawing.Bitmap tmp = img.ToBitmap())
                {
                    using (System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(m_backBuffer))
                    {
                        g.DrawImage(tmp, 0, 0, m_backBuffer.Width, m_backBuffer.Height);
                    }
                }

                CvVideoWriter writer = m_vidWriter;
                // If recording push to file.
                if (Recording && (writer != null))
                {
                    writer.WriteFrame(img);
                }

                System.Drawing.Bitmap swap = m_backBuffer;
                m_backBuffer = m_frontBuffer;
                m_frontBuffer = swap;
                pictureBox1.Image = m_frontBuffer;
            }

            img.Dispose();
        }

        /// <summary>
        /// Camcorder delay.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void numericUpDown3_ValueChanged(object sender, EventArgs e)
        {
            timer1.Interval = (int)numericUpDown3.Value;
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Technical Lead
United States United States
Phil is a Principal Software developer focusing on weird yet practical algorithms that run the gamut of embedded and desktop (PID loops, Kalman filters, FFTs, client-server SOAP bindings, ASIC design, communication protocols, game engines, robotics).

In his personal life he is a part time mad scientist, full time dad, and studies small circle jujitsu, plays guitar and piano.

Comments and Discussions