Skip to content
Snippets Groups Projects
README.md 3.53 KiB
Newer Older
CHEREL Vincent's avatar
CHEREL Vincent committed
# EmguCv_Unity



## Getting started

CHEREL Vincent's avatar
CHEREL Vincent committed
Hello this is a tutorial to use Open CV on Unity-Windows with the use of Emgu CV. 
CHEREL Vincent's avatar
CHEREL Vincent committed
We use a CPU version.
CHEREL Vincent's avatar
CHEREL Vincent committed

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


## Downloads

You'll need .Net

CHEREL Vincent's avatar
CHEREL Vincent committed
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)
CHEREL Vincent's avatar
CHEREL Vincent committed
### Step by Step
CHEREL Vincent's avatar
CHEREL Vincent committed

CHEREL Vincent's avatar
CHEREL Vincent committed
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"   
CHEREL Vincent's avatar
CHEREL Vincent committed

CHEREL Vincent's avatar
CHEREL Vincent committed
<img src="screenshots/emgucv_release_4_9_0.png" width="550"> 
CHEREL Vincent's avatar
CHEREL Vincent committed

CHEREL Vincent's avatar
CHEREL Vincent committed
Unzip it and go into \libs
Make a directory anywhere called Plugins (It'll go in your unity project later)   
CHEREL Vincent's avatar
CHEREL Vincent committed
Copy all the dynamic link library ( .dll) here into Plugins. 

CHEREL Vincent's avatar
CHEREL Vincent committed
<img src="screenshots/libemgucv_libs.png" width="550">
CHEREL Vincent's avatar
CHEREL Vincent committed

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

CHEREL Vincent's avatar
CHEREL Vincent committed
<img src="screenshots/libemgucv_libs_native.png" width="550"> 
CHEREL Vincent's avatar
CHEREL Vincent committed

CHEREL Vincent's avatar
CHEREL Vincent committed
And again copy all the dynamic link library ( .dll) here into Plugins.

CHEREL Vincent's avatar
CHEREL Vincent committed
### Unity
CHEREL Vincent's avatar
CHEREL Vincent committed

CHEREL Vincent's avatar
CHEREL Vincent committed
I didn't test in all version but it worked in my 2021 version's project
CHEREL Vincent's avatar
CHEREL Vincent committed
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)
CHEREL Vincent's avatar
CHEREL Vincent committed

CHEREL Vincent's avatar
CHEREL Vincent committed
<img src="screenshots/unity_blank3D.PNG" width="550">
CHEREL Vincent's avatar
CHEREL Vincent committed

CHEREL Vincent's avatar
CHEREL Vincent committed
In Assets we will (create a Scripts directory for later and )   
copy the Plugins directory. You will have a lot of Dependencies issues :
CHEREL Vincent's avatar
CHEREL Vincent committed

CHEREL Vincent's avatar
CHEREL Vincent committed
<img src="screenshots/erreur_drag_plugins.PNG" width="550">
CHEREL Vincent's avatar
CHEREL Vincent committed

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

CHEREL Vincent's avatar
CHEREL Vincent committed
<img src="screenshots/ex_nuget_pack.PNG" width="550"> 
CHEREL Vincent's avatar
CHEREL Vincent committed

CHEREL Vincent's avatar
CHEREL Vincent committed
Then inside we go to lib/netstandard2.0 and copy the .dll to Plugins
CHEREL Vincent's avatar
CHEREL Vincent committed

CHEREL Vincent's avatar
CHEREL Vincent committed
<img src="screenshots/inside_nupkg.PNG" width="550">
CHEREL Vincent's avatar
CHEREL Vincent committed

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

CHEREL Vincent's avatar
CHEREL Vincent committed
## Test

CHEREL Vincent's avatar
CHEREL Vincent committed
Wether you downloaded my /Plugins/ directory or built it step by step, we can now make a few test.
CHEREL Vincent's avatar
CHEREL Vincent committed

CHEREL Vincent's avatar
CHEREL Vincent committed
(We'll add a ball and a move script to have something to look at in our blank project see [Scripts/move.cs](Scripts/move.cs))
CHEREL Vincent's avatar
CHEREL Vincent committed

#### First test: Recording
CHEREL Vincent's avatar
CHEREL Vincent committed
a real time recording feature see [Scripts/Recorder](Scripts/Recorder.cs). 
CHEREL Vincent's avatar
CHEREL Vincent committed
Place the script on a gameobject and link it the camera you want to record.   
CHEREL Vincent's avatar
CHEREL Vincent committed

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)

CHEREL Vincent's avatar
CHEREL Vincent committed
The script can extend frames duration be sure to use Time.deltaTime in your scripts when needed.
If you have performance issue try recording in .avi format instead of .mp4 (comment/uncomment appropriate lines).

Note that the path by default ( Assets/output.avi ) will only be useful in the editor as /Assets is inaccessible once built.
You can use "Application.persistentDataPath" for a consistent way to read/write outside your Unity Application.

CHEREL Vincent's avatar
CHEREL Vincent committed

CHEREL Vincent's avatar
CHEREL Vincent committed
#### Second test: Basic tracking