1. Background
C# (C Sharp) is a development language on windows. Its advantage is that it is relatively simple and quick to develop window programs.
OpenCV is a commonly used image processing library, but officially supports C++ and Python.
OpenCvSharp is a library that enables OpenCV to support C# development. Based on Visual Studio development.
Follow this article to create an OpenCvSharp hello world.
2. Environment
Win10 + Visual Studio 2019 + OpenCvSharp4
3. Prepare Visual Studio 2019
The download page is https://visualstudio.microsoft.com/vs/ . Community edition, you can use it for free after registering. In addition, we need to use VIsual Studio Installer to install "Universal Windows Platform Development" so that we can use C# to develop desktop programs.
4. Install (Windows) Visual C++ 2019 Redistributable Package
This is because the official statement [1] requires this runtime package.
5. Hello world
5.1 Create a new project and enter the project name.
5.2 Add OpenCvSharp dll to the project
Tools -> NuGet Package Manager -> NuGet Packages for Management Solutions -> Install OpenCvSharp4 and OpenCvSharp4.runtime.win respectively
5.3 Add core code
5.3.1 Double click anywhere in the red box
After double-clicking, the code window will appear.
Type above:
using OpenCvSharp;
In the Form1_Load() function, enter the code
All codes:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using OpenCvSharp;
namespace OpenCVSharp_Test1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
Mat src = new Mat("C:\\Users\\qjfen\\Pictures\\Lenna-Kuurmaa-vanilla-ninja-13314479-1024-768.jpg", ImreadModes.Grayscale);
// Mat src = Cv2.ImRead("lenna.png", ImreadModes.GrayScale);
Mat dst = new Mat();
Cv2.Canny(src, dst, 50, 200);
using (new Window("src image", src))
using (new Window("dst image", dst))
{
Cv2.WaitKey();
}
}
}
}
6 Compile the project and run
6.1 Shortcut keys to compile the project shift + ctrl + B
6.2 Operation
Reference address:
1. OpenCvSharp official github repository, https://github.com/shimat/opencvsharp