简体   繁体   English

如何在c#中从网络摄像头抓取恒定的位图图像流

[英]How to grab constant stream of bitmap images from webcam in c#

We have ac# application that performs processing on video streams. 我们有ac#应用程序,可以对视频流进行处理。 This is a low-level application that receives each frame in Bitmap format, so basically we need 25 images each second. 这是一个以Bitmap格式接收每个帧的低级应用程序,所以基本上我们每秒需要25个图像。 This application is already working for some of our media sources, but we now need to add a webcam as an input device. 此应用程序已经适用于我们的一些媒体源,但我们现在需要添加网络摄像头作为输入设备。

So we basically need to capture bitmap images from a webcam continuously so that we can pass all these frames as a "stream" to our application. 所以我们基本上需要连续捕获来自网络摄像头的位图图像,以便我们可以将所有这些帧作为“流”传递给我们的应用程序。

What is the best and simplest way to access the webcam and read the actual frames directly from the webcam as individual images? 访问网络摄像头的最佳和最简单的方法是什么?直接从网络摄像头读取实际帧作为单独的图像? I am still in the starting blocks. 我还在起跑线上。

There are a multitude of libraries out there that allows one to access the webcam, preview the content of the webcam on a windows panel and then use screen capturing to capture this image again. 有许多库允许人们访问网络摄像头,在Windows面板上预览网络摄像头的内容,然后使用屏幕捕获再次捕获此图像。 This, unfortunately, will not give us the necessary performance when capturing 25 frames per second. 遗憾的是,在每秒捕获25帧时,这不会给我们提供必要的性能。 IVMRWindowlessControl9::GetCurrentImage has been mentioned as another alternative, but this again seems to be aimed at an infrequent snapshot rather than a constant stream of images. IVMRWindowlessControl9 :: GetCurrentImage已被提及作为另一种选择,但这似乎再次针对的是不常见的快照而不是恒定的图像流。 Directshow.Net is mentioned by many as a good candidate, but it is unclear how to simply grab the images from the webcam. Directshow.Net被许多人称为一个很好的候选人,但目前还不清楚如何简单地从网络摄像头抓取图像。 Also, many sources state a concern about Microsoft no longer supporting Directshow. 此外,许多消息来源表示担心微软不再支持Directshow。 Also, implementations I've seen of this requires ImageGrabber which is apparently also no longer supported . 此外,我已经看到的实现需要ImageGrabber,显然也不再支持 The newer alternative from MS seems to be Media Foundation, but my research hasn't turned up any working examples of how this can be implemented (and I'm not sure if this will run on older versions of windows such as XP). MS的新选择似乎是媒体基金会,但我的研究还没有找到任何可以实现的实例(我不确定这是否会在XP等旧版本的Windows上运行)。 DirectX.Capture is an awesome library ( see a nice implementation ) but seems to lack the filters and methods to get the video images directly. DirectX.Capture是一个很棒的库( 参见一个不错的实现 )但似乎缺少直接获取视频图像的过滤器和方法。 I have also started looking at Filters and Filter Graphs but this seems awfully complex and does feel a bit like "reinventing the wheel". 我也开始研究滤镜和滤镜图,但这看起来非常复杂,感觉有点像“重新发明轮子”。

Overall, all the solutions briefly mentioned above seem to rather old. 总的来说,上面简要提到的所有解决方案似乎都相当陈旧。 Can someone please point me in the direction of a step-by-step guide for getting a webcam working in C# and grabbing several images per second from it? 有人可以指点我一步一步的指导,让网络摄像头在C#中工作,并从中每秒抓取几张图像? (We will also have to do audio at some point, so a solution that does not exclude video would be most helpful). (我们还必须在某些时候做音频,因此不排除视频的解决方案将是最有帮助的)。

I use AForge.Video (find it here: code.google.com/p/aforge/) because it's a very fast c# implementation. 我使用AForge.Video(在这里找到它:code.google.com/p/aforge/)因为它是一个非常快速的c#实现。 i am very pleased with the performance and it effortlessly captures from two HD webcams at 30fps on an 8 year old PC. 我对它的表现感到非常满意,它可以在一台8岁的PC上以30fps的速度轻松捕获两个高清网络摄像头。 the data is supplied as a native IntPtr so it's ideal for further processing using native code or opencv. 数据作为本机IntPtr提供,因此它非常适合使用本机代码或opencv进行进一步处理。

opencv wrappers emgu and opencvsharp both implement a rudimentary video capture functionality which might be sufficient for your purposes. opencv包装器emgu和opencvsharp都实现了基本的视频捕获功能,这可能足以满足您的需要。 clearly if you are going perform image processing / computer vision you might want to use those anyway. 很明显,如果你要进行图像处理/计算机视觉,你可能还是想要使用它们。

As dr.mo suggests, Aforge was the answer. 正如dr.mo建议的那样,Aforge就是答案。

I used the tutorial from here: http://en.code-bude.net/2013/01/02/how-to-easily-record-from-a-webcam-in-c/ 我从这里使用了教程: http//en.code-bude.net/2013/01/02/how-to-easily-record-from-a-webcam-in-c/

In the tutorial, they have an event handler fire each time a frame is received from the webcam. 在本教程中,每次从网络摄像头收到帧时,它们都会触发事件处理程序。 In the original tutorial, this bitmap is used to write the image to a PictureBox. 在原始教程中,此位图用于将图像写入PictureBox。 I have simply modified it to save the bitmap image to a file rather than to a picturebox. 我只是修改它以将位图图像保存到文件而不是图片框。 So I have replaced the following code: 所以我已经替换了以下代码:

pictureBoxVideo.BackgroundImage = (Bitmap)eventArgs.Frame.Clone();

with the following code: 使用以下代码:

Bitmap myImage = (Bitmap)eventArgs.Frame.Clone();
string strGrabFileName = String.Format("C:\\My_folder\\Snapshot_{0:yyyyMMdd_hhmmss.fff}.bmp", DateTime.Now);            
myImage.Save(strGrabFileName, System.Drawing.Imaging.ImageFormat.Bmp);

and it works like a charm! 它就像一个魅力!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM