OpenCvSharp
.NET wrapper for OpenCV computer vision
OpenCvSharp is an open-source .NET wrapper for the OpenCV computer vision library. It is used to build self-hosted surveillance and video-analysis applications in C# with motion detection and object recognition.
Key features
- OpenCV bindings for .NET
- Image and video analysis
- Motion and object detection
- Cross-platform
Pros & cons
Strengths
- Mature and complete
- Great for custom systems
Trade-offs
- Library, not an app
- Requires development work
OpenCvSharp replaces
Last reviewed Sep 13, 2026 · 768 words
OpenCvSharp will not record your cameras, detect people, or send you a notification. It is a NuGet package that exposes the OpenCV computer-vision library to C#, and it belongs in a self-hosted stack only when you are writing your own program. If what you want is an NVR with object detection, Frigate is the finished product and nothing here competes with it. If what you want is a specific vision task Frigate does not do, counting cars past the gate, reading a water-meter dial, checking whether the garage door is open, and your language is C#, this is the most complete way to build it.
What "wrapper" means in practice
OpenCV is C++. OpenCvSharp maps its API almost one-to-one into .NET classes, so cv::Mat becomes Mat, cv::imread becomes Cv2.ImRead, and a tutorial written for Python or C++ translates line for line. That fidelity is the project's whole value: it has tracked OpenCV since 2008, covers the core, imgproc, video, DNN and feature modules, and is Apache-2.0 so it can ship inside anything. The cost is that it is a thin layer, so you are learning OpenCV, not a friendlier abstraction, and the docs you read will be OpenCV's.
A working motion detector in one file
Install two packages, the bindings and the native runtime for your platform:
dotnet add package OpenCvSharp4
dotnet add package OpenCvSharp4.runtime.win # or the linux runtime package
Then pull frames from an RTSP stream and diff them:
using OpenCvSharp;
using var cap = new VideoCapture("rtsp://user:[email protected]:554/stream1");
using var subtractor = BackgroundSubtractorMOG2.Create(history: 500, varThreshold: 32);
var frame = new Mat(); var mask = new Mat();
while (cap.Read(frame) && frame.Empty() == false)
{
subtractor.Apply(frame, mask);
Cv2.Threshold(mask, mask, 200, 255, ThresholdTypes.Binary);
var moving = Cv2.CountNonZero(mask);
if (moving > frame.Rows * frame.Cols / 100)
Console.WriteLine($"{DateTime.Now:HH:mm:ss} motion: {moving} px");
}
That runs at full frame rate on a single core for a 1080p stream and uses well under the 512 MB the catalogue lists. Swap the background subtractor for Cv2.Dnn.ReadNetFromOnnx and a YOLO model and the same loop does object detection; the DNN module accepts ONNX, and the inference cost is then the model's, not the wrapper's.
Where it sits next to the servers
The clean pattern is to let a real camera stack own the streams and have your program consume a copy. go2rtc or Frigate's built-in restreamer turns one camera connection into as many RTSP consumers as you like, so a .NET service reading from rtsp://go2rtc:8554/driveway does not add a second connection to a camera that can only handle two. Publish results over MQTT and Home Assistant picks them up as a sensor. Your custom detector becomes one more subscriber, not a rival NVR, and everything else in the video surveillance category keeps doing what it does well.
The Linux packaging gotcha
The runtime.win package just works. On Linux the native library expects a set of system dependencies, and the runtime packages target specific distributions. In a container, start from the mcr.microsoft.com/dotnet/runtime image matching your target, install the handful of lib* packages the runtime package's README lists, and test Cv2.GetVersionString() before writing any vision code. Getting a wrong-distro runtime package produces a DllNotFoundException at first call with no further hint, and that one line has cost more people an evening than any OpenCV concept.
Who should not be here
Anyone who does not already write C#. The equivalent Python is pip install opencv-python and the tutorials are Python-first; a .NET wrapper only wins when the rest of your system is .NET. Anyone who wants recording, timelines, clips and a mobile app, which is Frigate or ZoneMinder, both complete applications with thousands of deployments. And anyone hoping for a GUI: there is Cv2.ImShow for debugging on a desktop, and nothing else.
What I'd do
Run Frigate for the cameras, full stop. When a task comes up that Frigate's object detection does not cover, write a small .NET worker with OpenCvSharp that reads a go2rtc restream, does the one job, and publishes to MQTT. Containerise it with the runtime image that matches the package, and keep it stateless so it can restart freely. That division, finished NVR for the 95 percent and a 100-line custom service for the rest, is where this library earns its keep.
Similar video surveillance apps
MediaMTX
Video SurveillanceReady-to-use media server and RTSP/WebRTC proxy
Replaces Wowza
go2rtc
Video SurveillanceUltimate camera streaming gateway with low latency
Replaces Nest Cam
OpenALPR
Video SurveillanceAutomatic license plate recognition library
Replaces Rekor Scout, Plate Recognizer
CompreFace
Video SurveillanceSelf-hosted face recognition service with REST API
Replaces Amazon Rekognition
ZoneMinder
Video SurveillanceFull-featured closed-circuit television software suite
Replaces Blue Iris, Milestone XProtect
Cameradar
Video SurveillanceRTSP stream discovery and access-auditing tool for cameras
Replaces Commercial camera audit tools