Click here to Skip to main content
Click here to Skip to main content

Video Capture using OpenCV with C#

, 7 Feb 2014 CPOL
Rate this:
Please Sign up or sign in to vote.
How to capture video from webcam and video file (*.AVI)

Introduction

The project is about how to capture video from webcam and video file (*.AVI). This project is made in C# and OpenCV.

This will help developers who love the C# and OpenCV environment. The application is totally made in Visual Studio 2010 version C#.NET environment. The application shows how to use the OpenCV with C#.NET, Visual Studio 2010 IDE. This application is totally a demonstration for how to create applications in Visual Studio 2010, C#.NET.

In this, I explain how to configure the Visual Studio 2010, Steps of Installation of EmguCV 2.4.9. version and Computer Environmental Variable in order to execute OpenCV applications.

EmguCV: Let's Start the Work...

EmguCV is a cross platform .NET wrapper to the OpenCV image processing library. It allows one to call OpenCV functions into the .NET languages such as C#, VB, VC++. The wrapper can be compiled in Mono and run on Windows, Linux, Mac OS X, iPhone, iPad and Android devices.

EmguCV is written in C#. It can be compiled in Mono that’s why it is able to run on any platform Mono supports, including Linux, Mac and Android.

Prepare Visual Studio 2010

Step 1: Install EmguCV 2.4.9

Download the EmguCV 2.4.9 version. Install it at c:\ drive location, do not change the path, use default path “C:\Emgu\emgucv-windows-universal-gpu2.4.9.1847”.

Installation path – “C:\Emgu\emgucv-windows-universal-gpu2.4.9.1847”.

Select all check boxes to full package installation.

Step 2: Set Environmental Variable:

Set the following three paths in user variable and system.

  • C:\Emgu\emgucv-windows-universal-gpu 2.4.9.1847\bin;
  • C:\Emgu\emgucv-windows-universal-gpu 2.4.9.1847\bin\x64;
  • C:\Emgu\emgucv-windows-universal-gpu 2.4.9.1847\bin\x86;

Step 3: Configure Visual Studio 2010:

  1. Create new Windows Application project named ‘Two Layer’.
  2. Select Reference, right click, click Add References.
  3. Select Browse tab, look in “C:\Emgu\emgucv-windows-universal-gpu2.4.9.1847\bin”, Select “Emgu.CV.dll”,”Emgu.CV.UI.dll”,”Emgu.Util.dll” three files, and click on ok.

  4. References appear in Solution Explorer.

Capturing Video

Capturing video function, capture video in two ways, one is capture from camera, second is capture from video file. The following part of the code shows how to capture the video from camera.

In this block, capture, FRAME PER SECOND is set to 30 FPS, Video File capture Height and width is set to 240, 320 respectively. then video_seek is initialized with zero '0', This video seek control seeks the video between lower and upper video limits.

The following statement is most useful in this application. it's like Multithreading. When applications go into idle state, function "ProcessFrame" gets called till the end of video frame or till the frame not 'null'.

Application.Idle += ProcessFrame; 

In Capture from video file code, we require the total frames count to set upper limit of video seek control. FOURCC is used for finding the media codec name.

Capture From Camera Code

#region cameracapture
   if (comboBox1.Text == "Capture From Camera")
   {
     try
     {
       _capture = null;
       _capture = new Capture(0);
       _capture.SetCaptureProperty(Emgu.CV.CvEnum.CAP_PROP.CV_CAP_PROP_FPS, 30);
       _capture.SetCaptureProperty(Emgu.CV.CvEnum.CAP_PROP.CV_CAP_PROP_FRAME_HEIGHT, 240);
       _capture.SetCaptureProperty(Emgu.CV.CvEnum.CAP_PROP.CV_CAP_PROP_FRAME_WIDTH, 320);
       Time_Label.Text = "Time: ";
       Codec_lbl.Text = "Codec: ";
       Frame_lbl.Text = "Frame: ";
       webcam_frm_cnt = 0;
       cam = 1;
       Video_seek.Value = 0;
       Application.Idle += ProcessFrame;
       button1.Text = "Stop";
       comboBox1.Enabled = false;
     }
     catch (NullReferenceException excpt)
     {
        MessageBox.Show(excpt.Message);
     }
   }
#endregion cameracapture 

Capture From Video File Code

#region filecapture
   if (comboBox1.Text == "Capture From File")
   {
     openFileDialog1.Filter = "MP4|*.mp4";
     openFileDialog1.FileName = "";
     if (openFileDialog1.ShowDialog() == DialogResult.OK)
     {
      try
      {
        _capture = null;
        _capture = new Capture(openFileDialog1.FileName);
        _capture.SetCaptureProperty(Emgu.CV.CvEnum.CAP_PROP.CV_CAP_PROP_FRAME_HEIGHT, 240);
        _capture.SetCaptureProperty(Emgu.CV.CvEnum.CAP_PROP.CV_CAP_PROP_FRAME_WIDTH, 320);
        FrameRate = _capture.GetCaptureProperty(Emgu.CV.CvEnum.CAP_PROP.CV_CAP_PROP_FPS);
        TotalFrames = _capture.GetCaptureProperty(Emgu.CV.CvEnum.CAP_PROP.CV_CAP_PROP_FRAME_COUNT);
        codec_double = _capture.GetCaptureProperty(Emgu.CV.CvEnum.CAP_PROP.CV_CAP_PROP_FOURCC);
        string s = new string(System.Text.Encoding.UTF8.GetString
        (BitConverter.GetBytes(Convert.ToUInt32(codec_double))).ToCharArray());
        Codec_lbl.Text = "Codec: " + s;
        cam = 0;
        Video_seek.Minimum = 0;
        Video_seek.Maximum = (int)TotalFrames - 1;
        Application.Idle += ProcessFrame;
        button1.Text = "Stop";
        comboBox1.Enabled = false;
      }
      catch (NullReferenceException excpt)
      {
         MessageBox.Show(excpt.Message);
      }
    }
  }
#endregion filecapture 

Processing Images

The following function processes the frame. Process frame extracts some details like frame number, time index, total frames. This function show the image sequences in picture box. Frame gets converted into byte array. That byte array gets converted into hex values of each frame. Then those hex values are stored into the array list for further process. Extract the current frame from video capture, be it device or video file.

frame = _capture.QueryFrame(); 

Frame is converted into bitmap and assigned to picture box to display.

pictureBox1.Image = frame.ToBitmap();  

Function sleeps for a specified time with division of frame rate.

Thread.Sleep((int)(1000.0 / FrameRate)); 

Frame I gets converted into byte array. That byte array gets converted into hex values of each frame. Then those hex values are stored into the array list for further process.

private void ProcessFrame(object sender, EventArgs arg)
{
 try
  {
    Framesno = _capture.GetCaptureProperty(Emgu.CV.CvEnum.CAP_PROP.CV_CAP_PROP_POS_FRAMES);
    frame = _capture.QueryFrame();
    if (frame != null)
    {
        pictureBox1.Image = frame.ToBitmap();
        if (cam == 0)
        {
          Video_seek.Value = (int)(Framesno);
          double time_index = _capture.GetCaptureProperty(Emgu.CV.CvEnum.CAP_PROP.CV_CAP_PROP_POS_MSEC);
          Time_Label.Text = "Time: " + TimeSpan.FromMilliseconds(time_index).ToString().Substring(0, 8);
          double framenumber = _capture.GetCaptureProperty(Emgu.CV.CvEnum.CAP_PROP.CV_CAP_PROP_POS_FRAMES);
          Frame_lbl.Text = "Frame: " + framenumber.ToString();
          Thread.Sleep((int)(1000.0 / FrameRate));
        }
        if (cam == 1)
        {
          Frame_lbl.Text = "Frame: " + (webcam_frm_cnt++).ToString();
        }
     }
   }
   catch (Exception ex)
   {
     MessageBox.Show(ex.Message.ToString());
   }
}  

Release Data

This method releases the data. This also releases the resources acquired by capture variable.

private void ReleaseData()
{
  if (_capture != null)
        _capture.Dispose();
}  

Points of Interest

  • Learn how to configure Visual Studio C#.NET
  • Learn how to set environmental variable
  • Learn how to capture video from web camera
  • Learn how to capture video from video file
  • Learn how to handle events

References

Sorry

Sorry for my English. If you notice errors or can suggest a more correct version, please let me know.

History

  • 7 Feb 2014- First release

License

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

Share

About the Author

SOHAM_GANDHI
Software Developer Opulent Infotech
India India
I am Soham Gandhi from India. I studied BE in Information Technology at University of Pune. (www.unipune.ac.in). I have been learning OpenCV, Enjoying it.
My research interest in Image Processing, Artificial Intelligence, Security Systems, Security Metrics. I work on OpenCV, EmguCV, Visual Studio 2008/10, Java platform.
 
Home Page (Blog)


You Tube Channel
Follow on   Twitter   Google+   LinkedIn

Comments and Discussions

 
Questionwhere the captured video will store ? & GrabProcessState error Pinmembersomisetti subbarao26-Aug-14 23:20 
AnswerRe: where the captured video will store ? & GrabProcessState error PinmemberSOHAM_GANDHI2-Sep-14 0:42 
Questionwhere does it go? PinmemberMember 1096411322-Jul-14 9:46 
AnswerRe: where does it go? PinmemberSOHAM_GANDHI22-Jul-14 20:06 
QuestionError message Pinmemberjose ali6-Jun-14 6:08 
GeneralRe: Error message PinmemberSOHAM_GANDHI6-Jun-14 19:43 
GeneralNice Article / ? on Saving Video PinmemberMember 209684223-Apr-14 3:56 
QuestionThe type initializer for 'Emgu.CV.CvInvoke' threw an exception. PinmemberMember 1061118010-Apr-14 0:58 
AnswerRe: The type initializer for 'Emgu.CV.CvInvoke' threw an exception. PinmemberSOHAM_GANDHI20-Apr-14 4:19 
GeneralRe: The type initializer for 'Emgu.CV.CvInvoke' threw an exception. Pinmemberbartvanderhoeven122-Jul-14 4:54 
GeneralRe: The type initializer for 'Emgu.CV.CvInvoke' threw an exception. PinmemberSOHAM_GANDHI2-Jul-14 19:49 
AnswerRe: The type initializer for 'Emgu.CV.CvInvoke' threw an exception. PinmemberMember 1080717514-Dec-14 8:41 
QuestionFrames AVI c# visual studio PinmemberMember 106327493-Mar-14 8:05 
AnswerRe: Frames AVI c# visual studio PinmemberSOHAM GANDHI4-Mar-14 2:52 
GeneralRe: Frames AVI c# visual studio PinmemberMember 106394374-Mar-14 12:13 
GeneralRe: Frames AVI c# visual studio PinmemberSOHAM GANDHI5-Mar-14 3:42 
GeneralRe: Frames AVI c# visual studio PinmemberSOHAM GANDHI5-Mar-14 19:19 
Questionrstp feed Pinmemberdommy1A10-Feb-14 1:40 
AnswerRe: rstp feed PinmemberSOHAM GANDHI20-Feb-14 7:25 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.141216.1 | Last Updated 7 Feb 2014
Article Copyright 2014 by SOHAM_GANDHI
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid