Click here to Skip to main content
15,867,921 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 71.8K   17.2K   90  
How to teach a program to recognize something within a video stream.
///////////////////////////////////////////////////////////////////////////////
//
//  ClassifierUI.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.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using OpenCvSharp;

namespace OpenCVDemo
{
    /// <summary>
    /// Classifier UI.
    /// </summary>
    public partial class ClassifierUI : UserControl
    {
        /// <summary>
        /// Classifier.
        /// </summary>
        protected Classifier m_classifier = new Classifier();

        /// <summary>
        /// Capture.
        /// </summary>
        protected CvCapture m_cvCapture = null;

        /// <summary>
        /// Last video file name.
        /// </summary>
        protected string m_videoFileName = "";

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

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

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

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

        /// <summary>
        /// Load video.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, EventArgs e)
        {
            timer1.Enabled = false;
            button6.Enabled = false;
            OpenFileDialog ofd = new OpenFileDialog();
            DialogResult dr = ofd.ShowDialog();
            if (dr != DialogResult.OK) return;

            if (m_cvCapture != null)
            {
                m_cvCapture.Dispose();
            }
            m_cvCapture = CvCapture.FromFile(ofd.FileName);
            m_videoFileName = ofd.FileName;
        }

        /// <summary>
        /// Append a feature file
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button3_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            DialogResult dr = ofd.ShowDialog();
            if ((dr == DialogResult.OK) || (dr == DialogResult.Yes))
            {
                if (ofd.FileNames == null)
                {
                    m_classifier.AppendFeature(ofd.FileName);
                }
                else
                {
                    if (ofd.FileNames.Length == 0)
                    {
                        m_classifier.AppendFeature(ofd.FileName);
                    }
                    else
                    {
                        for (int i = 0; i < ofd.FileNames.Length; i++)
                        {
                            m_classifier.AppendFeature(ofd.FileNames[i]);
                        }
                    }
                }
                m_classifier.Reset();
            }
        }

        /// <summary>
        /// Clear features.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button2_Click(object sender, EventArgs e)
        {
            m_classifier.Features.Clear();
            m_classifier.Reset();
        }

        /// <summary>
        /// Classify.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button4_Click(object sender, EventArgs e)
        {
            timer1.Enabled = false;
            m_classifier.Reset();
            if (m_cvCapture != null)
            {
                m_cvCapture.Dispose();
            }
            m_cvCapture = CvCapture.FromFile(m_videoFileName);
            timer1.Enabled = true;
            button6.Enabled = true;
            
        }

        /// <summary>
        /// Classify and save.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button5_Click(object sender, EventArgs e)
        {
            timer1.Enabled = false;
            m_classifier.Reset();
            button6.Enabled = false;
            if (m_cvCapture != null)
            {
                m_cvCapture.Dispose();
            }
            m_cvCapture = CvCapture.FromFile(m_videoFileName);
            SaveFileDialog sfd = new SaveFileDialog();
            DialogResult dr = sfd.ShowDialog();
            if (dr != DialogResult.OK)
            {
                return;
            }
            if (m_cvVideoWriter != null)
            {
                m_cvVideoWriter.Dispose();
                m_cvVideoWriter = null;
            }

            m_cvVideoWriter = new CvVideoWriter(sfd.FileName,
                FourCC.DIB, // IYUB, MSVC, DIB, CVID are supported.
                10,
                new CvSize(m_backBuffer.Width, m_backBuffer.Height));
            timer1.Enabled = true;
            button6.Enabled = true;
        }

        /// <summary>
        /// Timer, grabs an input frame if there is one,
        /// triggers processing and handles double buffering as needed.
        /// 
        /// Also uses simple double buffering.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void timer1_Tick(object sender, EventArgs e)
        {
            // This is demo code, so not everything is thread safe but
            // Since the obvious most annoying race is m_cvCapture changing,
            // This made sence to guard against.
            //
            // Grab a local copy of the reference then act on it.
            CvCapture threadSafeCapture = m_cvCapture;
            if (threadSafeCapture == null) return;
            threadSafeCapture.GrabFrame();
            IplImage img = threadSafeCapture.RetrieveFrame();

            // Video is done.
            if ((img == null) || (threadSafeCapture.PosFrames == threadSafeCapture.FrameCount))
            {
                m_cvCapture.Dispose();
                m_cvCapture = null;
                timer1.Enabled = false;
                button6.Enabled = false;
                if (m_cvVideoWriter != null)
                {
                    m_cvVideoWriter.Dispose();
                    m_cvVideoWriter = null;
                }
                return;
            }

            // Make sure the bitmap buffers are the right size.
            setupBuffers(img.Width, img.Height);

            // Draw the raw frame to the left.
            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);
            }

            // Learn if learning.
            m_classifier.Classify(img, m_backBuffer);

            img.Dispose();

            // Save if saving.
            if (m_cvVideoWriter != null)
            {
                CvVideoWriter cvw = m_cvVideoWriter;
                if (cvw != null)
                {
                    if (!cvw.IsDisposed)
                    {
                        IplImage ipltmp = IplImage.FromBitmap(m_backBuffer);
                        cvw.WriteFrame(ipltmp);
                        ipltmp.Dispose();
                    }
                }
            }

            // Dispose of the temp. bitmap.
            tmp.Dispose();


            // Swap left and right buffers and display.
            System.Drawing.Bitmap btmp = m_backBuffer;
            m_backBuffer = m_frontBuffer;
            m_frontBuffer = btmp;
            pictureBox1.Image = m_frontBuffer;
        }

        /// <summary>
        /// Setup the image buffers.
        /// </summary>
        /// <param name="w"></param>
        /// <param name="h"></param>
        protected void setupBuffers(int w, int h)
        {
            bool doSetup = true;
            if (m_backBuffer != null)
            {
                if ((m_backBuffer.Width == w) && (m_backBuffer.Height == h))
                {
                    doSetup = false;
                }
            }
            if (doSetup)
            {
                if (m_backBuffer != null) m_backBuffer.Dispose();
                m_backBuffer = new System.Drawing.Bitmap(w, h, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
            }
        }

        /// <summary>
        /// Stop button.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button6_Click(object sender, EventArgs e)
        {
            timer1.Enabled = false;
        }
    }
}

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