zoukankan      html  css  js  c++  java
  • 使用 AForge.NET 做视频采集

    AForge.NET 是基于C#设计的,在计算机视觉和人工智能方向拥有很强大功能的框架。btw... it's an open source framework. 附上官网地址: http://www.aforgenet.com/aforge/framework/ 。

    今天要介绍的是AForge中的视频采集功能,这里的视频包括从摄像头等设备的输入和从视频文件的输入。

    首先来认识一下 视频源播放器:VideoSourcePlayer,从摄像头和文件输入的视频,都会通过它来播放,并按帧(Frame)来输出Bitmap数据。

    VideoSourcePlayer 使用有这么几个重要的步骤:

    • 初始化它,设置 VideoSource 属性。VideoSource 接受 IVideoSource 类型的参数,对应到摄像头输入和文件输入,我们分别会把它设置为 VideoCaptureDevice 和 FileVideoSource。
    • 注册 NewFrame 事件,开始播放。在 NewFrame 注册的事件中处理每一帧的Bitmap。
    • 处理完成后,取消 NewFrame 事件注册,停止它。使用 SignalToStop(); and WaitForStop();

    整个使用过程是非常简单的。下面分别来看看摄像头输入和文件输入的代码吧:

     1. 摄像头输入

    首先是初始化和开始:

    // 获取视频输入设备列表
    FilterInfoCollection devices = new FilterInfoCollection(FilterCategory.VideoInputDevice);
    
    // 获取第一个视频设备(示例代码,未对devices个数为0的情况做处理)
    VideoCaptureDevice source = new VideoCaptureDevice(devices[0].MonikerString);
    // 设置Frame 的 size 和 rate
    source.DesiredFrameSize = new Size(640, 360);
    source.DesiredFrameRate = 1;
    
    // 设置VideoSourcePlayer的VideoSource
    VideoSourcePlayer videoPlayer = new VideoSourcePlayer();
    videoPlayer.VideoSource = source;
    
    videoPlayer.NewFrame += videoPlayer_NewFrame;
    
    videoPlayer.Start();

    这里是NewFrame事件代码:

    private void videoPlayer_NewFrame(object sender, ref Bitmap image)
    {
        // do sth with image
        ...
    }

    在使用完成后,停止的代码:

    videoPlayer.NewFrame -= videoPlayer_NewFrame;
    videoPlayer.SignalToStop();
    videoPlayer.WaitForStop();

    2. 文件输入

    首先是初始化和开始:

    // 活体对应视频路径的文件作为视频源
    FileVideoSource videoSource = new FileVideoSource(videoFilePath);
    videoPlayer.VideoSource = videoSource;
    
    videoPlayer.NewFrame += videoPlayer_NewFrame;
    
    videoPlayer.Start();

    其余两部分代码和摄像头输入是一样的,这里就不重复了。

    对于文件输入,还有一点需要注意的,有些机器的codec并不完整,导致FileVideoSource读取某些格式,比如mp4的时候会出现读取错误,这时需要安装一个codec的pack,就可以了。

    好了,AForge.NET 的视频采集功能就介绍完了,接下来会再挑一些AForge中有趣的功能来做介绍。

  • 相关阅读:
    数据结构之队列
    数据结构之循环链表-c语言实现
    数据结构之栈-c语言实现
    数据结构之栈
    vue v-model原理实现
    vue中使用mixins
    async和await
    vue组件中使用watch响应数据
    vue组件中使用<transition></transition>标签过渡动画
    react-motion 动画案例介绍
  • 原文地址:https://www.cnblogs.com/shaomeng/p/4999816.html
Copyright © 2011-2022 走看看