使用Emgucv进行手检测的Haar Cascade

时间:2014-05-23 15:51:07

标签: c# image-processing emgucv haar-classifier

我正在研究用于手势识别的haar级联,当我为单个gexture训练xml并在Emgucv C#中使用它时运行良好没有滞后但是当我使用xml并使用xml时它变得滞后.... 这是代码:

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;
using System.Threading;
using Emgu.CV;
using Emgu.CV.Structure;
using Emgu.Util;
using Emgu.CV.CvEnum;
namespace VideoCaptureDemo
{
    public partial class Form1 : Form
    {
        private Capture _capture = null;
        Image<Bgr, Byte> frame,roi_i;
        int threshold_value = 145; //0-255
        public HaarCascade haar=null;

        int cam = 0;

        public Form1()
        {
            InitializeComponent();
        }

        private void ReleaseData()
        {
            if (_capture != null)
                _capture.Dispose();
        }

        private void ProcessFrame(object sender, EventArgs arg)
        {
            try
            {

                frame = _capture.QueryFrame();

                if (frame != null)
                {
                         // var faces = frame.DetectHaarCascade(haar)[0];
                    Image<Gray, byte> grayframe = frame.Convert<Gray, byte>();
                    var faces = haar.Detect(grayframe, 1.4, 4, HAAR_DETECTION_TYPE.DO_CANNY_PRUNING, new Size(frame.Width / 8, frame.Height / 8), new Size(frame.Width, frame.Height));
                    var pra = faces;
                    foreach (var face in faces)
                    {
                        frame.Draw(face.rect, new Bgr(255, double.MaxValue, 0), 2);
                        //roi_i.ROI = face.rect;
                    }
                    pictureBox1.Image = frame.ToBitmap();
                    roi_i = frame;
                    foreach (var face1 in pra)
                    {

                        frame.Draw(face1.rect, new Bgr(255, double.MaxValue, 0), 2);
                        frame.ROI = face1.rect;
                    }
                    /*Hsv lowerLimit = new Hsv(0, 50, 100);
                    Hsv upperLimit = new Hsv(250, 150, 100);

                    Image<Hsv, Byte> HsvFrame = frame.Convert<Hsv, byte>();
                    HsvFrame = HsvFrame.SmoothGaussian(5, 5, 0.1, 0.1);
                    Image<Gray, byte> InRageFrame = HsvFrame.InRange(lowerLimit, upperLimit);*/
               //     InRageFrame = InRageFrame.SmoothGaussian(3,3,1,1);
                    Image<Gray,Byte> img= frame.Convert<Gray, Byte>();
                    img = img.ThresholdBinary(new Gray(threshold_value), new Gray(255));
                    pictureBox2.Image = img.ToBitmap(); //display results in different picturebox
                   // pictureBox2.Image = InRageFrame.ToBitmap();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message.ToString());
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (button1.Text == "Play")
                {
                    #region cameracapture
                      try
                        {

                            _capture = null;
                            _capture = new Capture(0);
                            haar = new HaarCascade("C:\\Users\\Adeel\\Downloads\\hand.xml");
                            if(haar==null)MessageBox.Show("aasasas");
                            _capture.SetCaptureProperty(Emgu.CV.CvEnum.CAP_PROP.CV_CAP_PROP_FPS, 30);
                            _capture.SetCaptureProperty(Emgu.CV.CvEnum.CAP_PROP.CV_CAP_PROP_FRAME_HEIGHT, 240);
                            _capture.SetCaptureProperty(Emgu.CV.CvEnum.CAP_PROP.CV_CAP_PROP_FRAME_WIDTH, 320);
                            Application.Idle += ProcessFrame;
                            button1.Text = "Stop";

                        }
                        catch (NullReferenceException excpt)
                        {
                            MessageBox.Show(excpt.Message);
                        }
                    #endregion cameracapture
                }
                else
                    #region stopcapture
                    if (button1.Text == "Stop")
                    {
                        _capture.Stop();

                        Application.Idle -= ProcessFrame;
                        ReleaseData();
                        button1.Text = "Play";
                        pictureBox1.Image = Properties.Resources.blank;
                        pictureBox2.Image = Properties.Resources.blank;
                        if (cam == 1)
                        {
                            _capture.Dispose();
                            cam = 0;
                        }
                    }
                    #endregion stopcapture
        }
        private void close_Click(object sender, EventArgs e)
        {
            ReleaseData();
            this.Close();
        }

        private void label1_Click(object sender, EventArgs e)
        {

        }


    }
}

任何代码都比它更好或链接plz ......

0 个答案:

没有答案