shimat / opencvsharp

OpenCV wrapper for .NET
Apache License 2.0
5.42k stars 1.15k forks source link

System.DllNotFoundException: OpenCvSharpExtern #273

Closed DvD12 closed 4 years ago

DvD12 commented 7 years ago

Environment Lubuntu 16.10 (x86)

Steps followed

  1. I've installed mono-devel, mono-complete, monodevelop
  2. Under MonoDevelop, I've created a .NET C# Console solution
  3. I've used the following code in Program.cs:

    using OpenCvSharp;
    class Program 
    {
    static void Main() 
    {
        Mat src = new Mat("Lenna.png", ImreadModes.GrayScale);
        Mat dst = new Mat();
    
        Cv2.Canny(src, dst, 50, 200);
    }
    }
  4. Using NuGet, I've installed the OpenCvSharp3-AnyCPU package
  5. I've removed the PresentationCore and PresentationFramework packages which couldn't be referenced
  6. I've downloaded the repo and went to src/OpenCvSharpExtern, then ran cmake ./ and make
  7. I've copied the generated libOpenCvSharpExtern.so library in my project's bin/Debug folder

Errors encountered As I build my project and run it, I encounter the following error:

OpenCvSharpExtern An exception has occurred because of P/Invoke. Please check the following: (1) OpenCV's DLL files exist in the same directory as the executable file. (2) Visual C++ Redistributable Package has been installed. (3) The target platform(x86/x64) of OpenCV's DLL files and OpenCvSharp is the same as your project's.

System.DllNotFoundException: OpenCvSharpExtern at (wrapper managed-to-native) OpenCvSharp.NativeMethods:core_Mat_sizeof () at OpenCvSharp.NativeMethods.TryPInvoke () [0x0000e] in <816ddc33ac054fd88cb1b2a9cfae9411>:0

Unhandled Exception: System.TypeInitializationException: The type initializer for 'OpenCvSharp.Mat' threw an exception. ---> System.TypeInitializationException: The type initializer for 'OpenCvSharp.NativeMethods' threw an exception. ---> OpenCvSharp.OpenCvSharpException: OpenCvSharpExtern An exception has occurred because of P/Invoke. Please check the following: (1) OpenCV's DLL files exist in the same directory as the executable file. (2) Visual C++ Redistributable Package has been installed. (3) The target platform(x86/x64) of OpenCV's DLL files and OpenCvSharp is the same as your project's.

System.DllNotFoundException: OpenCvSharpExtern at (wrapper managed-to-native) OpenCvSharp.NativeMethods:core_Mat_sizeof () at OpenCvSharp.NativeMethods.TryPInvoke () [0x0000e] in <816ddc33ac054fd88cb1b2a9cfae9411>:0 ---> System.DllNotFoundException: OpenCvSharpExtern at (wrapper managed-to-native) OpenCvSharp.NativeMethods:core_Mat_sizeof () at OpenCvSharp.NativeMethods.TryPInvoke () [0x0000e] in <816ddc33ac054fd88cb1b2a9cfae9411>:0 --- End of inner exception stack trace --- at OpenCvSharp.NativeMethods.TryPInvoke () [0x00032] in <816ddc33ac054fd88cb1b2a9cfae9411>:0 at OpenCvSharp.NativeMethods..cctor () [0x0006d] in <816ddc33ac054fd88cb1b2a9cfae9411>:0 --- End of inner exception stack trace --- at OpenCvSharp.Mat..cctor () [0x00000] in <816ddc33ac054fd88cb1b2a9cfae9411>:0 --- End of inner exception stack trace --- at Program.Main () [0x00001] in <9e3ae112208c415a9a4a90874c2c166e>:0 [ERROR] FATAL UNHANDLED EXCEPTION: System.TypeInitializationException: The type initializer for 'OpenCvSharp.Mat' threw an exception. ---> System.TypeInitializationException: The type initializer for 'OpenCvSharp.NativeMethods' threw an exception. ---> OpenCvSharp.OpenCvSharpException: OpenCvSharpExtern An exception has occurred because of P/Invoke. Please check the following: (1) OpenCV's DLL files exist in the same directory as the executable file. (2) Visual C++ Redistributable Package has been installed. (3) The target platform(x86/x64) of OpenCV's DLL files and OpenCvSharp is the same as your project's.

System.DllNotFoundException: OpenCvSharpExtern at (wrapper managed-to-native) OpenCvSharp.NativeMethods:core_Mat_sizeof () at OpenCvSharp.NativeMethods.TryPInvoke () [0x0000e] in <816ddc33ac054fd88cb1b2a9cfae9411>:0 ---> System.DllNotFoundException: OpenCvSharpExtern at (wrapper managed-to-native) OpenCvSharp.NativeMethods:core_Mat_sizeof () at OpenCvSharp.NativeMethods.TryPInvoke () [0x0000e] in <816ddc33ac054fd88cb1b2a9cfae9411>:0 --- End of inner exception stack trace --- at OpenCvSharp.NativeMethods.TryPInvoke () [0x00032] in <816ddc33ac054fd88cb1b2a9cfae9411>:0 at OpenCvSharp.NativeMethods..cctor () [0x0006d] in <816ddc33ac054fd88cb1b2a9cfae9411>:0 --- End of inner exception stack trace --- at OpenCvSharp.Mat..cctor () [0x00000] in <816ddc33ac054fd88cb1b2a9cfae9411>:0 --- End of inner exception stack trace --- at Program.Main () [0x00001] in <9e3ae112208c415a9a4a90874c2c166e>:0

I'm not sure what to do. Program asks for a .dll but I have an .so? Not sure if this has something to do with the problem. I've just followed the wiki, which says

When successfully compiled, OpenCvSharpExtern/libOpenCvSharpExtern.so is generated. Put the generated file in the same directory as the executable file.

which is what I've done, but this is not helping. Any help?

DvD12 commented 7 years ago

Running MONO_LOG_LEVEL=debug mono shows the culprit:

Mono: DllImport error loading library 'libOpenCvSharpExtern.so': '/usr/local/lib/libOpenCvSharpExtern.so: undefined symbol: _ZTIN2cv16ParallelLoopBodyE'.

So the compiled library is found but it complains about undefined symbols.

A few people have already had this issue and everyone seems to agree that the problem is inconsistency between compiler and openCV sources being used. I don't think that is the problem though:

  1. I'm using OpenCvSharp3
  2. There was no previous openCV installation in my system
  3. I've used OpenCV 3.1 directly from OpenCV's home page

Therefore there is no way the compiler is using the "wrong" version as there is no other openCV version besides the one I installed.

Wiki says this only works for 2.4.10 but I thought that was an old reference. I might have to try and see if downgrading works.

ybznek commented 7 years ago

@DvD12 Hi, are you able to make it working on Linux? I've tried downgrading, but without success.

DvD12 commented 7 years ago

Nope, I haven't. As much as I like OpenCVSharp's ease of use I had to resort to EMGU. Installing it with NuGet works without the need of any further workaround both in VS and in Mono.

Hodapp87 commented 7 years ago

I'm seeing the very same error on NixOS. opencv_version is reporting 3.2.0 and it is under this that I built libOpenCvSharpExtern.so. I am using the 3.2 release of OpenCvSharp from the release page (not NuGet).

That's building it with nix-shell --pure and the following shell.nix

{ pkgs ? import <nixpkgs> {} }:
let stdenv = pkgs.stdenv;
in stdenv.mkDerivation {
  name = "opencvsharp";
  buildInputs = with pkgs; [
    cmake
    mono
    (pkgs.opencv3.override { enableContrib = true; })
  ];
}
Hodapp87 commented 7 years ago

Okay, I just looked at the CMakeLists.txt and this build is all kinds of mangled.

ldd on the resultant file doesn't show me linking with anything remotely related to OpenCV. CMake's variables for OpenCV_INCLUDE_DIR, OpenCV_LIBRARY_DIR, and OpenCV_LIBRARIES are all completely empty when I run.

As far as I can tell, the build neither tries to find OpenCV, nor to link with it.

I was able to get this to run on NixOS by deleting src/cmake/Modules/FindOpenCV.cmake altogether, and applying the below diff:

diff --git a/src/OpenCvSharpExtern/CMakeLists.txt b/src/OpenCvSharpExtern/CMakeLists.txt
index ea40878..6d7210c 100644
--- a/src/OpenCvSharpExtern/CMakeLists.txt
+++ b/src/OpenCvSharpExtern/CMakeLists.txt
@@ -1,6 +1,12 @@

-include_directories(${OpenCV_INCLUDE_DIR})
-link_directories(${OpenCV_LIBRARY_DIR} ${OpenCV_LIBRARIES})
+find_package(OpenCV REQUIRED)
+if(OpenCV_FOUND)
+   message("Found OpenCV")
+   message("Includes: " ${OpenCV_INCLUDE_DIRS})
+endif(OpenCV_FOUND)
+
+include_directories(${OpenCV_INCLUDE_DIRS})
+link_directories(${OpenCV_LIBS})

 set(OPENCVSHARP_FILES 
        bgsegm.cpp
@@ -24,6 +30,7 @@ set(OPENCVSHARP_FILES
 )

 add_library(OpenCvSharpExtern SHARED ${OPENCVSHARP_FILES})
+target_link_libraries(OpenCvSharpExtern ${OpenCV_LIBS} )

 install(TARGETS OpenCvSharpExtern
         RUNTIME DESTINATION bin

I suspect this broke things on Windows but I haven't tried it.

Later note: This lets things run further, but then I run into what was described in https://github.com/shimat/opencvsharp/issues/85#issuecomment-255036540 with System.EntryPointNotFoundException: imgcodecs_imread.

stale[bot] commented 4 years ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.