CSharp OpenCV
OpenCV is released under a BSD license and hence it’s free for both academic and commercial use. It has C++, C, Python and Java interfaces and supports Windows, Linux, Mac OS, iOS and Android. OpenCV was designed for computational efficiency and with a strong focus on real-time applications. Written in optimized C/C++, the library can take advantage of multi-core processing. Enabled with OpenCL, it can take advantage of the hardware acceleration of the underlying heterogeneous compute platform. Adopted all around the world, OpenCV has more than 47 thousand people of user community and estimated number of downloads exceeding 7 million. Usage ranges from interactive art, to mines inspection, stitching maps on the web or through advanced robotics.
Contents
Конфигуриране на OpenCV със C#.
- Инсталиране на EMGU wrapper
- Инсталиране (Repair) на Microsoft Visual C++ 2010 Redistributable Package (x86)
Създаване на проект в C# и добавяне на необходимите библиотеки
- Start->All Programs->MS VS2010 Express -> MS VS2010 Express
- File->New Project->Windows Forms Application->OK
- File->Save
- References->Add Reference->Browse->C:/EMGU->emgu...->bin->Emgu.CV.dll,Emgu.CV.UI.dll, Emgu.Utils.dll
- References->Add Reference->.Net->WindowsBase->OK
- C:/EMGU->emgu...->bin->x86->Object of type - exe, dll, ocx) -> преместват се се всички в директорията debug на приложението
- Desktop->nvcuda.dll премества се в директорията debug на приложението.
Добавяне на namespaces
Освен автоматично генерираните
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
Трябва да се добавят използваните от openCV
using Emgu.CV;
using Emgu.Util;
using Emgu.CV.Structure;
using System.Windows.Threading;
Проверка
private Capture cap = new Capture();
Добавяне PictureBox
Визуализиране на кадър
private Capture cap = new Capture();
Image<Bgr, byte> frame;
private void Form1_Load(object sender, EventArgs e)
{
this.Size = new System.Drawing.Size(100, 100);
frame = cap.QueryFrame();
this.Size = frame.Size;
pictureBox1.Image = frame.Bitmap;
}
Генериране събитие на определено време
private DispatcherTimer timer = new DispatcherTimer();
private void Form1_Load(object sender, EventArgs e)
{
this.Size = new System.Drawing.Size(100, 100);
frame = cap.QueryFrame();
this.Size = frame.Size;
timer.Tick += new EventHandler(timer_tick);
timer.Interval = new TimeSpan(0, 0, 0, 0, 35);
timer.Start();
}
void timer_tick(object sender, EventArgs e)
{
frame = cap.QueryFrame();
pictureBox1.Image = frame.Bitmap;
}
Добавяне алгоритъм за разпознаване на лица
Към timer_tick ще се добави функционалност за търсене на лица
private Rectangle[] faces;
private Image<Gray, byte> gray;
private CascadeClassifier faceDetect = new CascadeClassifier("haar.xml");
private Size min = new Size(20, 20);
private Size max = new Size(300, 300);
void timer_tick(object sender, EventArgs e)
{
frame = cap.QueryFrame();
gray = frame.Convert<Gray, byte>();
faces = faceDetect.DetectMultiScale(gray, 1.2, 2, min, max);
foreach (var face in faces)
frame.Draw(face, new Bgr(0, double.MaxValue, 0), 3);
pictureBox1.Image = frame.Bitmap;
}