OpenCVSharp:无法加载 DLL 'OpenCvSharpExtern'

OpenCVSharp: Unable to load DLL 'OpenCvSharpExtern'

我已经在我的项目中使用 NuGet 管理器安装了最新的 OpenCVSharp 2 (2.4.10.201...)。有关 OpenCVSharp.CvMat 的一切工作正常(加载、操作等)所以我确信安装是正确的。

然而我根本无法使用OpenCVSharp.CPlusPlus! 问题是它没有加载 OpenCvSharpExtern.dll。

try-catch 块中显示的错误:

try
{
    Mat mat = new Mat();
} catch (Exception err)
{
    Console.WriteLine( err );
}

System.TypeInitializationException: The type initializer for 'OpenCvSharp.CPlusPlus.Mat' threw an exception. ---> System.DllNotFoundException: Unable to load DLL 'OpenCvSharpExtern': The specified module could not be found. (Exception from HRESULT: 0x8007007E)
   at OpenCvSharp.CPlusPlus.NativeMethods.core_Mat_sizeof()
   at OpenCvSharp.CPlusPlus.Mat..cctor()
   --- End of inner exception stack trace ---
   at OpenCvSharp.CPlusPlus.Mat..ctor()
   at MainClass.Main() in C:\Users\kotsias\Documents\Visual Studio 2015\Projects\MyCVSharp\ConsoleApplication1\Test.cs:line 14

OpenCvSharpExtern.dllIS与我的.exe一起部署在下面子文件夹:

..\dll\x86

&

..\dll\x64

如果我手动将 x64 .dll 复制到与我的 .exe 相同的文件夹中,我仍然会遇到上述相同的错误。如果我复制 x86 .dll,我会得到一个不同的错误:

System.TypeInitializationException: The type initializer for 'OpenCvSharp.CPlusPlus.Mat' threw an exception. ---> System.BadImageFormatException: An attempt was made to load a program with an incorrect format. (Exception from HRESULT: 0x8007000B)
   at OpenCvSharp.CPlusPlus.NativeMethods.core_Mat_sizeof()
   at OpenCvSharp.CPlusPlus.Mat..cctor()
   --- End of inner exception stack trace ---
   at OpenCvSharp.CPlusPlus.Mat..ctor()
   at MainClass.Main() in C:\Users\kotsias\Documents\Visual Studio 2015\Projects\MyCVSharp\ConsoleApplication1\Test.cs:line 14

可能您没有安装正确版本的 Visual C++ Redistributable 包。

OpenCvSharp 2.4.10 使用 Visual C++ 2013 Redistributable Package。但是,OpenCvSharp 2.4.10 is deprecated and author recommends to use OpenCvSharp 3.2:

#OpenCvSharp 2.4.10 Cross platform wrapper of OpenCV 2.4.10 for .NET Framework.

This project is deprecated. The latest release is available in OpenCvSharp.

更多信息请查看:

在包管理器上使用此代码:

Install-Package OpenCvSharp4.Windows -Version 4.0.0.20190108

安装“OpencvSharp{version} nuget”包还不够。 您还必须安装“OpencvSharp{version}.runtime.{operating_system}”包。

例如,在我的例子中,我必须安装以下 Nuget 包:

  • OpencvSharp4
  • OpencvSharp4.runtime.win

有些人可能遇到的另一个问题:

OpenCvSharpExtern.dll 需要作为媒体功能包一部分的 mfplat.dll。 Windows 'N' 版本默认不包含此项。

可以从应用和功能->可选功能->添加功能安装。