zoukankan      html  css  js  c++  java
  • [转] c# 中使用opencv进行视频捕获

    简介

    这个项目是关于如何从网络摄像头或者视频文件(*.AVI)中捕获视频的,这个项目是用C#和OPENCV编写的。

    这将有助于那些喜欢C#和OpenCV环境的人。这个程序完全基于Visual Studio 2010 version C#.NET环境。这个程序展示了怎样用C#.NET环境的Visual Studio 2010 IDE编写OpenCV,这个程序是一个怎样用Visual Studio 2010,C#.NET创建程序的例子。

    在这篇文章中,我解释了怎样配置Visual Studio 2010,一种配置计算机环境变量EmguCV2.4.9以便运行OpenCV程序的步骤。

    EmguCV:让我们开始工作吧。。。

    EmguCV 是一个跨平台的运行OpenCV图形库的壳。它允许从.NET语言例如C#,VB,VC++中调用OpenCV函数,这个壳可以用Mono编译,在Windows,Linux,Mac OS X,iPhone,iPad 和Android 设备中运行。

    EmguCV是用C#编写的。可以在Mono中编译,所以它可以在任何Mono支持的平台上运行,包括 Linux, Mac and Android。

    pou
    pou
    翻译于 2年前

    2人顶

     翻译的不错哦!

    准备 Visual Studio 2010

    第 1 步: 安装 EmguCV 2.4.9

    下载 EmguCV 2.4.9 版本. 将它安装在 c: 磁盘 位置, 不要改变路径, 使用默认的路径“C:Emguemgucv-windows-universal-gpu2.4.9.1847”.

    安装路径 – “C:Emguemgucv-windows-universal-gpu2.4.9.1847”.

    所有的框框都选上,全包安装.

    第 2 步: 设置环境变量:

    在用户和系统变量中设置如下的三个路径.

    • C:Emguemgucv-windows-universal-gpu 2.4.9.1847in;

    • C:Emguemgucv-windows-universal-gpu 2.4.9.1847inx64;

    • C:Emguemgucv-windows-universal-gpu 2.4.9.1847inx86;

    第 3 步: 配置 Visual Studio 2010:

    1. 创建一个新的 Windows Application 项目,命名为‘Two Layer’.

    2. 选择引用, 右键点击添加引用.

    3. 选择 Browse 选项卡, 找到“C:Emguemgucv-windows-universal-gpu2.4.9.1847in”, 选择“Emgu.CV.dll”,”Emgu.CV.UI.dll”,”Emgu.Util.dll” 三个文件, 并点击ok.

    4. 引用就会在Solution Explorer中出现.

    leoxu
    leoxu
    翻译于 2年前

    0人顶

     翻译的不错哦!

    捕获视频

    捕获视频功能,捕获视频有两种方式,一种是从摄像机捕获,第二种是从视频文件捕获。接下来的部分,代码会向你展示如果从摄像机捕获视屏.

    在这一节中, 捕获, FRAME PER SECOND 设置为 30 FPS, 视频文件捕获的高和宽分别设置为 240, 320 . 然后video_seek 被初始化为零 '0', 这个视频搜寻控制会在视频限制的低位和高位之间搜索视频.

    下面的语句在应用程序中最有用. 它有点像多线程. 当应用程序进入到空闲状态,"ProcessFrame" 就会一直调用直到视频帧结束或者直到帧不为 'null'.

     
    1
    Application.Idle += ProcessFrame;

    在从视频文件捕获的代码中,我们需要总帧数来设置视频搜寻控制的上限. FOURCC 被用来找到多媒体的编码解码器名称.

    leoxu
    leoxu
    翻译于 2年前

    0人顶

     翻译的不错哦!

    从摄像机捕获的代码

     
    #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

    从视频文件捕获的代码

     
    #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

    处理图像

    下面的函数用来处理帧. 帧处理可以提取出一些详细信息,如帧的编号,时间轴,总的帧数等. 这个函数展示了图片盒子中的图像序列. 帧可以被转换成字节数组. 这个字节数据可以被转换成每一帧的16进制值. 然后那些十六进制值被存到数组中做进一步的处理. 从设备或者视频文件捕获的视频中提取当前帧.

     
    frame = _capture.QueryFrame();

    帧被转换成 bitmap 并被赋值给图片盒子用于展示 .  

     
    pictureBox1.Image = frame.ToBitmap();

    函数在按帧率划分的特定时间休眠.  

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

    帧被转换成字节数组. 这个字节数据被转换成每一帧的16进制值. 然后那些十六进制值被存到数组中做进一步的处理.

     
    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());
       }
    }

    释放数据

    这个方法用来释放数据. 它同时也释放了捕获变量所需要的一些资源.

     

    private void ReleaseData()
    {
      if (_capture != null)
            _capture.Dispose();
    }
  • 相关阅读:
    IT项目管理之系统规划
    项目经理感悟之风险管理
    IT项目管理之系统设计
    IT项目管理之系统验收
    IT项目管理之系统测试
    IT项目管理之系统部署
    大中小型项目管理的区别
    算法评测
    IT项目管理那些事儿
    应用算法的实际情况——简单就是美
  • 原文地址:https://www.cnblogs.com/vice/p/8921850.html
Copyright © 2011-2022 走看看