Wrapping OpenCV

Exclusive offer: get 50% off this eBook here
Emgu CV Essentials

Emgu CV Essentials — Save 50%

Develop your own computer vision application using the power of Emgu CV with this book and ebook

$20.99    $10.50
by Shin Shi | November 2013 | Open Source

In this article by Shin Shi author of the book Emgu CV Essentials, the author is explaining how to wrap OpenCV.

Emgu CV is special because it is a .NET wrapper to OpenCV. The wrapper is so amazing that it lets OpenCV functions be called from .NET compatible languages such as C#, VB, VC++, and IronPython. This article shows the user a better understanding of wrapping OpenCV.

(For more resources related to this topic, see here.)

Architecture overview

In this section we will examine and compare the architectures of OpenCV and Emgu CV.

OpenCV

In the hello-world project, we already knew our code had something to do with the bin folder in the Emgu library that we installed. Those files are OpenCV DLLs, which have the filename starting with opencv_. So the Emgu CV users need to have some basic knowledge about OpenCV.

OpenCV is broadly structured into five main components. Four of them are described in the following section:

  • The first one is the CV component, which includes the algorithms about computer vision and basic image processing. All the methods for basic processes are found here.
  • ML is short for Machine Learning, which contains popular machine learning algorithms with clustering tools and statistical classifiers.

  • HighGUI is designed to construct user-friendly interfaces to load and store media data.
  • CXCore is the most important one. This component provides all the basic data structures and contents.

The components can be seen in the following diagram:

The preceding structure map does not include CvAux, which contains many areas. It can be divided into two parts: defunct areas and experimental algorithms. CvAux is not particularly well documented in the Wiki, but it covers many features. Some of them may migrate to CV in the future, others probably never will.

Emgu CV

Emgu CV can be seen as two layers on top of OpenCV, which are explained as follows:

  • Layer 1 is the basic layer. It includes enumeration, structure, and function mappings. The namespaces are direct wrappers from OpenCV components.
  • Layer 2 is an upper layer. It takes good advantage of .NET framework and mixes the classes together. It can be seen in the bridge from OpenCV to .NET.

The architecture of Emgu CV can be seen in the following diagram, which includes more details:

After we create our new Emgu CV project, the first thing we will do is add references. Now we can see what those DLLs are used for:

  • Emgu.Util.dll: A collection of .NET utilities
  • Emgu.CV.dll: Basic image-processing algorithms from OpenCV
  • Emgu.CV.UI.dll: Useful tools for Emgu controls
  • Emgu.CV.GPU.dll: GPU processing (Nvidia Cuda)
  • Emgu.CV.ML.dll: Machine learning algorithms
Emgu CV Essentials Develop your own computer vision application using the power of Emgu CV with this book and ebook
Published: November 2013
eBook Price: $20.99
Book Price: $34.99
See more
Select your format and quantity:

Function mapping

Emgu.CV.Invoke class introduces an easy way to directly call OpenCV functions in .NET programming languages. Every method in the Emgu.CV.Invoke class keeps the original name in OpenCV. An example of cvCreateImage is given here. In Emgu CV, we write it like this:

IntPtr img1 = CvInvoke.cvCreateImage(new Size(256, 256), IPL_DEPTH.IPL_DEPTH_16U, 1);

It shows the same effects in OpenCV code:

IplImage* img1 = cvCreateImage(cvSize(256, 256), IPL_DEPTH_16U, 1);

Both of them will create an image with the size of 256 x 256 pixels, the depth of 16 bit unsigned, and the channel 1.

According to Emgu CV library documentation, the CvInvoke type exposes the many members and methods. All those hundreds of methods are very useful because they are base functions. Please see the Emgu CV library documentation for more details.

Structure mapping

Emgu.CV.Structurenamespace contains all the direct mappings to OpenCV basic structures. Examples are shown in the following table:

Emgu CV structure

OpenCV structure

Emgu.CV.Structure.MIplImage

IplImage

Emgu.CV.Structure.MCvMat

CvMat

...

...

Emgu.CV.Structure.Mxxxx

xxxx

The prefix M in the table means managed structure.

The best thing is that Emgu CV mixes .NET graphics and the OpenCV structures together. Parts of them are shown in the following structure:

.NET structure

OpenCV structure

System.Drawing.Point

CvPoint

System.Drawing.PointF

CvPoint2D32f

System.Drawing.Size

CvSize

System.Drawing.Rectangle

CvRect

This is amazing because it is just like a bridge from OpenCV to .NET structure. If we know something about .NET graphics programming, we can code our Emgu project more easily because of the structure mapping.

Enumeration mapping

Emgu.CV.CvEnum namespace contains all the direct mappings to OpenCV enumerations. An example is given here:

CvEnum.IPL_DEPTH.IPL_DEPTH_16U

This enumeration shares the same value in OpenCV given here:

IPL_DEPTH_16U

Both of them are 16. We can see that each Emgu CV and OpenCV enum type has the same name. That is what direct mapping means. OpenCV programmers will get quick access to Emgu CV because of it. Please see the Emgu CV library documentation for more details.

Summary

In this article, we examined and compared the architectures of OpenCV and Emgu CV. By the end of the article, the user has a preliminary knowledge of Emgu CV.

Resources for Article:


Further resources on this subject:


Emgu CV Essentials Develop your own computer vision application using the power of Emgu CV with this book and ebook
Published: November 2013
eBook Price: $20.99
Book Price: $34.99
See more
Select your format and quantity:

About the Author :


Shin Shi

Shin Shi is a member of the Digital Art Lab at Shanghai Jiao Tong University. When he built his first robot at the age of fourteen in 2005, he became interested in the field of computer vision. Over the last two years, he created a real-time face recognition attendance system using Emgu CV at Huazhong University of Science and Technology. He obtained a B.E. in Software Engineering from HUST and is currently an M.S. candidate in DA Lab from SJTU. Now he is focusing on the field of computer graphics, such as creating physics-based simulations and the use of animation.

Books From Packt


OpenCV Computer Vision with Python
OpenCV Computer Vision with Python

 Android Application Programming with OpenCV
Android Application Programming with OpenCV

OpenCL Parallel Programming Development Cookbook
OpenCL Parallel Programming Development Cookbook

OpenCL Programming by Example
OpenCL Programming by Example

Mastering OpenCV with Practical Computer Vision Projects
Mastering OpenCV with Practical Computer Vision Projects

OpenCV 2 Computer Vision Application Programming Cookbook
OpenCV 2 Computer Vision Application Programming Cookbook

Instant OpenCV Starter [Instant]
Instant OpenCV Starter [Instant]

Instant OpenCV for iOS [Instant]
Instant OpenCV for iOS [Instant]


Code Download and Errata
Packt Anytime, Anywhere
Register Books
Print Upgrades
eBook Downloads
Video Support
Contact Us
Awards Voting Nominations Previous Winners
Judges Open Source CMS Hall Of Fame CMS Most Promising Open Source Project Open Source E-Commerce Applications Open Source JavaScript Library Open Source Graphics Software
Resources
Open Source CMS Hall Of Fame CMS Most Promising Open Source Project Open Source E-Commerce Applications Open Source JavaScript Library Open Source Graphics Software