Tutorial: Difference between revisions

From EMGU
Jump to navigation Jump to search
 
(65 intermediate revisions by 12 users not shown)
Line 1: Line 1:
==Namespace==
== Wrapping OpenCV ==


===Emgu===
===Function Mapping - Emgu.CV.CvInvoke ===
All libraries implemented by Emgu® belongs to the namespace of Emgu.
 
===Emgu.CV===
The Emgu.CV namespace implement wrapper functions for [[OpenCV]]. To use this namespace in your code, it is recommended to include
<source lang="csharp">
using Emgu.CV;
</source>
in the beginning of your C# code.
 
====Function Mapping - Emgu.CV.CvInvoke ====
The CvInvoke class provides a way to directly invoke [[OpenCV]] function within .NET languages. Each method in this class corresponds to a function in [[OpenCV]] of the same name. For example, a call to  
The CvInvoke class provides a way to directly invoke [[OpenCV]] function within .NET languages. Each method in this class corresponds to a function in [[OpenCV]] of the same name. For example, a call to  
<source lang="csharp">  
<source lang="csharp">  
  IntPtr image = CvInvoke.cvCreateImage(new MCvSize(400, 300), CvEnum.IPL_DEPTH.IPL_DEPTH_8U, 1);  
  IntPtr image = CvInvoke.cvCreateImage(new System.Drawing.Size(400, 300), CvEnum.IPL_DEPTH.IPL_DEPTH_8U, 1);  
</source> is equivalent to the following function call in C   
</source> is equivalent to the following function call in C   
<source lang="c">  
<source lang="c">  
  IplImage* image = cvCreateImage(cvSize(400, 300), IPL_DEPTH_8U, 1);  
  IplImage* image = cvCreateImage(cvSize(400, 300), IPL_DEPTH_8U, 1);  
</source>
</source>
Both of which create a 400x300 of 8-bit unsigned grayscale image .
Both of which create a 400x300 of 8-bit unsigned grayscale image.


====Enumeration Mapping - Emgu.CV.CvEnum ====
===Structure Mapping - Emgu.CV.Structure.M''xxx'' ===
The CvEnum namespace provides direct mapping to [[OpenCV]] enumerations. For example, <code> CvEnum.IPL_DEPTH.IPL_DEPTH_8U </code> has the same value as <code> IPL_DEPTH_8U </code> in [[OpenCV]]; both of which equals <code>8</code>.
This type of structure is a direct mapping to [[OpenCV]] structures.
{| style="text-align:center" border="1px" cellspacing="0" cellpadding="5"
![[Emgu CV]] Structure || [[OpenCV]] structure
|-
| Emgu.CV.Structure.MIplImage || IplImage
|-
| Emgu.CV.Structure.MCvMat || CvMat
|-
| ... || ...
|-
| Emgu.CV.Structure.M''xxxx'' || ''xxxx''
|}


====Structure Mapping - Emgu.CV.M''xxx'' ====
This type of structure is a direct mapping to [[OpenCV]] structures. For example
*<code> MIplImage </code> is equivalent to <code> IplImage </code> structure in [[OpenCV]]
*<code> MCvSize</code> is equivalent to <code>CvSize</code> structure
*<code> M''xxxx'' </code> is equivalent to <code>''xxxx''</code> structure
The prefix ''M'' here stands for Managed structure.
The prefix ''M'' here stands for Managed structure.


