OpenCV (EMGUCV wrapper) integration in Unity

匆匆过客 提交于 2019-12-28 10:06:38

问题


As you know OpenCV is very useful library that let you do amazing and powerful things in Computer vision. So I passed a good time to figure out how to use it in Unity3d, I had many problems, and searching in the Net, I have found several suggestions but not one worked for me.

  • I’m using a Unity Pro 4.0
  • This version of Emgu CV (emgucv-windows-universal-gpu 2.4.9.1847)
  • My target for unity project is: windows and not web player

回答1:


This is what I did for everyone who want to use it:

  1. Install this version of EmguCV, which creates a folder EMGU in C:
  2. In this folder you will have a single folder named “emgucv-windows-universal-gpu 2.4.9.1847”
  3. Go to “bin folder”
  4. From the “bin folder” you need to copy all the dlls in the x86 folder (22 dlls), and also those in the bin directly (12 dlls: Emgu.CV, Emgu.CV.UI….). For X64 target, you can try the same thing, I think this will work by taking the dlls from the X64 folder.
  5. Create a folder named Plugins, in your Unity project (Assets->Plugins)
  6. Paste all these 34 dlls file in the Plugins folder
  7. From this 34 dll files, copy the “npp32_50_35” and “cudart32_50_35” into the Unity editor folder, in my case it’s “C:\Program Files (x86)\Unity\Editor”
  8. You are done for the Dlls stuff.

This is how to test it:

  1. Create a small script, that takes a “picture1.jpg” and change the color of the first 200 diagonal pixels color to white color (255,255,255) and then save the new “picture2.jpg” This is what you have to put in the script:

    using UnityEngine;
    using System.Collections;    
    using Emgu.CV;    
    using Emgu.CV.Util;    
    using Emgu.CV.UI;           
    using Emgu.CV.CvEnum;    
    using Emgu.CV.Structure;    
    using System.Runtime.InteropServices;    
    using System;    
    using System.Drawing;    
    
    Image<Bgr, byte> picture = new Image<Bgr, byte>("C:\\picture1.jpg");     
    Bgr myWhiteColor = new Bgr(255, 255, 255);    
    For (int i=0; i<200; i++)    
    {picture[i,i]= color;}    
    picture.Save("C:\\picture2.jpg"); 
    
  2. By adding the different dlls in the Plugins folder they will be referenced automatically in the Mono Editor, but probably you will have a problem with “System.Drawing” library, so don’t forget using System.Drawing; and also you have to reference it by going to your script editor.

    If MonoDevelop-Unity, go the solution explorer in the left, right click on Refrences->Edit references-> and search for system.drawing in the left column and check it to see it in the right column, then press ok

  3. Also don’t forget to change the build settings in Unity->file->build settings in this way:

    • PC Mac and Linux instead of webplayer
    • Target= windows
    • Architecture= x86
    • Then push the “player settings button” go to the “other settings in the new screen” and set Api Compatibility lavel to .NET 2.0, instead of .NET 2.0 subset

For now you’re free to test your script and do awesome work using OpenCV

PS: I’m not sure why exactly the “npp32_50_35” and “cudart32_50_35” have to be in the Editor folder, but it’s the only situation that worked for me.



来源:https://stackoverflow.com/questions/16612513/opencv-emgucv-wrapper-integration-in-unity

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!