Home > Uncategorized > Capture a #webcam image using .NET Core and #OpenCV

Capture a #webcam image using .NET Core and #OpenCV

TL;DR; Here is the public github repo: https://github.com/infiniteloopltd/WebcamDemo

Using OpenCV to capture a webcam in .NET is an easy process, you just need to include two Nuget Packages;

Install-Package Emgu.CV 
Install-Package Emgu.CV.runtime.windows

The windows runtime is required if running this code on Windows, you will need to target another platform, then you could use “Emgu.CV.runtime.ubuntu” for instance.

Here, you can use VideoCapture object to capture your video:

static void Main(string[] args)
{
  var filename = "webcam.jpg";
  if (args.Length > 0) filename = args[0];
  using var capture = new VideoCapture(0, VideoCapture.API.DShow); 
  var image = capture.QueryFrame(); //take a picture
  image.Save(filename);
}

The parameters to the VideoCapture constructor are the video source index (0 being default), and the API being used, here DirectShow worked best for me.

Categories: Uncategorized
  1. No comments yet.
  1. No trackbacks yet.

Leave a comment