Click here to Skip to main content
15,615,945 members
Articles / Multimedia / Video
Article
Posted 7 Feb 2014

Stats

291.4K views
19.2K downloads
69 bookmarked

Video Capture using OpenCV with C#

Rate me:
Please Sign up or sign in to vote.
4.76/5 (26 votes)
7 Feb 2014CPOL4 min read
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.

Image 1

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”.

Image 2

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

Image 3

Select all check boxes to full package installation.

Step 2: Set Environmental Variable:

Image 4

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. Image 5

  4. 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.

    Image 6

  5. References appear in Solution Explorer.

    Image 7

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'.

C#
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

C#
#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

C#
#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.

C#
frame = _capture.QueryFrame(); 

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

C#
pictureBox1.Image = frame.ToBitmap();  

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

C#
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.

C#
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.

C#
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)


Written By
Software Developer
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

Comments and Discussions

 
QuestionThe similarity of the two photos with opencv in c # Pin
Member 1499381515-Nov-20 20:35
Member 1499381515-Nov-20 20:35 
QuestionEmgu videocapture Pin
Camilo Andres Agudelo15-May-20 18:56
Camilo Andres Agudelo15-May-20 18:56 
Questionkinect? Pin
Ganesh Kums3-Aug-17 7:03
Ganesh Kums3-Aug-17 7:03 
QuestionCAP_PROP ERROR Pin
Member 1221093919-Jul-17 1:33
Member 1221093919-Jul-17 1:33 
PraiseThis Works Fine Pin
Member 1299495211-Feb-17 17:26
Member 1299495211-Feb-17 17:26 
QuestionHow to capture time of a video between start and pause by C# Pin
mehdymahmood4-Nov-16 18:46
mehdymahmood4-Nov-16 18:46 
QuestionSoham thanks for your contributions, one day hope to repay in kind Pin
jai Lopez25-Apr-16 9:41
jai Lopez25-Apr-16 9:41 
PraiseWorks Fine Pin
Member 1183629715-Mar-16 21:32
Member 1183629715-Mar-16 21:32 
QuestionUnable to create capture from file Pin
Member 1157923520-Apr-15 21:55
Member 1157923520-Apr-15 21:55 
AnswerRe: Unable to create capture from file Pin
SOHAM_GANDHI20-Apr-15 22:28
SOHAM_GANDHI20-Apr-15 22:28 
GeneralRe: Unable to create capture from file Pin
Member 1157923521-Apr-15 20:04
Member 1157923521-Apr-15 20:04 
GeneralRe: Unable to create capture from file Pin
SOHAM_GANDHI21-Apr-15 20:26
SOHAM_GANDHI21-Apr-15 20:26 
QuestionThank you Pin
Carlos Flávio Barreto Ferreira de Souza16-Mar-15 8:08
professionalCarlos Flávio Barreto Ferreira de Souza16-Mar-15 8:08 
Questionmemory usage Pin
Çağrı Daşkın8-Feb-15 23:01
Çağrı Daşkın8-Feb-15 23:01 
Questionwhere the captured video will store ? & GrabProcessState error Pin
somisetti subbarao26-Aug-14 22:20
somisetti subbarao26-Aug-14 22:20 
AnswerRe: where the captured video will store ? & GrabProcessState error Pin
SOHAM_GANDHI1-Sep-14 23:42
SOHAM_GANDHI1-Sep-14 23:42 
GeneralRe: where the captured video will store ? & GrabProcessState error Pin
Member 1159885215-Jul-15 9:30
Member 1159885215-Jul-15 9:30 
GeneralRe: where the captured video will store ? & GrabProcessState error Pin
SOHAM_GANDHI21-Dec-15 23:31
SOHAM_GANDHI21-Dec-15 23:31 
GeneralRe: where the captured video will store ? & GrabProcessState error Pin
Aulia Purnama24-Jun-16 9:32
Aulia Purnama24-Jun-16 9:32 
GeneralRe: where the captured video will store ? & GrabProcessState error Pin
Member 1501857017-Feb-21 20:26
Member 1501857017-Feb-21 20:26 
Questionwhere does it go? Pin
Member 1096411322-Jul-14 8:46
Member 1096411322-Jul-14 8:46 
AnswerRe: where does it go? Pin
SOHAM_GANDHI22-Jul-14 19:06
SOHAM_GANDHI22-Jul-14 19:06 
QuestionError message Pin
jose ali6-Jun-14 5:08
jose ali6-Jun-14 5:08 
GeneralRe: Error message Pin
SOHAM_GANDHI6-Jun-14 18:43
SOHAM_GANDHI6-Jun-14 18:43 
GeneralNice Article / ? on Saving Video Pin
Member 209684223-Apr-14 2:56
Member 209684223-Apr-14 2:56 

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

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