星空网 > 软件开发 > ASP.net

使用 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 和 ratesource.DesiredFrameSize = new Size(640, 360);source.DesiredFrameRate = 1;// 设置VideoSourcePlayer的VideoSourceVideoSourcePlayer 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中有趣的功能来做介绍。

 




原标题:使用 AForge.NET 做视频采集

关键词:.NET

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

跨境小苏:https://www.goluckyvip.com/tag/36930.html
跨境小兔:https://www.goluckyvip.com/tag/36931.html
跨境小侠醉千丝:https://www.goluckyvip.com/tag/36932.html
跨境小仙女:https://www.goluckyvip.com/tag/36933.html
跨境小新手:https://www.goluckyvip.com/tag/36934.html
跨境小学生:https://www.goluckyvip.com/tag/36935.html
大连有啥好玩的景点-大连有啥好玩的景点推荐孩子:https://www.vstour.cn/a/411235.html
咸阳机场到临潼景点 西安咸阳机场到临潼有多远:https://www.vstour.cn/a/411236.html
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流