19

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? Thanks!

Rup
  • 33,765
  • 9
  • 83
  • 112
Dilshan Zuhdi
  • 199
  • 1
  • 1
  • 5
  • I know this is an old question, but if your OS is 64bit then you should use EmguCV x64 dlls and if OS is 32bit, then EmguCV x86 dlls. It is not that important how you build your solution, can easily build as 'Any CPU'. – Mr. Blond May 04 '17 at 18:00
  • download the the nuget for Emgu.cv and the windows runtime http://www.emgu.com/wiki/index.php/Download_And_Installation – RadioSpace Jun 24 '20 at 04:57

13 Answers13

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

Muhammed Tanriverdi
  • 3,230
  • 1
  • 23
  • 24
  • 1
    Guys this is the only way that worked for me after several hours of trying !! Thanks Muhammed – Elie M Mar 25 '15 at 13:07
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.)
jlew
  • 10,491
  • 1
  • 35
  • 58
  • ya its in platform target x86.. and i have included the opencv dlls to the bin directory debug folder but still it gives the same error... – Dilshan Zuhdi Jun 24 '13 at 12:11
  • 1
    Can you break in the debugger when the exception is thrown? You might get more information in the exception details there. – jlew Jun 24 '13 at 12:13
  • im new to this i dont know exactly how to break i the debugger – Dilshan Zuhdi Jun 24 '13 at 16:32
  • In Visual Studio: Debug | Exceptions... | Common Language Runtime Exceptions | [Checked] Thrown – jlew Jun 25 '13 at 18:48
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"

  • 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.dll – Brian Jan 21 '18 at 13:32
2

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

in my project add : opencv_core290.dll , opencv_highgui290.dll , opencv_ffmpeg290.dll , opencv_imageproc290.dll and cudart32_55.dll

2

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.

Best of luck!!!!

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.

2

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.

Mavi Domates
  • 4,262
  • 2
  • 26
  • 45
  • I have that 2 file runtimes windows (x86), vc_redist 2005 -> 2017 (x86 & x64), .net 4.8. But some pc work, and some threw exception. did is missing something? – Trương Quốc Khánh May 19 '21 at 19:04
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.

Mannu
  • 11
  • 1
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).

tamsiv
  • 344
  • 4
  • 8
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 :)

0

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

you can view the post

Hello World
  • 211
  • 2
  • 4
0

I am have a same error in production machine, in development machine no have a problem, the dlls are in same folder.

  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Nov 17 '22 at 23:46
  • This does not really answer the question. If you have a different question, you can ask it by clicking [Ask Question](https://stackoverflow.com/questions/ask). To get notified when this question gets new answers, you can [follow this question](https://meta.stackexchange.com/q/345661). Once you have enough [reputation](https://stackoverflow.com/help/whats-reputation), you can also [add a bounty](https://stackoverflow.com/help/privileges/set-bounties) to draw more attention to this question. - [From Review](/review/late-answers/33189488) – Roland Bär Nov 21 '22 at 11:14
-1

just change the build setting from any CPU to x86 or x64 and this will work.