OpenCV (EMGUCV wrapper) integration in Unity

2019-01-16 20:31发布

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条回答
该账号已被封号
2楼-- · 2019-01-16 21:10

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.

查看更多
登录 后发表回答