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

12

21

I'm getting this error:

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

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.

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?

Chuppah answered 24/6, 2013 at 11:28 Comment(0)
U
16

I had the same problem. My inner exception was "Unable to load 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.

According to emgu wiki

Unobtrusive answered 3/11, 2014 at 10:15 Comment(0)
C
10

It's probably failing to load the unmanaged DLL dependencies. A couple of suggestoins:

  1. Make sure your .NET project is targeting x86 rather than AnyCPU, since you likely have the x86 build of OpenCV.
  2. Make sure the OpenCV dlls are in your PATH (or directly in the bin directory, if this is a Console/WinForms/WPF app.)
Coaming answered 24/6, 2013 at 12:7 Comment(0)
R
6

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"

Ruscher answered 18/2, 2014 at 11:40 Comment(1)
In my case, I did a clean build, and it wiped out what was in the bin folder. Re-installing restored the files. Here's whats in there by default: concrt140.dll cvextern.dll msvcp140.dll opencv_ffmpeg330_64.dll vcruntime140.dllGimcrackery
F
6

Installing this nuget: https://www.nuget.org/packages/Emgu.CV.runtime.windows/ solved my problem, looks like the manual dll copying was resolved by this package.

Flashing answered 11/7, 2020 at 22:37 Comment(0)
D
2
  1. Add three libraries to your project:
    • opencv_core290.dll
    • opencv_highgui290.dll
    • opencv_imgproc290.dll
      290 refers to your OpenCV version.
  2. Change in their property "Do not copy" to "copy always".
  3. Then save your project.
  4. If your machine is 64bit, go to the following folder C:\Emgu\emgucv-windows-universal-cuda 2.9.0.1922\bin\x64; if you're using a 32bit OS: 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.
  5. Rebuild your project.
Discordant answered 16/12, 2014 at 13:29 Comment(0)
A
2

Found this on 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.

Ambuscade answered 15/7, 2017 at 19:28 Comment(0)
A
1

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.

Anaphylaxis answered 6/8, 2015 at 6:17 Comment(0)
V
0

For me problem was solved by clicking in Visual Studio:

Menu -> Project -> Properties -> Build -> Prefer 32-bit.

It must have been a problem with emgu (probably x86) and my OS x64).

Venue answered 12/10, 2016 at 20:41 Comment(0)
P
0

you could using LD_LIBRARY_PATH to solve it:

  1. open the warning and check what lib your program lack

  2. locate the path

    $ locate # to find the library path

  3. In your IDE, set the environment variable LD_LIBRARY_PATH to the lib path,(I use monodevelop, the path is Run-->Run With-->Custom Parameters...)

it work quit good now :)

Prodigal answered 16/10, 2017 at 2:21 Comment(0)
H
0

If it doesn't work on Windows Server you have to enable UI Desktop Experience.

you can view the post

Harvester answered 20/9, 2018 at 7:27 Comment(0)
D
0

Change the build setting from any CPU to x86 or x64:

Doubles answered 11/7, 2022 at 15:44 Comment(0)
F
0

Try installing Windows Visual C++ 2010.

Fiddlededee answered 23/3, 2024 at 23:13 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.