12
Introduction to Emgu EE4H, M.Sc 04 24086 Computer Vision Dr. Mike Spann [email protected] http://www.eee.bham.ac.uk/spannm

Introduction to Emgu EE4H, M.Sc 04 24086 Computer Vision Dr. Mike Spann [email protected]

Embed Size (px)

Citation preview

Page 1: Introduction to Emgu EE4H, M.Sc 04 24086 Computer Vision Dr. Mike Spann m.spann@bham.ac.uk

Introduction to Emgu

EE4H, M.Sc 04 24086 Computer VisionDr. Mike Spann

[email protected]://www.eee.bham.ac.uk/spannm

Page 2: Introduction to Emgu EE4H, M.Sc 04 24086 Computer Vision Dr. Mike Spann m.spann@bham.ac.uk

About EmguAll of this material is taken from an Emgu wiki

tutorial

http://www.emgu.com/wiki/index.php/Tutorial

Also check out the API documentation

http://www.emgu.com/wiki/files/2.4.2/document/Index.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

Page 3: Introduction to Emgu EE4H, M.Sc 04 24086 Computer Vision Dr. Mike Spann m.spann@bham.ac.uk

Setting up Emgu – C#There is a nice wiki article about how to set

up Emgu using C#http://www.emgu.com/wiki/index.php/

Setting_up_EMGU_C_Sharp Also the article presents a tutorial on

writing a simple Emgu application

Page 4: Introduction to Emgu EE4H, M.Sc 04 24086 Computer Vision Dr. Mike Spann m.spann@bham.ac.uk

Emgu to OpenCV mappingCan 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);

Page 5: Introduction to Emgu EE4H, M.Sc 04 24086 Computer Vision Dr. Mike Spann m.spann@bham.ac.uk

Emgu to OpenCV mappingEmgu 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

Page 6: Introduction to Emgu EE4H, M.Sc 04 24086 Computer Vision Dr. Mike Spann m.spann@bham.ac.uk

Image representation in EmguNormal to create an image using the genric

class Image<TColor, TDepth> object instead of using CvInvoke.cvCreateImage()

Several advantages of thisMemory is automatically released by the

garbage collectorImage<TColor, TDepth > class can be

examined by debuggerImage<TColor, TDepth > class contains

advanced methods that is not available on OpenCV such as conversion to bitmaps

Page 7: Introduction to Emgu EE4H, M.Sc 04 24086 Computer Vision Dr. Mike Spann m.spann@bham.ac.uk

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<Gray, Byte> img1 = new Image<Gray, Byte>(400, 300);

Image<Gray, Byte> img2 = new Image<Gray, Byte>(400, 300, new Gray(30));

Image<Bgr, Byte> img3 = new Image<Bgr, Byte>(400,300, new Bgr(100, 100, 100));

Page 8: Introduction to Emgu EE4H, M.Sc 04 24086 Computer Vision Dr. Mike Spann m.spann@bham.ac.uk

Image representation in EmguImages can also created from a file or a

bitmapImage<Bgr, Byte> img1 = new Image<Bgr, Byte>("MyImage.jpg");

Image<Bgr, Byte> img3 = new Image<Bgr, Byte>(bmp);

Page 9: Introduction to Emgu EE4H, M.Sc 04 24086 Computer Vision Dr. Mike Spann m.spann@bham.ac.uk

Getting or Setting Pixels in EmguThe slow way is to use the [] operator on the image

object

Faster way is to use the Data property of the image object

http://stackoverflow.com/questions/5101986/iterate-over-pixels-of-an-image-with-emgu-cv gives some benchmarking for iterating over pixel data

// Get the gray levelGray g = img[y, x];

// Set the gray levelimg[y,x] = new Gray(100);

Byte g = img.Data[y, x, 0];

Page 10: Introduction to Emgu EE4H, M.Sc 04 24086 Computer Vision Dr. Mike Spann m.spann@bham.ac.uk

Displaying, drawing and converting images

An image can be displayed using the ImageBox controlImageBox 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 objectAllows the image to be displayed on a PictureBox control

There is also an ImageViewer class in the Emgu.CV.UI namespace

Page 11: Introduction to Emgu EE4H, M.Sc 04 24086 Computer Vision Dr. Mike Spann m.spann@bham.ac.uk

Displaying, drawing and converting images

The Draw() method in Image< Color,Depth> 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<Gray, Byte> image = new Image<Gray, Byte>(400, 300);Rectangle rect =new Rectangle(0,0,200,300);image.Draw(rect, new Gray(200), 2);

Image<Gray, Byte> image = new Image<Gray, Byte>(400, 300);Rectangle rect =new Rectangle(0,0,200,300);Graphics.FromImage(image). DrawRectangle(new Pen(yellow), rect);

Page 12: Introduction to Emgu EE4H, M.Sc 04 24086 Computer Vision Dr. Mike Spann m.spann@bham.ac.uk

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#