Getting Emgu CV (OpenCV) to work in GHPython Guide

Guide for getting Emgu CV to work in the GHPython component:

You want to do something with Computer Vision and Grasshopper?
Already Realised that OpenCV doesn’t work with IronPython?
Scrapped the forums and found that Emgu CV is an option?
Can’t figure out how to install the damn thing?

I feel for you because I’ve been there too. That’s why I made this guide!

Follow these 7 easy steps and you will be ready to detect faces (and other body parts) from the comfort of the GHPython component in Grasshopper:

Step0:
Turn on your Computer.

Step1:
Download the latest version of EmguCV from SourceForge (in my case it was 4.2.0).

Link for EmguCV download: https://sourceforge.net/projects/emgucv/files/

Step2:
Install EmguCV to “C:\Emgu”

Step3:
Create a python file called “EmguInit.py” and save it at “C:\Emgu\bin”.

The file should contain the following code:

import clr
clr.AddReferenceToFile("Emgu.CV.World.NetStandard.dll")
clr.AddReferenceToFile("Emgu.CV.UI.dll")
from Emgu.CV import *
from Emgu.CV.UI import *
from Emgu.CV.Structure import *
from System import *

Step4:
In Rhino add a Module Search path for python.
You can do this by typing in EditPythonScript in the command line. When the Rhino Python Editor opens, go to Tools --> Options and add “C:\Emgu\bin” to the search paths.

Step5:
Under “C:\Emgu\libs” you are going to find two folders “x64” and “x86”. Copy both of them to the “C:\Program Files\Rhino 6\System” folder.

Step6:
Restart Rhino.

After Rhino starts again you will be able to use Emgu CV from GHPython by importing EmguInit:

import EmguInit
from EmguInit import *

To use OpenCV methods you will need to utilise the Emgu CV “CvInvoke” class:

img = cv2.imread('test_2.png')OpenCV syntax

img = CvInvoke.Imread('test_2.png')Emgu CV syntax

You can find more information on EmguCV CvInvoke methods here:

http://www.emgu.com/wiki/files/4.2.0/document/html/bb544594-4bf7-5a11-594c-f143b226f1da.htm

Good luck in anything you are trying to do!

VBL

4 Likes

This is awesome! Thanks for sharing.

But I noticed I don’t get CvInvoke.VideoCapture (cv2.VideoCapture), so no webcam access? Would be a shame if this is the case :frowning:

and with Emgu CV 4.3, there is no bin folder from the zip download from: https://github.com/emgucv/emgucv/releases/tag/4.3.0. So I’m using the 4.2 release as well. Or do I need to build the .dll?