Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
191 views
in Technique[技术] by (71.8m points)

c# - 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!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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:


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...