Talk:CSharp OpenCV

From Ilianko
Revision as of 13:26, 3 February 2016 by Anko (talk | contribs)

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 Emgu.CV;
using Emgu.Util;
using Emgu.CV.Structure;
using System.Windows.Threading;

namespace emgu
{
    public partial class Form1 : Form
    {
        private Capture cap = new Capture();
        Image<Bgr, byte> frame;
        private DispatcherTimer timer = new DispatcherTimer();
    
        public Form1()
        {
            InitializeComponent();
        }

        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();
        }

        private Rectangle[] faces;
        private Rectangle[] eyes;
        private Rectangle eyeL;
        private Image<Gray, byte> gray;
        
        private CascadeClassifier faceDetect = new CascadeClassifier("haar.xml");
        private CascadeClassifier eyeDetect = new CascadeClassifier("haarcascade_eye.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);



            List< Bgr> colors = new List<Bgr>();

            colors.Add( new Bgr(255, 0, 0));colors.Add( new Bgr(255, 255, 0));colors.Add( new Bgr(255, 0, 255));
            var i = 0;
            foreach (var face in faces)
            {
                gray.ROI = face;
                frame.Draw(face, colors[i], 3); i++;
                if (i > 2) i = 0;

                eyes = eyeDetect.DetectMultiScale(gray, 1.2, 2, min, max);
                foreach (var eye in eyes)
                {
                    eyeL = eye;
                    eyeL.X += face.X;
                    eyeL.Y += face.Y;
                    frame.Draw(eyeL, new Bgr(0, 0, 0), 3); i++;
               
                }

            }

            pictureBox1.Image = frame.Bitmap;
        }


        private void pictureBox1_Click(object sender, EventArgs e)
        {

        }

    }
}