Original: C # using OpenCV for video capture (note)
introduction This item is about how to get from a webcam or video file (*. AVI), this project was written in C # and OpenCV. This will help those who prefer C # and OPENCV environments. This program is completely based on visual Studio version C #. NET environment. This program shows how to use C #. NET environment for Visual Studio the IDE to write OPENCV, this program is a how to use Visual Studio 2010,c#. NET Create a program example. In this article, I explained how to configure visual Studio 2010, a step that configures the computer environment variable EmguCV2.4.9 to run the OPENCV program. EMGUCV: Let's get to work ... EMGUCV is a cross-platform shell that runs the OpenCV graphics library. It allows from. NET language such as c#,vb,vc++ called the OPENCV function, which can be compiled with mono and run on Windows,linux,mac OS X,iphone,ipad and Android devices. The EMGUCV is written in C #. Can be compiled in mono, so it can run on any mono supported platform, including linux, Mac and Android. |
pou translated 2 years ago 2 Human top top Good translation! |
Preparing for Visual Studio 2010 1th Step: Install EMGUCV 2.4.9 Download EMGUCV 2.4. Version 9. Install it on the C: \ disk location, do not change the path, use the default path "C:\Emgu\emgucv-windows-universal-gpu2.4.9.1847". Installation path – "C:\Emgu\emgucv-windows-universal-gpu2.4.9.1847". All the boxes are selected and installed all-inclusive. 2nd step: Set Environment variables: Set the following three paths in the user and system variables.
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;
3rd Step: Configure Visual Studio 2010:
Create a new Windows application project named ' both Layer '.
Select Reference, right-click Add Reference.
Select the Browse tab, find "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 OK.
The reference will appear in the Solution Explorer.
|
Leoxu Translated over 2 years ago0 Person Top top translation of good Oh! |
Capturing video Capturing video features, there are two ways to capture video, one is to capture from a camera, and the second is to capture from a video file. In the next section, the code will show you if you capture the video from the camera. In this section, the capture, FRAME PER SECOND is set to 240 FPS, and the video file captures the height and width of the capture to be set to 320, respectively. The Video_seek is then initialized to 0 ' 0 ', and this video search control searches for video between the low and high levels of the video limit. The following statements are most useful in the application. It's a bit like multithreading. When the application enters the idle state, "Processframe" is called until the video frame ends or until the frame is not ' null '.
1 |
Application.Idle += ProcessFrame; |
In the code captured from the video file, we need the total number of frames to set the upper limit of the video search control. FOURCC is used to find the codec name for multimedia. |
Leoxu Translated over 2 years ago0 Person Top top translation of good Oh! |
Code captured from the camera
#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
|
Code captured from a video file
#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
|
Working with Images
The following function is used to process frames. Frame processing can extract some details, such as the number of frames, the timeline, the total number of frames, and so on. This function shows the sequence of images in a picture box. Frames can be converted into byte arrays. This byte data can be converted to 16 binary values for each frame. Then those hexadecimal values are stored in the array for further processing. Extracts the current frame from the video captured by the device or video file.
frame = _capture.QueryFrame(); |
Frames are converted to bitmap and assigned to the picture box for display.
pictureBox1.Image = frame.ToBitmap(); |
The function sleeps at a specific time divided by the frame rate.
?
1 |
Thread.Sleep(( int )(1000.0 / FrameRate)); |
The frame is converted into a byte array. this byte data is converted to 16 binary values per frame . Then those hexadecimal values are stored in the array for further processing .
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 is used to release data. It also frees up some of the resources needed to capture variables.
private void ReleaseData() { if (_capture != null ) _capture.Dispose(); } |
C # using OpenCV for video capture (note)