"Emgu.CV.CvInvoke”的类型初始值设定项引发异常 解决办法

 

 

系统win7 32位,只在这一台电脑上出现这种问题,已知VS编译是X86,在数台电脑上测试都正常。

 

后来把opencv的dll路径例如 E:\...\x86  加入到系统环境变量中就正常了。

 

emgucv安装里opencv的dll路径是这个样子 C:\Emgu\emgucv-windows-universal 3.0.0.2157\bin\x86,发布时把bin下的dll包括x86等拷贝到程序安装目录下。

 

但很奇怪这一台电脑无法使用,理论上emgucv发布不需要环境变量的。

还是要继续研究,欢迎知道的解答下。

 

 

 

官方解释:

The type initializer for 'Emgu.CV.CvInvoke' threw an exception.

If you see this exception, please check the following

Have you installed MSVCRT?
  • For Version 3.0+, the required vcrt dlls are included in the "x86" and "x64" folder. You will be ready as long as you copy all the unmanaged dlls in the "x86" and "x64" folder to the folder of executable.
  • For Version 2.4+, the bundled OpenCV binary is build with Visual Studio 2010, you will needs to installed MSVCRT 10.0 SP1 x86 or MSVCRT 10.0 SP1 x64 to resolve the dependency issue.
  • For Version 2.0+, the bundled OpenCV binary is build with Visual Studio 2008, you will needs to installed MSVCRT 9.0 SP1 to resolve the dependency issue.
  • For Version 1.5, the bundled OpenCV pre1.1 binary is build with Visual Studio 2005, you will needs to installed MSVCRT 8.0 SP1 to resolve the dependency issue.
Have you copied the OpenCV dlls to the execution directory?
  • Make sure the unmanaged DLLs are in the execution directory.
    • For Emgu CV version >=2.4.2, this means the "x86" and "x64" folder and all the dlls within the folders. The folder names and file structures should not be altered when deploying with the application.
    • For EMGU CV version 2.4 cudart64_42_9.dll, cvextern.dll, npp64_42_9.dll, opencv_calib3dXXX.dll, opencv_contribXXX.dll, opencv_coreXXX.dll, opencv_features2dXXX.dll, opencv_flannXXX.dll, opencv_highguiXXX.dll, opencv_imgprocXXX.dll, opencv_legacyXXX.dll, opencv_mlXXX.dll, opencv_nonfreXXX.dll, opencv_objectdetectXXX.dll, opencv_videoXXX.dll,</code?> where <code>XXX is the OpenCV version number.
    • For Emgu CV version 2.2, 2.3 this means the following dlls: opencv_calib3dXXX.dll, opencv_contribXXX.dll, opencv_coreXXX.dll, opencv_features2dXXX.dll, opencv_highguiXXX.dll, opencv_imgprocXXX.dll, opencv_legacyXXX.dll, opencv_mlXXX.dll, opencv_objectdetectXXX.dll, opencv_videoXXX.dll where XXX is the OpenCV version number.
    • For Emgu CV version <= 2.1, this means the following dlls:cvXXX.dll, cvauxXXX.dll, cxcoreXXX.dll, highguiXXX.dll, opencv_ffmpegXXX.dll, mlXXX.dllcvextern.dll where XXX is the OpenCV version number.
  • The best way to set up your project is:
    • Copy the unmanaged DLLs to your project folder
    • Right click on the project, click Add->Existing Item and select all unmanaged DLLs. Add them to the project.
    • For each of the included Dlls, left click on it, find the "Copy to Output Directory" option and select "Copy if newer"
Are you missing any dependency?

Download Dependency Walker and use it to open the "cvextern.dll" file. Check if any dependency is missing.

I have checked all of above but I still got the Exception

In this case, please try to build and run the examples. After building the examples, try to run the "Hello World" Program.

If "Hello World" runs without any problem, compare it with you project, find the difference in configuration and fix it.

If "Hello World" get the same "The type initializer for 'Emgu.CV.CvInvoke' threw an exception." message, try to find out the InnerException and report it to the discussion forum

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
这个问题通常是由于Emgu.CV库在加载C++库时发生的错误导致的。为了解决这个问题,你可以尝试以下步骤: 1.确保你已经按照Emgu.CV的安装指南进行了安装,并且已经添加了所需的引用。 2.在你的目中添加以下NativeLibrary.SetDllImportResolver代码,该代码会解决安卓目下无法找到C++库的问题。 ```csharp using System.Runtime.InteropServices; using Emgu.CV; using NativeLibrary = System.Runtime.InteropServices.NativeLibrary; public static class Bootstrapper { public static void Init() { NativeLibrary.SetDllImportResolver(typeof(CvInvoke).Assembly, DllImportResolver); } private static IntPtr DllImportResolver(string libraryName, Assembly assembly, DllImportSearchPath? searchPath) { if (libraryName == "libgdiplus") { return LoadLibgdiplus(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "libgdiplus.so")); } return IntPtr.Zero; } [DllImport("libdl.so")] private static extern IntPtr dlopen(string filename, int flags); [DllImport("libdl.so")] private static extern IntPtr dlerror(); private static IntPtr LoadLibgdiplus(string path) { IntPtr lib = dlopen(path, RTLD_NOW); if (lib == IntPtr.Zero) { var error = Marshal.PtrToStringAnsi(dlerror()); throw new Exception($"Failed to load libgdiplus from '{path}'. {error}"); } return lib; } private const int RTLD_NOW = 2; } ``` 3.在你的Android目的MainActivity.cs文件中添加以下代码,用于在应用程序启动时初始化Emgu.CV。 ```csharp protected override void OnCreate(Bundle savedInstanceState) { base.OnCreate(savedInstanceState); Bootstrapper.Init(); // Initialize Emgu.CV // Other initialization code } ``` 通过这些步骤,你应该可以解决System.TypeInitializationException: The type initializer for 'Emgu.CV.CvInvoke' threw an exception问题。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值