C# “Emgu.CV.CvInvoke”的类型初始值设定项引发异常
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17274372/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
The type initializer for 'Emgu.CV.CvInvoke' threw an exception
提问by Dilshan Zuhdi
I'm getting this error
我收到这个错误
The type initializer for 'Emgu.CV.CvInvoke' threw an exception.
'Emgu.CV.CvInvoke' 的类型初始值设定项抛出异常。
when I try to use Emgu CV. I've tried everything I can think of to fix this but it's still giving the same error, and when I click a button it shows
当我尝试使用 Emgu CV 时。我已经尝试了所有我能想到的方法来解决这个问题,但它仍然给出同样的错误,当我点击一个按钮时它会显示
Object reference not set to an instance of an object.
你调用的对象是空的。
This is the code I'm trying:
这是我正在尝试的代码:
void ProcessFunction(object sender, EventArgs e)
{
imgOrg = capturecam.QueryFrame();
if (imgOrg == null) return;
imgProc = imgOrg.InRange(new Bgr(50, 50, 50), new Bgr(255, 255, 255));
imgProc = imgProc.SmoothGaussian(9);
imageBox1.Image = imgOrg;
imageBox2.Image = imgProc;
}
What might I have done wrong and how can I debug this further? Thanks!
我可能做错了什么,我该如何进一步调试?谢谢!
回答by jlew
It's probably failing to load the unmanaged DLL dependencies. A couple of suggestoins:
可能无法加载非托管 DLL 依赖项。几个建议:
- Make sure your .NET project is targeting x86 rather than AnyCPU, since you likely have the x86 build of OpenCV.
- Make sure the OpenCV dlls are in your PATH (or directly in the bin directory, if this is a Console/WinForms/WPF app.)
- 确保您的 .NET 项目面向 x86 而不是 AnyCPU,因为您可能拥有 OpenCV 的 x86 版本。
- 确保 OpenCV dll 在您的 PATH 中(或者直接在 bin 目录中,如果这是一个 Console/WinForms/WPF 应用程序。)
回答by Subodha Charles
Copy and paste all the unmanaged dlls in the bin folder of your EMGU installation to where your exe file is (Release or Debug folder of your project folder).
In my case, unmanaged dlls are at "C:\Emgu\emgucv-windows-universal-cuda 2.9.0.1922\bin\x86"
将 EMGU 安装的 bin 文件夹中的所有非托管 dll 复制并粘贴到 exe 文件所在的位置(项目文件夹的 Release 或 Debug 文件夹)。
就我而言,非托管 dll 位于“C:\Emgu\emgucv-windows-universal-cuda 2.9.0.1922\bin\x86”
回答by keyvan moazami
you should add dependency dll into project and change 'copy to output directory' property to 'copy always' (add -> existing item) from Emgu\emgucv-windows-universal-cuda 2.9.0.1922\bin\x86
您应该将依赖项 dll 添加到项目中,并将Emgu\emgucv-windows-universal-cuda 2.9.0.1922\bin\x86 中的“复制到输出目录”属性更改为“始终复制”(添加 -> 现有项目)
in my project add : opencv_core290.dll , opencv_highgui290.dll , opencv_ffmpeg290.dll , opencv_imageproc290.dll and cudart32_55.dll
在我的项目中添加: opencv_core290.dll 、 opencv_highgui290.dll 、 opencv_ffmpeg290.dll 、 opencv_imageproc290.dll 和cudart32_55.dll
回答by Muhammed Tanr?verdi
I had the same problem. My inner exception was "Unable to load DLL 'opencv_core290'".
我有同样的问题。我的内部异常是“无法加载 DLL 'opencv_core290'”。
You need to copy x86 and x64 folders from "yourEmguFolder/bin" to your project's output(bin) directory. Then build project again and run.
您需要将 x86 和 x64 文件夹从“yourEmguFolder/bin”复制到项目的 output(bin) 目录。然后再次构建项目并运行。
According to emgu wiki
回答by Mohamed Masrooj
First you have to add three libraries to your project. opencv_core290.dll,opencv_highgui290.dll,opencv_imgproc290.dll. In here 290 refers that your opencv version. After that change in their property "Do not copy" to "copy always". than save your project. After that go to following location. If your machine is 64 bit than go to following folder "C:\Emgu\emgucv-windows-universal-cuda 2.9.0.1922\bin\x64" or if your using 32 bit os than go to "C:\Emgu\emgucv-windows-universal-cuda 2.9.0.1922\bin\x86" and copy all the dlls in that folder and paste that into your Your Project/bin/debug/ folder. than rebuild your project. Than it will works fine.
首先,您必须将三个库添加到您的项目中。opencv_core290.dll、opencv_highgui290.dll、opencv_imgproc290.dll。在这里 290 是指您的 opencv 版本。之后将他们的属性“不要复制”更改为“始终复制”。而不是保存您的项目。之后前往以下位置。如果您的机器是 64 位,请转到以下文件夹“C:\Emgu\emgucv-windows-universal-cuda 2.9.0.1922\bin\x64”,或者如果您使用的是 32 位操作系统,请转到“C:\Emgu\emgucv- windows-universal-cuda 2.9.0.1922\bin\x86”并复制该文件夹中的所有dll并将其粘贴到您的项目/bin/debug/文件夹中。而不是重建你的项目。比它会正常工作。
Best of luck!!!!
祝你好运!!!!
回答by Mannu
I Had the same problem in my 32 bit machine for 3 days. I resolved this by simply copying all dlls into system32 folder from the bin and x86 folders and it worked. It looks like there are dlls that depend on other dlls and the moment you transfer all it works. I did not have time to find out which one.
我在我的 32 位机器上遇到了同样的问题 3 天。我通过简单地将所有 dll 从 bin 和 x86 文件夹复制到 system32 文件夹来解决这个问题,并且它工作正常。看起来有些 dll 依赖于其他 dll,并且在您传输所有文件的那一刻它就起作用了。我没有时间找出是哪一个。
回答by tamsiv
For me problem was solved by clicking in Visual Studio:
对我来说,问题是通过单击 Visual Studio 解决的:
Menu -> Project -> Properties -> Build -> Prefer 32-bit.
菜单 -> 项目 -> 属性 -> 构建 -> 首选 32 位。
It must have been a problem with emgu (probably x86) and my OS x64).
emgu(可能是 x86)和我的操作系统 x64 一定有问题。
回答by mark deacon
Found this on c#,VS 2017, emgu version 3.2.0.2682
在 c#,VS 2017, emgu version 3.2.0.2682 上找到这个
Tried a simple build on AnyCPU and received the bad format exception. So I tried x86 bad format exception because of possible suggestions in the comments. Finally I put it as x64 and it worked fine.
在 AnyCPU 上尝试了一个简单的构建并收到了错误格式的异常。因此,由于评论中可能有建议,我尝试了 x86 错误格式异常。最后我把它作为 x64 并且它工作得很好。
回答by Hymaney Huo
you could using LD_LIBRARY_PATHto solve it:
你可以LD_LIBRARY_PATH用来解决它:
open the warning and check what lib your program lack
locate the path
$ locate # to find the library path
In your IDE, set the environment variable
LD_LIBRARY_PATHto the lib path,(I use monodevelop, the path is Run-->Run With-->Custom Parameters...)
打开警告并检查您的程序缺少什么库
找到路径
$ locate # 查找库路径
在你的IDE中,将环境变量
LD_LIBRARY_PATH设置为lib路径,(我用的是monodevelop,路径是Run-->Run With-->Custom Parameters...)
it work quit good now :)
它现在工作得很好:)
回答by Hello World
If it doesn't work on Windows Server you have to enable UI Desktop Experience.
如果它在 Windows Server 上不起作用,则必须启用 UI 桌面体验。

