📜 ⬆️ ⬇️

Getting started with OpenCV and its use in C #

I want to tell you a little about OpenCV technology and its application in the C # programming language.

OpenCV (English Open Source Computer Vision Library, open source computer vision library) is a library of computer vision, image processing and general-purpose numerical algorithms with open source. Implemented in C / C ++, also being developed for Python, Java, Ruby, Matlab, Lua and other languages. Can be freely used for academic and commercial purposes - distributed under the terms of a BSD license.

I came across this library just recently. On the website toster.ru I asked a question about unusual topics for theses and in one of the answers I received links to the result of working with this library. Here are some of them:
')
www.youtube.com/watch?v=h9kPI7_vhAU
www.youtube.com/watch?v=256bg5_vNvg
www.youtube.com/watch?v=PUhwGTSNGhI

To get started, you need to download the files we need from www.emgu.com/wiki/index.php/Main_Page .

Emgu CV is a cross-platform .Net add-on for the OpenCV image processing library. Designed to work with .NET compatible languages ​​such as C #, VB, VC ++, IronPython, etc., can be used in Visual Studio, Xamarin, works with Windows, Linux, Mac OS X, IOS, Android and Windows Phone.

Once we have downloaded and installed the latest version, you can get to work. In this example, we will find and recognize the face and eyes of a person.

For this we need ready-made xml-files, which contain all the information we need. As mentioned, OpenCV is an Open Source project, so if you wish, you can find many ready-made solutions.

For work we will connect the relevant libraries:

using Emgu.CV; using Emgu.CV.CvEnum; using Emgu.CV.Structure; using Emgu.CV.Cvb; 

First we need to create an instance of the Capture class:

 Capture capture = new Capture(); 

This object will be responsible for intercepting the video stream.

To create an object that our face or eyes recognize, we will use the wonderful HaarCascade class. The classifier (namely, the cascade of forced classifiers working with Haar-like functions) is trained with several hundred samples of species of a particular object (for example, a person or a car), calls positive examples that scale to the same size (say, 20x20), and negative examples - arbitrary images of the same size.

That is, when creating an instance of the HaarCascade class in its constructor, we specify a link to the xml file that contains the data we need.

 //for face HaarCascade faceCascade = new HaarCascade("haarcascade_frontalface_alt.xml"); //for eye HaarCascade eyeCascade = new HaarCascade("haarcascade_eye.xml"); 

In order to work further with the intercepted frame, you need to use the QueryFrame () method.

 Image<Bgr, Byte> image = capture.QueryFrame(); 

We receive our frame, which is transmitted to us by our device (webcam). Next, we find the image is translated in gray.

 Image<Gray, Byte> grayImage = image.Convert<Gray, Byte>(); 

Now we need to find the attributes that belong to our face and eyes.

 var Face = grayImage.DetectHaarCascade(faceCascade)[0]; foreach (var face in Face) { //  ,     ,      . image.Draw(face.rect, new Bgr(255, 255, 255), 10); } //    var Eye = grayImage.DetectHaarCascade(eyeCascade)[0]; foreach (var eye in Eye) { image.Draw(eye.rect, new Bgr(0, 0, 255), 3); } 

Well, almost everything, it remains only somewhere to display the image we received. For this we will use the imageBox control (it must be added to the manual).

 imageBoxEyeAndFaceDetector.Image = image; 

Now we can set our work when we need it, for example, at the touch of a button.

 Application.Idle += Method; 

Download link for xml files: ifolder.com.ua/z3fihkcet19n.html

I also advise you to view this video for a general understanding of the work of machine vision www.youtube.com/watch?v=TyEfJyJA7gQ .

Good luck with your work.

Links


www.emgu.com/wiki/index.php/Main_Page
ru.wikipedia.org/wiki/OpenCV
devnuances.com

Source: https://habr.com/ru/post/260741/


All Articles