How do I correctly link to and use OpenCV?

I’m using VS2015. Here’s what I have so far:

  1. I’ve added the include files and the lib file to Build.cs file as follows:

        using UnrealBuildTool;
        public class MyProject : ModuleRules
        {
        	public MyProject(TargetInfo Target)
        	{
                	PublicIncludePaths.Add(@"C:/PathToOpenCV/build/include");
                	PublicAdditionalLibraries.Add(@"C:/PathToOpenCV/build/x64/vc12/lib/opencv_world300d.lib");
                	PublicDependencyModuleNames.AddRange(new string[] { "Core", "CoreUObject", "Engine", "InputCore" });
                	PrivateDependencyModuleNames.AddRange(new string[] {  });
            }
        }
    
  2. The DLL file of same name is sitting in the same directory as the LIB file.

  3. Everything compiles nicely at this point, including my custom actor classes.

  4. If I attempt to #include a .hpp file from the OpenCV source (such as objdectect.hpp) at the top of one of my existing headers, I get a bunch of nasty errors such as “error C4003: not enough actual parameters for macro ‘check’”, which I believe are macro collisions and other nastiness. I’m not about to try troubleshooting collisions with OpenCV if I can help it… WAY out of my league.

So if I ditch the OpenCV source entirely, is there a way to simply call out to a function in the OpenCV DLL without this headache? I presume it’d be the same for any DLL, but any explanation I’ve found so far has been outdated, incomplete, or has otherwise lead into a swamp of unanswered questions.

I don’t know how to do it but there’s a tutorial on Wiki.

I seem to remember this one being a dead-end as well. Perhaps I had an older version of this page, as I don’t recall the resolution for library collisions being there. I’ll give this one another try and reply back with my findings.

Gave it another shot, and the same thing happened as the first time I tried this one. Per the README file:

  • unzip this folder DONE
  • add the Binaries, Plugins, and ThirdParty folders to your UE4 project (merge if prompted) DONE
  • launch the project DONE

On launch, “The following modules are missing or built with a different engine version: UE4Editor-OpenCV.dll.” Would you like to rebuild them now?" YES “MyProject could not be compiled. Try rebuilding from source manually.”

I’m hoping I don’t need to track down the developer for this apparently outdated plugin. But I’ll keep it as an option if there’s no simple way to call out to the DLL.

Attempting a build directly through VS: OpenCV.Build.cs(10,44): error CS0619: ‘UnrealBuildTool.RulesCompiler.GetModuleFilename(string)’ is obsolete… ; So I updated OpenCV.Build.cs per this answer. On build, I still get “fatal error LNK1181: cannot open input file ‘opencv_ts300.lib’”… If there’s some sign this plugin has been viable in recent history, I’ll follow it down the rabbit hole.

I think I MAY have it solved. Now, I haven’t tested out the functions yet, or tried to package with the DLL, but here are my steps:

  1. Add the following into your Build.cs file, in public MyProject(TargetInfo Target), using paths to your include folder and lib file for OpenCV (3.0.0 in this case):

    PublicIncludePaths.Add(@“C:/PathToOpenCV/build/include”);
    PublicAdditionalLibraries.Add(@“C:/PathToOpenCV/build/x64/vc12/lib/opencv_world300d.lib”);

  2. In the OpenCV code, find and comment out the declaration “bool check() const;” In 3.0.0, it’s in utility.hpp, on line 729.

  3. In the header file of the cpp file in which you want to use the OpenCV method, #include the appropriate header file from the OpenCV includes. Be sure to do that before any “.generated.h” header gets #included.

  4. In your code in the cpp file, type “cv::”. You should see the OpenCV members exposed via the cv namespace!

I’m able to work with the OpenCV members in VS, and can even play the game in UE4. However I’m off-and-on getting messages when I start up or try to play standalone: "The game module ‘MyProject’ could not be loaded. There may be an operating system error or the module may not be properly set up.’ I’m really starting to get homesick for Unity at this point.

This error means UE4 can’t find the proper dlls to load at runtime. Make sure you’ve copied the OpenCV .dll next to your game .dll :slight_smile:

I have updated my tutorial and plugin for 4.14 with opencv 3.0. There was a small API change which is included in the updates. :slight_smile:

As for the dll problems, your system needs to know where the dll’s are at runtime. In my tutorial I suggest putting the ‘opencv_world300.dll’ file in the binaries folder of your project. This works for play-in-editor, but if you use any other features (such as packaging your game) then you will need to place this file in the same folder as your project’s executable (project.exe) file.

Have fun with your OpenCV / UE4 projects! :slight_smile:

-Ginku

Hello Ginku,

I’ve been able to successfully run your plugin inside my UE4 4.14 project. Thank you so much, it has been great help.
One issue I face is its resolution is stuck at 640 * 360 which is very low. Enabling should resize and changing resolution cause UE4 to crash. Can you please guide me how can I improve he resolution?
I’ve tried multiple cameras, they are all Full HD.

Thanks in advance.

You can check my tutorial on integrating OpenCV into Unreal Engine 4.17 with Visual Studio 2017 :slight_smile:

Hello, if someone else has problem to link OpenCV 4 with UE4 and gets the
-too many arguments for function-like macro invocation ‘check’- or the
- syntax error: ‘end Parse’ ‘’- in the cvstd_wrapper.hpp its because UE4 macros are conflicting.

I don’t know why someone would use ‘check’ for macros but u can find more here

Hello, I found when I copired the opencvworld460.dll to Binaries folder, the UE open always stucked!

if I didn’t copied, it can not found dll.

How to solve it?