Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OpenCV + Unity3D integration

I am a unity developer trying my hands on opencv for the first time. My initial goal is to run the camera and detect blobs via opencv in unity3d. I am new to OpenCV and am trying to integrate it in Unity3D (on Windows 8 with Unity 4.3.2 and on a mac with Unity 4.2.1f). I followed this thread. But I am getting the following error as soon as I add a new C# script. And the moment I delete this script, the error goes (this script is Unity generated C# script).

Internal compiler error. See the console log for more information. output was:
Unhandled Exception: System.Reflection.ReflectionTypeLoadException: The classes in the module cannot be loaded.
  at (wrapper managed-to-native) System.Reflection.Assembly:GetTypes (bool)
  at System.Reflection.Assembly.GetTypes () [0x00000] in <filename unknown>:0 
  at Mono.CSharp.RootNamespace.ComputeNamespaces (System.Reflection.Assembly assembly, System.Type extensionType) [0x00000] in <filename unknown>:0 
  at Mono.CSharp.RootNamespace.ComputeNamespace (Mono.CSharp.CompilerContext ctx, System.Type extensionType) [0x00000] in <filename unknown>:0 
  at Mono.CSharp.GlobalRootNamespace.ComputeNamespaces (Mono.CSharp.CompilerContext ctx) [0x00000] in <filename unknown>:0 
  at Mono.CSharp.Driver.LoadReferences () [0x00000] in <filename unknown>:0 
  at Mono.CSharp.Driver.Compile () [0x00000] in <filename unknown>:0 
  at Mono.CSharp.Driver.Main (System.String[] args) [0x00000] in <filename unknown>:0 

I couldn't find much about Unity and OpenCV integration. It would be great if you could help me out with this error and point me to a recent tutorial to learn more.

Thanks in advance!

like image 214
jainam Avatar asked Jun 07 '14 10:06

jainam


1 Answers

We recently had to deal with the same problem, I'll post some generic information that would solve your problem and help other people.

  1. OpenCV library and your OpenCV project must be compiled as static libraries (see OpenCV as a static library).
  2. OpenCV library and your OpenCV project must be compiled for both 32bit and 64bit architectures.
  3. The 32bit version will be used inside the editor (since the Unity3D editor supports 32bit architectures only), the 64 bit version for production.
  4. The compiled OpenCV project must be copied inside the Asset > Plugins folder, the OpenCV library must be copied inside the Assets folder.
  5. To use your OpenCV project inside a C# script, follow this code example:

    using UnityEngine;
    using System.Collections;
    using System;
    using System.Runtime.InteropServices;
    
    public class PluginImport : MonoBehaviour {
        //Lets make our calls from the Plugin
        [DllImport ("OpenCVProject")]
        private static extern int openCVFunction(); 
    
        void Start () {
            openCVFunction();
        }
    }
    

    pay attention to the using directives!

Other sources of information:

  • Unity Manual:Plugins
like image 92
Alberto Malagoli Avatar answered Sep 30 '22 07:09

Alberto Malagoli