建立C#的WinForm程序,添加一个pictureBox组件,一个timer组件,timer响应函数为timer1_Tick()。工程代码如下:
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Text.RegularExpressions;
using System.IO.Ports;
using System.IO;
using System.Runtime.InteropServices;
using System.Drawing.Imaging;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using Emgu.CV;
using Emgu.Util;
using Emgu.CV.ML;
using Emgu.CV.UI;
using Emgu.CV.GPU;
using Emgu.CV.OCR;
using Emgu.CV.Structure;
using Emgu.CV.ML.Structure;
namespace ProjectName
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
timer1.Enabled = true;
timer1.Interval = 1000;//时间间隔设置为1秒
}
Emgu.CV.Capture capture = new Emgu.CV.Capture();
private void timer1_Tick(object sender, EventArgs e)
{
Image<Bgr, Byte> frame = capture.QueryFrame();//获取摄像头数据
frame=frame.Flip(Emgu.CV.CvEnum.FLIP.HORIZONTAL);
Image<Gray, Byte> gray = frame.Convert<Gray, Byte>(); //Convert it to Grayscale
//gray._EqualizeHist();//均衡化
CascadeClassifier face = new CascadeClassifier("数据库xml文件路径");//加载xml文件,识别人手或人脸
Rectangle[] facesDetected = face.DetectMultiScale(gray, 1.1, 10, new Size(20, 20), Size.Empty);//寻找图片中人脸并加检测框
foreach (Rectangle f in facesDetected)
{
frame.Draw(f, new Bgr(Color.Red), 3);//绘制检测框
}
pictureBox1.Image = frame.ToBitmap();
}
}
}
这实现了一个简单的从摄像头获取图片数据并将其中关心的部分框出的功能,利用了OpenCv提供的学习机方法。对于摄像头更多的处理方法可参考:
http://blog.csdn.net/hbqhdlc/article/details/6848063
文中利用了WindowsAPI的函数功能,实现摄像。对于人手识别,不像人脸识别这么普遍,可参考:
http://www.360doc.com/content/11/1220/16/5087210_173660914.shtml
相应xml文件链接也在文中提供。