Introduction to Emgu EE4H, M.Sc Computer Vision Dr. Mike Spann
About Emgu All of this material is taken from an Emgu wiki tutorial Also check out the API documentation dex.html dex.html Emgu is a cross platform.NET wrapper to the OpenCV image processing library Written entirely in C# but Allowing OpenCV functions to be called from.NET compatible languages such as C#, VB, VC++, IronPython
Setting up Emgu – C# There is a nice wiki article about how to set up Emgu using C# GU_C_Sharp GU_C_Sharp Also the article presents a tutorial on writing a simple Emgu application
Emgu to OpenCV mapping Can easily map from Emgu to OpenCV Function mapping Emgu.CV.CvInvoke Structure mapping Emgu.CV.Structure.Mxxx Enumeration mapping Emgu.CV.CvEnum Example: IntPtr image = CvInvoke.cvLoadImage("myImage.jpg", LOAD_IMAGE_TYPE.CV_LOAD_IMAGE_GRAYSCALE);
Emgu to OpenCV mapping Emgu also borrows some existing structures in.NET to represent structures in OpenCV:.NET StructureOpenCV structure System.Drawing.PointCvPoint System.Drawing.PointFCvPoint2D32f System.Drawing.SizeCvSize System.Drawing.RectangleCvRect
Image representation in Emgu Normal to create an image using the genric class Image object instead of using CvInvoke.cvCreateImage() Several advantages of this Memory is automatically released by the garbage collector Image class can be examined by debugger Image class contains advanced methods that is not available on OpenCV such as conversion to bitmaps
Image representation in Emgu Can create an un-initialized graylevel image of a given size Or can initialize the pixel data Similarly for an RGB image Image img1 = new Image (400, 300); Image img2 = new Image (400, 300, new Gray(30)); Image img3 = new Image (400,300, new Bgr(100, 100, 100));
Image representation in Emgu Images can also created from a file or a bitmap Image img1 = new Image ("MyImage.jpg"); Image img3 = new Image (bmp);
Getting or Setting Pixels in Emgu The slow way is to use the [] operator on the image object Faster way is to use the Data property of the image object pixels-of-an-image-with-emgu-cv gives some benchmarking for iterating over pixel data pixels-of-an-image-with-emgu-cv // Get the gray level Gray g = img[y, x]; // Set the gray level img[y,x] = new Gray(100); Byte g = img.Data[y, x, 0];
Displaying, drawing and converting images An image can be displayed using the ImageBox control ImageBox is a high performance control for displaying images. Displays a Bitmap that shares memory with the Image object, therefore no memory copy is needed (very fast). The Image class has a ToBitmap() function that return a Bitmap object Allows the image to be displayed on a PictureBox control There is also an ImageViewer class in the Emgu.CV.UI namespace
Displaying, drawing and converting images The Draw() method in Image can be used to draw different types of objects, including fonts, lines, circles, rectangles, boxes, ellipses as well as contours More efficient method is to draw as a graphics overlay Image image = new Image (400, 300); Rectangle rect =new Rectangle(0,0,200,300); image.Draw(rect, new Gray(200), 2); Image image = new Image (400, 300); Rectangle rect =new Rectangle(0,0,200,300); Graphics.FromImage(image). DrawRectangle(new Pen(yellow), rect);
Conclusions Emgu is a powerful development platform for image processing/computer vision Similar capabilities to OpenCV but has additional capabilities because of additional language features specific to C#