OpenCV + Android + Unity

Viewed 11933

I am looking for a way to use OpenCV in a Unity project and my target platform is an Android device.

I know that some assets exists on Unity asset store but I DO NOT want to use them as I find them way too expensive.

I manage to use use opencv in Unity as a C++ native pluggins by precompiling OpenCV in dlls using this tutorial, but dll means Windows Desktop so it doesn't help me much to build my project for Android.

I also found opencv jar archive, I know they can be easily imported into Unity, but I don't know how to do the next step: that is how to access OpenCV stuff from Unity C# scripts.

So, if anyone knows how to configure even a dummy hello world project using OpenCV in Unity editor for build to Android, or even has hints, I would take any infos about that.

Thanks in advance.

PS: I know this question is some sort of vague, and trust me it is not a LMGFY question as on google there is a lot of question like this and no real answer, so please don't rush -1 vote.

UPDATE

Using this tutorial, I managed to build opencv for Android using Android studio, but still I don't know how to use OpenCV from C# scripting. For example, how to create a cv::Mat?

So what I managed to do:

  • Build OpenCV and run some native code using Android studio (so from Java).
  • Build the Unity native example (one single C function) and call it from a C#script.

But I still can't figure out how to build some C++ code with OpenCV dependencies and call this code from a C# script.

3 Answers

This is just to add on to Pierre's answer, I wanted to comment but I don't have enough reputation. I followed his answer using the latest version of OpenCV(4.0.1) and there were a few extra things I needed to do.

First, replace the import with #include <opencv2/opencv.hpp>. The other import statement points to old code.

Then go to the project's properties and select C/C++->Language. Set the language standard to C++ 11 and set Enable Run-Time Type Information to yes.

Then go to Code Generation and set Enable C++ Exceptions to yes.

After that, you should be all set.

Just to share what i had to do to compile the .so using C++17 with OpenCV 4.1.0 in Visual Studio.

First, i had to update my AndroidNDK via the VS-Installer to n15c. Using a separately downloaded NDK did not work for me. When starting to build, the NDK_ROOT will be printed so you can check if it's setup correctly.

Follow Pierre's Steps 1 and 2. In Linker->Input, i only set the library name in "Library Dependencies". I my case, that's opencv_java4, which is how the Linker likes it, derived from libopencv_java4.so

In Tab General set:

  • Platform Toolset: Clang 5.0
  • Configuration Type: Dynamic Library (.so)
  • Target API: Nougat 7.0 (android-24) (others might be fine, too)
  • Use of STL: LLVM libc++ shared (c++_shared)

In Tab Language set:

  • Enable Run-Time Information: true
  • C++ Language Standard: c++17 (-std=c++1z)

In Tab Code Generation set Enable C++ Exceptions to yes

In Tab Linker->Command Line add -lm -lz

I also had to set Not using Precompiled Headers in C/C++ -> Precompiled Headers. I don't think this is generally necessary though.

Hope this will be helpful for someone!

Related