【问题标题】:Setting up OpenCvSharp with Visual Studio 2019使用 Visual Studio 2019 设置 OpenCvSharp
【发布时间】:2021-02-01 12:13:37
【问题描述】:

我正在尝试设置 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 无法识别定义?

【问题讨论】:

  • 最好复制粘贴代码段并翻译 i18n 的错误消息。

标签: c# .net windows opencv visual-studio-2019


【解决方案1】:

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();
                }
            }
        }
    }
}

【讨论】:

  • 我们正在使用这段代码。我把它归结为 3 个错误,即“Mat”没有“Zero”、“WidthStep”或“ImageData”的定义。这些是否与不正确的 OpenCV 安装有关?我在这里没有使用 NuGet,只是 OpenCV 和 OpenCvSharp 的 SourceForge 档案
  • 可能是。不同的版本可能会导致它。我对OpenCV CSharp 包装器不太熟悉。正如我所说,函数名和类名必须有一些东西。我用 opencv 3.4.1 找到了一些 samples
  • @AndriiKozytskyi 您使用的是哪个 opencv 版本?你能检查一下例子吗?
【解决方案2】:

多亏了 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 异常。现在就像一个魅力。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-01-09
    • 1970-01-01
    • 2022-08-03
    • 1970-01-01
    • 2020-12-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多