CSharp OpenCV
From Ilianko
Revision as of 11:02, 12 February 2014 by Anko (talk | contribs) (→Генериране събитие на определено време)
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 Capture cap = new Capture();
private Image<Bgr, byte> frame;
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;
pictureBox1.Image = frame.Bitmap;
timer.Tick += new EventHandler(timer_tick);
timer.Interval = new TimeSpan(0, 0, 0, 0, 10);
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 xxx = new CascadeClassifier(@"haarcascade_frontalface_default.xml");
void timer_tick(object sender, EventArgs e)
{
frame = cap.QueryFrame();
gray = frame.Convert<Gray, Byte>();
faces = frame.DetectMultiScale(grayFrame, 1.2, 2, Size(20,20), Size(300,300));
foreach (var face in faces)
frame.Draw(face, new Bgr(0, double.MaxValue, 0), 3);
pictureBox1.Image = frame.Bitmap;
}
faces = frame.DetectMultiScale(grayFrame, 1.2, 2, min, max);
i = 0;
}
i++;
foreach (var face in ccc)
currentFrame.Draw(face, new Bgr(0, double.MaxValue, 0), 3);
pictureBox1.Image = currentFrame.Bitmap;
За
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public Rectangle[] ccc;
private CascadeClassifier xxx = new CascadeClassifier(@"haarcascade_frontalface_default.xml");
private Size min = new Size(20,20);
private Size max = new Size(300,300);
private DispatcherTimer timer = new DispatcherTimer();
private void Form1_Load(object sender, EventArgs e)
{
Image<Bgr, Byte> currentFrame = cap.QueryFrame();
this.Size = currentFrame.Size;
timer.Tick += new EventHandler(timer_Tick);
timer.Interval = new TimeSpan(0, 0, 0, 0, 35);
timer.Start();
}
int i = 0;
void timer_Tick(object sender, EventArgs e)
{
Image<Bgr, Byte> currentFrame = cap.QueryFrame();
Image<Gray, Byte> grayFrame = currentFrame.Convert<Gray, Byte>();
if (i > 35 || i == 0)
{
ccc = xxx.DetectMultiScale(grayFrame, 1.2, 2, min, max);
i = 0;
}
i++;
foreach (var face in ccc)
currentFrame.Draw(face, new Bgr(0, double.MaxValue, 0), 3);
pictureBox1.Image = currentFrame.Bitmap;
}
}
}
4.