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:
- Create new Windows Application project named ‘Two Layer’.
- Select Reference, right click, click Add References.
- 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.

- 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