Intro To EmguCV

Common Features Within EmguCV


Although Python’s OpenCV library is an extensive library with numerous functions and methods that allow the user to accomplish virtually anything, the library does have its limitations. Originally released in C++, OpenCV has drastically changed over time, with supported languages growing to Python and Java.

Over time, due to its increasing popularity, OpenCV has had numerous wrappers created to allow its libraries to be implemented in a verity of other languages. One of the most common, and the wrapper that we will be using today, is EmguCV.

Written in C#, EmguCV utilizes almost all of the functions from OpenCV, and is very helpful when wanting to implement machine learning and computer vision into the .NET framework. In the following blog post, I’ll detail how to get started within EmguCV, and some of its primary overall functionality.

After downloading and declaring the EmguCV Library or Nuget Package in Visual Studio, we will want to create an empty Mat object to store any information in that we deem fit. To create an empty Mat object, you can use the following code below.

Mat img = new Mat();

Next, we want to load an image into our program, so we can use it and manipulate it to our specifications. To load in an image, we use the following code below, and storing the information in the Mat object we created above.

img = CvInvoke.Imread("img.jpg", ImreadModes.AnyColor);

In the line above, we specified the image to be read in as AnyColor, or a Bgr image. This is ideal because, if we ever need to change the format of the image later, we can do so, while still having the original image to reference to.

Although the above way is a simple way to load in an image, it can be done all on one line with combining the code above.

Mat img = CvInvoke.Imread("img.jpg", ImreadModes.AnyColor);

Although the Mat object is the ideal way we want to transfer and convert data within an image, there are some older algorithms within EmguCV, that require an Image format instead of a Mat. To implement this, use the following code.

//Declaring an empty Image
Image<Bgr, byte> img = new Image<Bgr, byte>();

//Converting a Mat object to an Image
Mat img = new Mat();
Image<Bgr, byte> newImg = img.ConvertTo<Bgr, byte>();

Again, we are loading in the image in Bgr mode, but if necessary, we can declare it in grayscale instead.

//Declaring an empty Image
Image<Gray, byte> img = new Image<Gray, byte>();

//Converting a Mat object to an Image
Mat img = new Mat();
Image<Gray, byte> newImg = img.ConvertTo<Gray, byte>();

If you want to convert an Image to a Mat object, you can use the following code.

Image<Gray, byte> img = new Image<Gray, byte>();
Mat img2 = img.Mat;

To make life easier when converting an image to grayscale, we can use the CvInvoke class within EmguCV, and convert a Mat object to grayscale, instead of coverting it to an Image first, then converting it.

//Read in the image and store it in a Mat
Mat img = CvInvoke.Imread("img.jpg", ImreadModes.AnyColor);

//This Mat object is used to store the output
Mat output = new Mat();

//The CvInvoke method that will convert the image to grayscale
CvInvoke.CvtColor(img, output, ColorConversion.Bgr2Gray);

Although EmguCV can be difficult to grasp at first, once you can learn the basic functions within the wrapper, the library becomes very easy to use. The classes and functions used within EmguCV are ripped directly from OpenCV, and allow for easier implementation of computer vision algorithms within the .NET framework.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: