# EmguCv_Unity



## Getting started

Hello this is a tutorial to use Open CV on Unity-Windows with the use of Emgu CV. 

This is open source but for commercial uses you need to purchase licenses, for more details see : https://www.emgu.com/wiki/index.php/Licensing:#A_Quick_Overview


## Downloads

You'll need .Net

then you can copy the /Plugins directory above and put it in your Unity's /Assets/ directory   
Or build it step by step see below (skip to Test otherwise)
### Step by Step

On this link https://github.com/emgucv/emgucv/releases   
Go find the release 4.9.0 (latest currently) download "libemgucv-windesktop-4.9.0.5494.zip"   

![](screenshots/emgucv_release_4_9_0.png)  

Unzip it and go into \libs

![](screenshots/libemgucv_libs.png)   

Make a directory anywhere called Plugins (It'll go in your unity project later)   
Copy all the dynamic link library ( .dll) here into Plugins. 

Now go into \libs\runtimes\win-x64\native (change win-x64 if you're not in 64 bit)

![](screenshots/libemgucv_libs_native.png)  

And again copy all the dynamic link library ( .dll) here into Plugins.

### Unity

I didn't test in all version but it worked in my 2021 version's project
I'm using 2022.3.37f1 version here. We'll start from a blank 3D project (if you can't make it work in your project you can create a blank project quickly and try)

![](screenshots/unity_blank3D.PNG) 

Dans Assets we will (create a Scripts directory for later)   
and copy the Plugins directory. You will have a lot of Dependencies issues :

![](screenshots/erreur_drag_plugins.PNG)

We'll need to go on www.nuget.org and download those dependencies :   
Here I need System.Text.Json , PresentationCore , System.Drawing.Common , Microsoft.VisualStudio.DebuggerVisualizers
First download the nupkg and extract it (same as downlading a .zip and unzip)

![](screenshots/ex_nuget_pack.PNG)  

Then inside we go to lib/netstandard2.0 and copy the .dll to Plugins

![](screenshots/inside_nupkg.PNG)  

We keep downloading until all dependencies are resolved (note that some dependencies will have dependencies of their own)
I deleted Emgu.CV.Wpf in Plugins because I couldn't find his dependencies PresentationCore. it's a minor part of EmguCv we should stil be able to use the majority of it.

## Test

Wether you downloaded my /Plugins/ directory or built it step by step, we can now make a few test.

(We'll add a ball and a move script to have something to look at in our blank project see Scripts/move.cs)

#### First test: Recording
a real time recording feature to place on a camera see [Scripts/Recorder](Scripts/Recorder.cs). 
Place the script on the camera tou want to record.   

There is a colorConversion because OpenCv is in BGR while Unity is in RGB.
And a Flip because OpenCv places the origin on the top left of images and Unity in the bottom left (=> going up means an increasing Y_coordinate in Unity but decreasing in Open_Cv)

#### Second test: Basic tracking