==Working with images==
[[Emgu CV]] also borrows some existing structures in .Net to represent structures in [[OpenCV]]:
=== Depth and Color as Generic Parameter ===
An Image is defined by its generic parameters: '''color''' and '''depth'''. To create a 8bit unsigned Grayscale image, in [[Emgu CV]] it is done by calling
<source lang="csharp">
Image<Gray, Byte> image = new Image<Gray, Byte>( width, height);
</source>
Not only this syntax make you aware the color and the depth of the image, it also restrict the way you use functions and capture errors in compile time. For example, the <code>SetValue(C color, Image<Gray, Byte>  mask)</code> function in Image<C, D> class (version >= [[Version_History#Emgu.CV-1.2.2.0|1.2.2.0]])  will only accept colors of the same type, and mask has to be an 8-bit unsigned grayscale image. Any attempts to use a 16-bit floating point or non-grayscale image as a mask will results a compile time error!


===Creating Image===
{| style="text-align:center" border="1px" cellspacing="0" cellpadding="5"
Although it is possible to create image by calling <code>CvInvoke.cvCreateImage</code>, it is suggested to construct a Image< [[#Image Color |Color]], [[#Image Depth |Depth]]> object instead. There are several advantages using the managed Image<Color, Depth> class
!.Net Structure || [[OpenCV]] structure
* Memory is automatically released when the garbage collector dispose the Image< [[#Image Color |Color]], [[#Image Depth |Depth]]> Object
|-
* Image< [[#Image Color |Color]], [[#Image Depth |Depth]]> class contains advanced method that is not available on [[OpenCV]], for example, [[Tutorial#Generic_Operation | generic operation on image pixels]], conversion to Bitmap etc.  
| System.Drawing.Point || CvPoint
|-
| System.Drawing.PointF || CvPoint2D32f
|-
| System.Drawing.Size || CvSize
|-
| System.Drawing.Rectangle || CvRect
|}


====Image Color====
===Enumeration Mapping - Emgu.CV.CvEnum ===
The first generic parameter of the Image class specific the color of the image type. For example
The CvEnum namespace provides direct mapping to [[OpenCV]] enumerations. For example, <code> CvEnum.IPL_DEPTH.IPL_DEPTH_8U </code> has the same value as <code> IPL_DEPTH_8U </code> in [[OpenCV]]; both of which equals <code>8</code>.


<code>Image<Gray, ...> img1; </code>
==Managed classes==
===[[Working with Images]]===
===[[Working with Matrices]]===


indicates that <code>img1</code> is a single channel grayscale image.
==Error Handling==
[[Emgu CV]] register a custom error handler in [[OpenCV]]. When error is encountered from [[OpenCV]], a <code>CvException</code> will be thrown.
Color Types supported in [[Version_History#Emgu.CV-1.3.0.0|Emgu CV 1.3.0.0]] includes:
* Gray
* Bgr (Blue Green Red)
* Bgra (Blue Green Red Alpha)
* Hsv (Hue Saturation Value)
* Hls (Hue Lightness Saturation)
* Lab (CIE L*a*b*)
* Luv (CIE L*u*v*)
* Xyz (CIE XYZ.Rec 709 with D65 white point)
* Ycc (YCrCb JPEG)


====Image Depth====
==Code Documentation==
Image Depth is specified using the second generic parameter <code>Depth</code>.
===Xml Documentation===
The types of depth supported in [[Version_History#Emgu.CV-1.4.0.0|Emgu CV 1.4.0.0]] include
Documentation is embedded in the code using xml format, which can then be compiled as HTML documentation using [http://www.codeplex.com/Sandcastle Sandcastle]. You can browse our [[Documentation | Online Documentation]] for the latest stable and development code.
* Byte
* SByte
* Single (float)
* Double
* UInt16
* Int16
* Int32 (int)


====Creating a new image====
===Method Documentation===
To create an 480x320 image of Bgr color and 8-bit unsigned depth. The code in C# would be
A library of coding examples according to the methods is being formed here: [http://www.emgu.com/wiki/index.php/Code_Reference Online Code Reference].
<source lang="csharp">
Image<Bgr, Byte> img1 = new Image<Bgr, Byte>(480, 320);
</source>
If you wants to specify the background value of the image, let's say in Blue. The code in C# would be
<source lang="csharp">
Image<Bgr, Byte> img1 = new Image<Bgr, Byte>(480, 320, new Bgr(255, 0, 0));
</source>
====Reading image from file====
Creating image from file is also simple. If the image file is "MyImage.jpg", in C# it is
<source lang="csharp">
Image<Bgr, Byte> img1 = new Image<Bgr, Byte>("MyImage.jpg");
</source>


====Creating image from Bitmap====
===Intellisense in Visual Studio===
It is also possible to create an Image< [[#Image Color |Color]], [[#Image Depth |Depth]]> from a .Net Bitmap object. The code in C# would be
If you are using Visual Studio as your development tools, you will have intellisense support when developing [[Emgu CV]] applications. For example, if you want to create an image directly using cvCreateImage function, which is wrapped by the CvInvoke Class, just type <code>CvInvoke.</code>
<source lang="csharp">
Image<Bgr, Byte> img = new Image<Bgr, Byte>(bmp); //where bmp is a Bitmap
</source>


===Automatic Garbage Collection===
[[image:EmguCvIntellisenseCreateImage1.GIF]]
The Image class will automatically take care of the memory management and garbage collection.  


Once the garbage collector decided that there is no more reference to the Image object, it will call the <code>Disposed</code> method, which involves the <code>DisposeObjects</code> method that release the unmanaged IplImage with a call to the <code>CvInvoke.cvReleaseImage</code> function.
and a list of functions belonging to <code>CvInvoke</code> class is displayed along with a description for each of the functions. Since you are creating an image, select the <code> cvCreateImage </code> function


The time of when garbage collector decides to dispose the image is not guaranteed. When working with large image, it is recommend to call the <code>Dispose()</code> method to explicitly release the object. Alternatively, use the '''''using''''' keyword in C# to limit the scope of the image
[[image:EmguCvIntellisenseCreateImage2.GIF]]


<source lang="csharp">
The list of parameters for this function will be displayed as well as a description for each of the parameters.
using (Image<Gray, Single> image = new Image<Gray, Single>(1000, 800))
{
  ... //do something here in the image
} //The image will be disposed here and memory freed
</source>


===Getting or Setting Pixels===
==Examples==
* Suppose you are working on an Image<Bgr, Byte>. You can obtain the pixel on the y-th row and x-th column by calling
<b>[http://www.emgu.com/wiki/index.php/Code_Reference Online Code Reference]</b>
<source lang="csharp">
Bgr color = img[y, x];
</source>
* Setting the pixel on the y-th row and x-th column is also simple
<source lang="csharp">
img[y,x] = color;
</source>


===Methods===
===C#===
====Naming Convention====
====Image Processing Examples ====
* Method <code>XYZ</code> in Image< [[#Image Color |Color]], [[#Image Depth |Depth]]> class corresponse to the [[OpenCV]] function <code>cvXYZ</code>. For example, Image< [[#Image Color |Color]], [[#Image Depth |Depth]]>.Not() function corresponse to <code> cvNot </code> function with the resulting image being returned.
<b>Introductions</b>
* Method <code>_XYZ</code> is usually the same as Method <code>XYZ</code> except that the operation is performed '''inplace''' rather than returning a value. For example, Image< [[#Image Color |Color]], [[#Image Depth |Depth]]>._Not() function performs the bitwise inversion inplace.
* [[Hello World in CSharp|Hello World for Windows]]
* [[Hello World for Ubuntu|Hello World for Ubuntu]]
* [[Setting_up_EMGU_C_Sharp| User Guide to EMGU and Accessing Image Data]]
* [[Camera Capture in a few lines of code]]
<b>Intermediate</b>
* [[Shape (Triangle, Rectangle, Circle, Line) Detection in CSharp | Shape (Triangle, Rectangle, Circle, Line) Detection]]
* [[SURF feature detector in CSharp|SURF Feature Detector]]
* [[FAST feature detector in CSharp|FAST Feature Detector]]
* [[WPF in CSharp|Windows Presentation Foundation (WPF)]]
* [[Face detection| Face detection in Csharp]]
* [[Pedestrian Detection in CSharp | Pedestrian Detection, Histogram of oriented gradients (HOG)]]
* [[Traffic Sign Detection in CSharp|Traffic Sign Detection]]
* [[License Plate Recognition in CSharp|License Plate Recognition (LPR), Optical Character Recognition (OCR)]]
* [[Image Stitching in CSharp| Image Stitching]]
* [[Kalman_Filter| Using the Kalman Filter]]
* [[Asp.Net Core on Ubuntu| Asp.Net Core project on Ubuntu]]


===Operators Overload===
====Computational Geometry Examples ====
The operators <code>+ - * /</code> has been overloaded (version > [[Version_History#Emgu.CV-1.2.2.0|1.2.2.0]]) such that it is perfectly legal to write codes like:
* [[Planar Subdivision in CSharp|Delaunay's Triangulation and Voronoi Diagram]]
* [[Convex Hull in CSharp| Convex Hull]]
* [[Ellipse Fitting in CSharp | Ellipse Fitting]]
* [[Minimum Area Rectangle in CSharp | Minimum Area Rectangle]]
* [[Minimum Enclosing Circle in CSharp | Minimum Enclosing Circle]]


Image<Gray, Byte> image3 = (image1 + image2 - 2.0) * 0.5;
====Machine Learning Examples ====
* [[Normal Bayes Classifier in CSharp | Normal Bayes Classifier ]]
* [[K Nearest Neighbors in CSharp | K Nearest Neighbors ]]
* [[SVM (Support Vector Machine) in CSharp | Support Vector Machine (SVM) - thanks to Albert G.]]
* [[Expectation-Maximization in CSharp | Expectation-Maximization (EM)]]
* [[ANN MLP (Neural Network) in CSharp | Neural Network (ANN MLP) ]]
* [[Mushroom Poisonous Prediction (Decision Tree) in CSharp | Mushroom Poisonous Prediction (Decision Tree) ]]


===Generic Operation===
==== Video Codec ====
One of the advantage of using [[Emgu CV]] is the ability to perform generic operations.
* [[H264 Codec | VideoWriter with H264 codec]]


It's best if I demonstrate this with an example. Suppose we have an grayscale image of bytes
===C++===
<source lang="csharp">
* [[Hello World in C++|Hello World]]
Image<Gray, Byte> img1 = new Image<Gray, Byte>(400, 300, new Gray(30));
===IronPython===
</source>
* [[Setting up Emgu CV and IronPython]]
To invert all the pixels in this image we can call the Not function
* [[Face Detection from IronPython]]
<source lang="csharp">
===VB.NET===
Image<Gray, Byte> img2 = img1.Not();
* [[Face Detection in VB.NET]]
</source>
* [[Hello World in VB.NET]]
As an alternative, we can also use the generic method <code> Convert </code> available from the Image< [[#Image Color |Color]], [[#Image Depth |Depth]]> class
<source lang="csharp">
Image<Gray, Byte> img3 = img1.Convert<Byte>( delegate(Byte b) { return (Byte) (255-b); } );
</source>
The resulting image <code>img2</code> and <code>img3</code> contains the same value for each pixel.
 
At first glance it wouldn't seems to be a big gain when using generic operations. In fact, since [[OpenCV]] already has an implementation of the <code>Not</code> function and performance-wise it is better than the generic version of the equivalent <code>Convert</code> function call. However, there comes to cases when generic functions provide the flexibility with only minor performance penalty. 
 
Let's say you have an <code>Image<Gray, Byte> img1</code> with pixels set. You wants to create a single channel floating point image of the same size, where each pixel of the new image, correspond to the old image, described with the following delegate
<source lang="csharp">
delegate(Byte b) { return (Single) Math.cos( b * b / 255.0); }
</source>
This operation can be completed as follows in [[Emgu CV]]
<source lang="csharp">
Image<Gray, Single> img4 = img1.Convert<Single>( delegate(Byte b) { return (Single) Math.cos( b * b / 255.0); }  );
</source>
The syntax is simple and meaningful. On the other hand, this operation in [[OpenCV]] is hard to perform since equivalent function such as <code>Math.cos</code> is not available.


===Drawing Objects on Image===
===Unity===
The <code> Draw( )</code> method in Image< [[#Image Color |Color]], [[#Image Depth |Depth]]> can be used to draw different types of objects, including fonts, lines, circles, rectangles, boxes, ellipses as well as contours. Use the documentation and intellisense as a guideline to discover the many functionality of the <code> Draw </code> function.
* [[Working with Vuforia]]


===Color and Depth Conversion===
== Upgrading from Emgu CV 2.x to 3.x ==
Converting an Image< [[#Image Color |Color]], [[#Image Depth |Depth]]> between different colors and depths are simple. For example, if you have <code> Image<Bgr, Byte> img1 </code> and you wants to convert it to a grayscale image of Single, all you need to do is
<source lang="csharp">
Image<Gray, Single> img2 = img1.Convert<Gray, Single>();
</source>


=== Displaying Image ===
===Function Mapping - Emgu.CV.CvInvoke ===
====Using ImageBox====
[[Emgu CV]] recommand the use of [[ImageBox]] control for display purpose. The reasons are
* [[ImageBox]] is a high performance control for displaying image. Whenever possible, it display a Bitmap that shared memory with the Image object, therefore no memory copy is need (very fast).
* The user will be able to exam the image pixel values, video frame rates, color types when the image is being displayed.
* It is convenience to perform simple image operation with just a few mouse click.
To find out more about [[ImageBox]], go to [[ImageBox|this page]]


====Converting to Bitmap====
In Emgu CV v2.x, CvInvoke function calls use the C interface. In v3.x, we have migrate away from the opencv c interface to opencv C++ interface, so does the function names.  
The Image class has a <code>ToBitmap()</code> function that return a Bitmap object, which can easily be displayed on a PictureBox control using Windows Form.


===XML serialization===
For example, in v2.x, the function <pre>CvInvoke.cvAnd(IntPtr src1, IntPtr src2, IntPtr dst, Intptr mask)</pre> has been replaced by <pre>CvInvoke.BitwiseAnd(IInputArray src1, IInputArray src2, IOutputArray dst, IInputArray mask)</pre>
One of the future of Emgu CV is that Image< [[#Image Color |Color]], [[#Image Depth |Depth]]> can be XML serializated. You might ask why we need to serialization an Image. The answer is simple, we wants to use it in a web service!


Since the Image< [[#Image Color |Color]], [[#Image Depth |Depth]]> class implements ISerializable, when you work in WCF (Windows Communication Fundation), you are free to use Image< [[#Image Color |Color]], [[#Image Depth |Depth]]> type as parameters or return value of a web service.  
The best way to find out the new function names if you are migrating from version 2.x is through the Open CV documentation:


This will be ideal, for example, if you are building a cluster of computers to recognize different groups of object and have a centre computer to coordinate the tasks. I will also be useful if your wants to implement remote monitoring software that constantly query image from a remote server, which use the <code>Capture</code> class in Emgu CV to capture images from camera.
http://docs.opencv.org/trunk/


==Working with Matrix==
You can search for the C function name and the search result should have the C++ function name right next to the C interface.
=== Depth as Generic Parameter ===
A Matrix is defined by its generic parameters '''depth'''. To create a 32bit floating point matrix, in [[Emgu CV]] it is done by calling
<source lang="csharp">
Matrix<Single> matrix = new Matrix<Single>( width, height);
</source>
=== Matrix Depth ===
The types of depth supported in [[Version_History#Emgu.CV-1.4.0.0|Emgu CV 1.4.0.0]] include
* Byte
* SByte
* Single (float)
* Double
* UInt16
* Int16
* Int32 (int)


==Error Handling==
=== IInputArray, IOutputArray ===
Pop up box is out.
Exception is the way to go!


In [[Emgu CV]] we register our custom error handler in [[OpenCV]]. When error is encountered a CvException will be thrown, from where you can find the detail information. It is up to you to decide what to do next.
<code>IInputArray</code> has been introduced in version 3.0. You can find that many of our new interfaces accepts <code>IInputArray</code> and <code>IOutputArray</code>. They can be any one of the following:
*A CvArray, which is the base class of Matrix and Image<,>
*A Mat, which is the Open CV equivalent of cv::Mat
*A UMat, which is the Open CV equivalent of cv::UMat
*A ScalarArray, which can be used to convert a scalar to an IInputArray
*VectorOf{XXX}, this is the interface for the C++ standard vector


==Code Documentation==
=== T-API ===  
===Xml Documentation===
'''T-API is THE MOST AWESOME FUTURE in 3.0 release !!!'''
Documentation is embedded in the code using xml format, which can then be compiled as HTML documentation using [http://www.codeplex.com/Sandcastle Sandcastle]. You can browse our [[Documentation | Online Documentation]] for the latest stable and development code.


===Intellisense in Visual Studio===
Let me explain why:
If you are using Visual Studio as your development tools, you will have intellisense support when developing [[Emgu CV]] applications. For example, if you wants to create an image directly using cvCreateImage function, which is wrapped by the CvInvoke Class, just type <code>CvInvoke.</code>


[[image:EmguCvIntellisenseCreateImage1.GIF]]
For a simple image operation, suppose we have an image in memory and we wants to perform an invert operation. In Emgu CV 2.x, we can write the code as follows:
<pre>
Image<Gray, Byte> image = ... //load the image from some where
Image<Gray, Byte> imageInvert = new Image<Gray, Byte>(image.Width, image.Height);
CvInvoke.cvNot(image, imageInvert);
</pre>
In Emgu CV 3.x, we can still use the Image<Gray, Byte> class to perform the same operation, with a slight change in the CvInvoke function name
<pre>
Image<Gray, Byte> image = ... //load the image from some where
Image<Gray, Byte> imageInvert = new Image<Gray, Byte>(image.Width, image.Height);
CvInvoke.BitwiseNot(image, imageInvert);
</pre>
To realize the true potential with T-API, let's try to use UMat to perform the same operation
<pre>
UMat image = ... //load the image from some where
UMat imageInvert = new UMat();
CvInvoke.BitwiseNot(image, imageInvert);
</pre>
It all seems to be not much different from the code that use the Image<,> class in 3.0. However, the above code can automatically use OpenCL engine to perform the operation if a suitable OpenCL device is found. That means it will run many times faster on a system with a discrete GPU (Nvidia, AMD, Intel Iris Pro etc). On systems that do not have a OpenCL devices, the code will be run on CPU and have the same performance as if we are passing the Image<,> or Mat objects to the CvInvoke function.


and a list of functions belongs to <code>CvInvoke</code> class is displayed along with a description for each of the function. Since you are creating an image, select the <code> cvCreateImage </code> function
In short, T-API enable developer to automatically use the OpenCL devices (GPU) for computing and automatically fall back to CPU in the absent of OpenCL devices. You can also turn the OpenCL engine off by simply setting
<pre>
CvInvoke.UseOpenCL = false
</pre>
In which case all the code will be run on CPU instead.


[[image:EmguCvIntellisenseCreateImage2.GIF]]
The T-API is the motivation for us to rewrite all our code using the OpenCV C++ interface to take advantage of this future. We believe it is well worth the effort once we see the results.
 
The list of parameters for this function will be displayed as well as a description for each of the parameters.
 
==Examples==
===C#===
* [[Hello World in CSharp|Hello World]]
* [[Camera Capture in 7 lines of code]]
* [[Shape (Triangle, Rectangle, Circle, Line) Detection in CSharp]]
* [[SURF feature detector in CSharp|SURF feature detector]]
* [[Planar Subdivision in CSharp|Delaunay's Triangulation and Voronoi Diagram]]
* [[WPF in CSharp|WPF (Windows Presentation Foundation)]]
* [[ANN MLP (Neural Network) in CSharp | ANN MLP (Neural Network) ]]
* [[K Nearest Neighbors in CSharp | K Nearest Neighbors ]]
 
===C++===
* [[Hello World in C++|Hello World]]
===IronPython===
* [[Setting up Emgu CV and IronPython]]
* [[Face Detection from IronPython]]
===VB.NET===
* [[Face Detection in VB.NET]]

Latest revision as of 21:12, 10 January 2023

Wrapping OpenCV

Function Mapping - Emgu.CV.CvInvoke

The CvInvoke class provides a way to directly invoke OpenCV function within .NET languages. Each method in this class corresponds to a function in OpenCV of the same name. For example, a call to

 
 IntPtr image = CvInvoke.cvCreateImage(new System.Drawing.Size(400, 300), CvEnum.IPL_DEPTH.IPL_DEPTH_8U, 1);

is equivalent to the following function call in C

 
 IplImage* image = cvCreateImage(cvSize(400, 300), IPL_DEPTH_8U, 1);

Both of which create a 400x300 of 8-bit unsigned grayscale image.

Structure Mapping - Emgu.CV.Structure.Mxxx

This type of structure is a direct mapping to OpenCV structures.

Emgu CV Structure OpenCV structure
Emgu.CV.Structure.MIplImage IplImage
Emgu.CV.Structure.MCvMat CvMat
... ...
Emgu.CV.Structure.Mxxxx xxxx

The prefix M here stands for Managed structure.

Emgu CV also borrows some existing structures in .Net to represent structures in OpenCV:

.Net Structure OpenCV structure
System.Drawing.Point CvPoint
System.Drawing.PointF CvPoint2D32f
System.Drawing.Size CvSize
System.Drawing.Rectangle CvRect

Enumeration Mapping - Emgu.CV.CvEnum

The CvEnum namespace provides direct mapping to OpenCV enumerations. For example, CvEnum.IPL_DEPTH.IPL_DEPTH_8U has the same value as IPL_DEPTH_8U in OpenCV; both of which equals 8.

Managed classes

Working with Images

Working with Matrices

Error Handling

Emgu CV register a custom error handler in OpenCV. When error is encountered from OpenCV, a CvException will be thrown.

Code Documentation

Xml Documentation

Documentation is embedded in the code using xml format, which can then be compiled as HTML documentation using Sandcastle. You can browse our Online Documentation for the latest stable and development code.

Method Documentation

A library of coding examples according to the methods is being formed here: Online Code Reference.

Intellisense in Visual Studio

If you are using Visual Studio as your development tools, you will have intellisense support when developing Emgu CV applications. For example, if you want to create an image directly using cvCreateImage function, which is wrapped by the CvInvoke Class, just type CvInvoke.

and a list of functions belonging to CvInvoke class is displayed along with a description for each of the functions. Since you are creating an image, select the cvCreateImage function

The list of parameters for this function will be displayed as well as a description for each of the parameters.

Examples

Online Code Reference

C#

Image Processing Examples

Introductions

Intermediate

Computational Geometry Examples

Machine Learning Examples

Video Codec

C++

IronPython

VB.NET

Unity

Upgrading from Emgu CV 2.x to 3.x

Function Mapping - Emgu.CV.CvInvoke

In Emgu CV v2.x, CvInvoke function calls use the C interface. In v3.x, we have migrate away from the opencv c interface to opencv C++ interface, so does the function names.

For example, in v2.x, the function

CvInvoke.cvAnd(IntPtr src1, IntPtr src2, IntPtr dst, Intptr mask)

has been replaced by

CvInvoke.BitwiseAnd(IInputArray src1, IInputArray src2, IOutputArray dst, IInputArray mask)

The best way to find out the new function names if you are migrating from version 2.x is through the Open CV documentation:

http://docs.opencv.org/trunk/

You can search for the C function name and the search result should have the C++ function name right next to the C interface.

IInputArray, IOutputArray

IInputArray has been introduced in version 3.0. You can find that many of our new interfaces accepts IInputArray and IOutputArray. They can be any one of the following:

  • A CvArray, which is the base class of Matrix and Image<,>
  • A Mat, which is the Open CV equivalent of cv::Mat
  • A UMat, which is the Open CV equivalent of cv::UMat
  • A ScalarArray, which can be used to convert a scalar to an IInputArray
  • VectorOf{XXX}, this is the interface for the C++ standard vector

T-API

T-API is THE MOST AWESOME FUTURE in 3.0 release !!!

Let me explain why:

For a simple image operation, suppose we have an image in memory and we wants to perform an invert operation. In Emgu CV 2.x, we can write the code as follows:

Image<Gray, Byte> image = ... //load the image from some where
Image<Gray, Byte> imageInvert = new Image<Gray, Byte>(image.Width, image.Height);
CvInvoke.cvNot(image, imageInvert);

In Emgu CV 3.x, we can still use the Image<Gray, Byte> class to perform the same operation, with a slight change in the CvInvoke function name

Image<Gray, Byte> image = ... //load the image from some where
Image<Gray, Byte> imageInvert = new Image<Gray, Byte>(image.Width, image.Height);
CvInvoke.BitwiseNot(image, imageInvert);

To realize the true potential with T-API, let's try to use UMat to perform the same operation

UMat image = ... //load the image from some where
UMat imageInvert = new UMat();
CvInvoke.BitwiseNot(image, imageInvert);

It all seems to be not much different from the code that use the Image<,> class in 3.0. However, the above code can automatically use OpenCL engine to perform the operation if a suitable OpenCL device is found. That means it will run many times faster on a system with a discrete GPU (Nvidia, AMD, Intel Iris Pro etc). On systems that do not have a OpenCL devices, the code will be run on CPU and have the same performance as if we are passing the Image<,> or Mat objects to the CvInvoke function.

In short, T-API enable developer to automatically use the OpenCL devices (GPU) for computing and automatically fall back to CPU in the absent of OpenCL devices. You can also turn the OpenCL engine off by simply setting

CvInvoke.UseOpenCL = false

In which case all the code will be run on CPU instead.

The T-API is the motivation for us to rewrite all our code using the OpenCV C++ interface to take advantage of this future. We believe it is well worth the effort once we see the results.