使用 Visual Studio 2019 设置 OpenCvSharp

Setting up OpenCvSharp with Visual Studio 2019

我正在尝试设置 OpenCvSharp,但到目前为止,尽管遵循了文档,但我什至无法成功编译基本代码示例。我遵循了从 OpenCvSharp wiki Windows tutorial 到发球台的说明。

我从 SourceForge 获取了最新的 OpenCV 二进制文件并将文件夹添加到我的环境变量和路径中:

接下来,我下载了 OpenCvSharp DLL 文件并添加了一个 OpenCvSharp.dll 对我的示例项目的引用:

以及将 OpenCvSharpExtern.dll 添加到 EXE 输出目录,但这没有帮助,因为我无法首先编译可执行文件。

我刚刚从 wiki 复制了示例 Program.cs 代码,

using System;
using System.Runtime.InteropServices;
using OpenCvSharp;

namespace WindowsFormsApplication1
{
    static class Program
    {
        static void Main()
        {
            using (IplImage image = new IplImage(128, 128, BitDepth.U8, 1)) 
            {
                image.Zero();
                for (int x = 0; x < image.Width; x++) 
                {
                    for (int y = 0; y < image.Height; y++) 
                    {
                        int offset = y * image.WidthStep + x;
                        byte value = (byte)(x + y);
                        Marshal.WriteByte(image.ImageData, offset, value);
                    }
                }
                using (CvWindow window = new CvWindow(image))
                {
                    CvWindow.WaitKey();
                }
            }
        }
    }
}

但是当我尝试编译它时,我得到了 OpenCvSharp 函数和类型的错误:

  CS0246  The type or namespace "IplImage" was not found
  CS0246  The type or namespace "IplImage" was not found
  CS0103  The name "BitDepth" is not defined in current context
  CS0246  The type or namespace "CvWindow" was not found
  CS0246  The type or namespace "CvWindow" was not found
  CS0103  The name "CvWindow" is not defined in current context

引用已经到位并且包包含在 using 中,那么为什么 Visual Studio 无法识别定义?

IplImage 是用于存储来自旧 OpenCV1.x 界面的图像数据的原始格式。 Mat 是 OpenCV2.x 版本及以后的新格式。另外,CvWindow应该改为。由于您使用的是最新版本,因此需要更新代码或 NuGet 包。更新后的代码如下:

using System.Runtime.InteropServices;
using OpenCvSharp;

namespace WindowsFormsApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            using (var image  = new Mat(new Size(128, 128), MatType.CV_8U, Scalar.All(255)))
            {
                for (int x = 0; x < image.Width; x++) 
                {
                    for (int y = 0; y < image.Height; y++) 
                    {
                        int offset = y * image.Width + x;
                        byte value = (byte)(x + y);
                        Marshal.WriteByte(image.Data, offset, value);
                    }
                }

                using (var window = new Window("window", image: image, flags: WindowMode.AutoSize))
                {
                    Cv2.WaitKey();
                }
            }
        }
    }
}

感谢 Ceopee 的建议,成功了。根据 this guide 使用 OpenCV 重新安装并测试了示例 C++ 项目以确保其正常工作,然后使用以下代码编译并启动它:

using System.Runtime.InteropServices;
using OpenCvSharp;

namespace WindowsFormsApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            using (var image = new Mat(128, 128, MatType.CV_8U, Scalar.All(255)))
            {
                int w = image.Width; int h = image.Height;
                int WidthStep = w / image.Cols;
                for (int x = 0; x < w; x++)
                {
                    for (int y = 0; y < h; y++)
                    {
                        int offset = y * w + x;
                        byte value = (byte)(x + y);
                        Marshal.WriteByte(image.Data, offset, value);
                    }
                }

                using (var window = new Window("window", image: image, flags: WindowMode.AutoSize))
                {
                    Cv2.WaitKey();
                }
            }
        }
    }
}

故事的寓意是使用正确的格式(阅读文档!)并确保创建新的 CPU 配置并将平台设置为 x64 而不是“任何 CPU”,就像使用 C++ OpenCV 项目一样

否则你会得到一个 BadImageFormat 异常。现在效果很好。