c# 调用opencv
时间: 2023-07-04 08:26:20 浏览: 135
要在 C# 中调用 OpenCV,可以使用 Emgu CV 这个开源库。Emgu CV 是一个基于 OpenCV 的 .NET 接口,可以让 C# 开发者方便地使用 OpenCV。
以下是一些步骤:
1. 下载并安装 Emgu CV。
2. 在 Visual Studio 中创建一个新的 C# 项目。
3. 在项目中添加对 Emgu.CV 和 Emgu.CV.UI 的引用。
4. 编写 C# 代码,并使用 Emgu.CV 命名空间中的类来调用 OpenCV。
下面是一个简单的示例代码,演示了如何在 C# 中使用 Emgu CV 调用 OpenCV 的 cv::threshold 函数进行图像二值化:
```csharp
using Emgu.CV;
using Emgu.CV.Structure;
// Load an image
Image<Bgr, byte> img = new Image<Bgr, byte>("test.jpg");
// Convert the image to grayscale
Image<Gray, byte> gray = img.Convert<Gray, byte>();
// Apply thresholding
CvInvoke.Threshold(gray, gray, 128, 255, ThresholdType.Binary);
// Display the result
ImageViewer.Show(gray, "Result");
```
在这个示例中,我们首先加载了一张图片,然后将其转换为灰度图像。接着,我们调用了 cv::threshold 函数进行二值化,并将结果显示在一个窗口中。
需要注意的是,Emgu CV 的 API 与 OpenCV 的 API 并不完全一致,因此在使用 Emgu CV 时需要查阅其文档来了解具体的用法。
阅读全文
相关推荐

















