OpenCV + Android + Unity [英] OpenCV + Android + Unity

查看:211
本文介绍了OpenCV + Android + Unity的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种在Unity项目中使用OpenCV的方法,而我的目标平台是Android设备.

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

我知道Unity资产商店中存在一些资产,但是我不要要使用它们,因为我发现它们太昂贵了.

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.

我还找到了opencv jar档案,我知道它们可以很容易地导入Unity,但是我不知道下一步该怎么做:那就是如何从Unity C#脚本访问OpenCV内容.

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.

因此,如果有人知道如何甚至使用Unity编辑器中的OpenCV来配置虚拟的hello world项目以构建到Android,甚至有提示,我都会获得有关此的任何信息.

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.

谢谢.

PS:我知道这个问题有点含糊,请相信我这不是LMGFY问题,因为在Google上有很多这样的问题,但没有真正的答案,所以请不要急于-1投票.

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.

更新

使用教程,我设法使用Android Studio为Android构建了opencv,但是仍然我不知道如何从C#脚本中使用OpenCV.例如,如何创建cv :: Mat?

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?

所以我设法做到了:

  • 使用Android Studio(因此来自Java)构建OpenCV并运行一些本机代码.
  • 构建 Unity本机示例(单个C函数),然后从C#脚本.
  • 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.

但是我仍然不知道如何构建具有OpenCV依赖关系的C ++代码并从C#脚本调用此代码.

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

推荐答案

所以我终于设法使其工作了! =)

So I finally managed to get it work !!!!!!!!!! =)

我在这里发布了它为我工作的方式.因此,我设法做的是建立一个.so C ++库,并使用Visual Studio链接到OpenCV.将此库导入Unity并构建一个非常简单的Android应用程序调用函数,该函数在.so库中定义.在Android手机上运行该应用.

I am posting the way it worked for me here. So what I managed to do is build an .so C++ library with link to OpenCV with Visual Studio. Import this library in Unity and build a very simple Android application calling function defined in the .so library. Run the app on an Android phone.

使用的配置:

  • Unity 2017.2.0f2
  • Visual Studio 2017
  • 适用于Android的OpenCV 3.3.1. opencv-3.3.1-android-sdk.zip 可从 OpenCV网站下载.
  • Android智能手机:已在Lenovo Phab 2 Pro(Google探戈手机)和HTC 10上进行测试
  • Unity 2017.2.0f2
  • Visual Studio 2017
  • OpenCV 3.3.1 for Android. opencv-3.3.1-android-sdk.zip downloadable from OpenCV website.
  • Android smartphone : tested on Lenovo Phab 2 Pro (Google Tango Phone) and HTC 10

请注意,我将描述的步骤对我有用,但如果您在Android设备上使用不同的CPU,则可能对您有所不同(例如,您必须为ARM64而不是ARM进行构建,但事实是,这些步骤只是一个例子.

Note that the steps I will describe worked for me but it might be different for you if you have a different CPU on your Android device (you'll have to build for ARM64 instead of ARM for instance, but the truth is that these steps are just an example).

我将假定您已经在计算机上安装了Android SDK,NDK和Unity,因此您已经能够使用Unity来构建android应用.

I will assume that you already have Android SDK, NDK and Unity installed on your computer, so and that you are already able to build android app with Unity.

步骤1:使用Visual Studio 2017创建C ++ Android库.

  • 文件>新建>项目

从左侧的下拉菜单中转到模板> Visual C ++>跨平台> Android,然后选择动态共享库(Android)". (您可能必须安装VS工具,才能使用VS2017为Android构建).在此示例中,我将保留默认的"SharedObject1"名称.

From the Dropdown menu on the left go to Templates > Visual C++ > Cross Platform > Android, and select "Dynamic Shared Library (Android)". (You might have to install the VS tools the be able to build for Android with VS2017). I'll keep the default "SharedObject1" name for this example.

在解决方案平台"中(在调试/发布"下拉菜单旁边),选择"ARM".我建议您使用发布版本,但在这里我们将坚持调试,因为工作流程完全相同.

In "Solution platform" (next to Debug/Release dropdown) select "ARM". I suggest you build in release but here we'll stick in debug as the workflow is exactly the same.

步骤2:将此C ++ Android项目与适用于Android的OpenCV链接.

  • 在任意位置提取 opencv-3.3.1-android-sdk.zip (不过请选择并记住该位置,因为您需要将文件保存在此位置).您应该有一个包含3个子文件夹的文件夹,分别为"apk","samples"和"sdk".
  • 在Visual Studio中,转到项目">"SharedObject1属性".在配置中,选择所有配置"(因此它适用于调试"和发行版"),平台选择"ARM".然后:
    • 在C/C ++下,将OpenCV的include的完整路径添加到"Additional Include Directories".该路径为:C:\ OpenCV-android-sdk \ sdk \ native \ jni \ include.
    • 在链接器>常规下,将OpenCV的库的完整路径添加到其他库目录".该路径为:C:\ OpenCV-android-sdk \ sdk \ native \ libs \ armeabi-v7a.
    • 在链接器>输入下,将OpenCV的库文件的完整路径添加到其他依赖项".该路径为:C:\ OpenCV-android-sdk \ sdk \ native \ libs \ armeabi-v7a \ libopencv_java3.so.
    • Extract opencv-3.3.1-android-sdk.zip wherever you want (choose and remember the place nevertheless because you'll need to keep the files in this place). You should have a folder with 3 subfolders called "apk", "samples" and "sdk".
    • In Visual Studio, go to Project > SharedObject1 Properties. In configuration choose "All configurations" (so it applies in both Debug and Release), and platform choose "ARM". Then:
      • Under C/C++, add the FULL path to OpenCV's includes to "Additional Include Directories". This path is: C:\Path-to-OpenCV-android-sdk\sdk\native\jni\include.
      • Under Linker > General, add the FULL path to OpenCV's libraries to "Additional Library Directories". This path is: C:\Path-to-OpenCV-android-sdk\sdk\native\libs\armeabi-v7a.
      • Under Linker > Input, add the FULL path to OpenCV's libraries file to "Additional Dependencies". This path is: C:\Path-to-OpenCV-android-sdk\sdk\native\libs\armeabi-v7a\libopencv_java3.so.

      请注意,如果您知道如何设置环境变量,则可以使用环境变量来代替完整路径.我不会在这里解释.

      Note that instead of the full path, you can use environment variable if you know how to set them. I won't explain that here.

      第3步:是时候为我们的库编写一些C ++/OpenCV代码并构建它了

      ShareObject1.h

      extern "C"
      {
          namespace SharedObject1
          {
              float Foopluginmethod();
          }
      }
      

      ShareObject1.cpp

      #include "SharedObject1.h"
      #include <opencv2\core.hpp> // use OpenCV in this C++ Android Library
      
      extern "C" 
      {
          float SharedObject1::Foopluginmethod()
          {
              cv::Mat img(10,10,CV_8UC1); // use some OpenCV objects
              return img.rows * 1.0f;     // should return 10.0f
          }
      }
      

      然后构建库:构建">构建解决方案".如果此处有诸如找不到文件blablabla"之类的错误,请检查是否已将完整路径放入STEP 2或检查环境变量.如果您还有其他错误,我不知道,请提出评论.

      Then build the library: Build > Build Solution. If you have errors here like "Files not found blablabla", check that you put the full paths in STEP 2 or check your environment variables. If you have other errors, I don't know, ask in comments.

      这应该已经在您的VS-Project \ SharedObject1 \ ARM \ Debug(或发行版)路径下生成了一个 libSharedObject1.so 文件.

      This should have generated a libSharedObject1.so file under Path-to-your-VS-Project\SharedObject1\ARM\Debug (or Release).

      步骤4:让我们进入Unity

      • 创建一个新的Unity项目并根据需要命名.
      • 创建一个新场景并保存.
      • 文件>构建设置.在平台"下,选择"Android",然后单击切换平台".
      • 单击播放器设置".在其他设置"下,将程序包名称"更改为您目前的状态(Unity不喜欢默认值).选择"ARMv7"作为设备过滤器.
      • 将场景添加到构建中.

      在您的场景中,选择主摄像机",然后向其添加名为"CallNativeCode"的新C#脚本:添加组件">输入"CallNativeCode">新建脚本">创建并添加".在检查器中的清除标志"中,选择纯色"并放入深色(这仅用于快速演示).

      In your scene, select the Main Camera and add a new C# Script named "CallNativeCode" to it: "Add Component" > type "CallNativeCode" > New Script > Create And Add. In the inspector, in Clear Flags choose "Solid color" and put a dark color (this is just for the quick demo).

      CallNativeCode.cs

      using UnityEngine;
      using System.Collections;
      using System.Runtime.InteropServices;
      
      public class CallNativeCode : MonoBehaviour
      {    
          [DllImport("SharedObject1")]
          private static extern float Foopluginmethod();
      
          void OnGUI ()
          {
              // This Line should display "Foopluginmethod: 10"
              GUI.Label(new Rect(15, 125, 450, 100), "Foopluginmethod: " + Foopluginmethod());
          }
      }
      

      在Assets文件夹下,创建一个名为"Plugins"的子文件夹(拼写很重要),并在Plugins下创建另一个名为"Android"的子文件夹.在此文件夹中,复制文件 libSharedObject1.so libopencv_java3.so (这两个文件的路径在STEP 2和3中).因此,您应该具有类似的内容:

      Under the Assets folder, create a subfolder called "Plugins" (the spelling is important), and an other sub folder under Plugins called "Android". In this folder, copy the files libSharedObject1.so and libopencv_java3.so (the paths to these two files is in STEP 2 and 3). So you should have something like that :

      在Unity Editor中选择 libSharedObject1.so ,然后在检查器中检查插件的选定平台"是否仅选中了Android,并且CPU是ARMv7.对 libopencv_java3.so 做同样的事情.

      Select libSharedObject1.so in Unity Editor and check that in the inspector the Selected plaforms for plugin has only Android checked, and that the CPU is ARMv7. Do the same for libopencv_java3.so.

      现在,您可以在手机上构建和运行您的应用程序,然后尽情享受! ;-)

      Now you can Build and Run your app on a phone, and enjoy ! ;-)

      因此,这只是一个虚拟应用程序,但是它显示正确的短语,可以正常工作!!! =)这意味着我们设法使我们的Android Unity应用程序调用OpenCV C ++代码.关于更复杂的OpenCV C ++代码,这不是这里的主题,是时候让您的创造力发挥作用了.

      So this is just a dummy application, but it displays the right phrase, it works !!! =) That means that we managed to make our Android Unity app call OpenCV C++ code. Regarding more complex OpenCV C++ code, well, this is not the topic here, it's time to let your creativity flow.

      这篇关于OpenCV + Android + Unity的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