Opencv Emgu C# 人脸提取

发布于 2024-09-27 21:51:42 字数 2852 浏览 6 评论 0原文

到目前为止,我已经设法使我的程序正常工作并检测面部,现在我想做的是提取检测到的区域并显示在我添加的新图片框中,完成此操作后我想将图片添加到数据库中,将检测到的面孔与它们进行比较。 所以请帮忙

 private void ProcessFrame(object sender, EventArgs arg)
    {
        Image<Bgr, Byte> frame = _capture.QueryFrame();                       
        Image<Gray, Byte> gray = frame.Convert<Gray, Byte>(); //Convert it to Grayscale

         //normalizes brightness and increases contrast of the image
        gray._EqualizeHist();

        //Read the HaarCascade objects
        HaarCascade face = new HaarCascade("haarcascade_frontalface_alt_tree.xml");
        HaarCascade eye = new HaarCascade("haarcascade_eye.xml");

        //Detect the faces  from the gray scale image and store the locations as rectangle
        //The first dimensional is the channel
        //The second dimension is the index of the rectangle in the specific channel
        MCvAvgComp[][] facesDetected = gray.DetectHaarCascade(
           face,
           1.1,
           10,
           Emgu.CV.CvEnum.HAAR_DETECTION_TYPE.DO_ROUGH_SEARCH,
           new Size(20, 20));

        foreach (MCvAvgComp f in facesDetected[0])
        {
            //draw the face detected in the 0th (gray) channel with blue color
            frame.Draw(f.rect, new Bgr(Color.Blue), 2);
            /*
            //Set the region of interest on the faces
            gray.ROI = f.rect;
            MCvAvgComp[][] eyesDetected = gray.DetectHaarCascade(
               eye,
               1.1,
               10,
               Emgu.CV.CvEnum.HAAR_DETECTION_TYPE.DO_ROUGH_SEARCH,
               new Size(20, 20));
            gray.ROI = Rectangle.Empty;

            foreach (MCvAvgComp e in eyesDetected[0])
            {
                Rectangle eyeRect = e.rect;
                eyeRect.Offset(f.rect.X, f.rect.Y);
                frame.Draw(eyeRect, new Bgr(Color.Red), 2);

            }*/
        }

        pictureBox1.Image = frame.ToBitmap();Application.DoEvents();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        #region if capture is not created, create it now
        if (_capture == null)
        {
            try
            {
                _capture = new Capture();
            }
            catch (NullReferenceException excpt)
            {
                MessageBox.Show(excpt.Message);
            }
        }
        #endregion

        if (_capture != null)
        {
            if (_captureInProgress)
            {  //stop the capture
                button1.Text = "Start Capture";
                Application.Idle -= ProcessFrame;
            }
            else
            {
                //start the capture
                button1.Text = "Stop";
                Application.Idle += ProcessFrame;
            }

            _captureInProgress = !_captureInProgress;
        }
    }

So far I have managed to make my programme to work and detect face and now what i want to do is that extract the area detected and display in new picture box that i have added also after this is done i want to add pics to database and compare detected faces with them.
So please help

 private void ProcessFrame(object sender, EventArgs arg)
    {
        Image<Bgr, Byte> frame = _capture.QueryFrame();                       
        Image<Gray, Byte> gray = frame.Convert<Gray, Byte>(); //Convert it to Grayscale

         //normalizes brightness and increases contrast of the image
        gray._EqualizeHist();

        //Read the HaarCascade objects
        HaarCascade face = new HaarCascade("haarcascade_frontalface_alt_tree.xml");
        HaarCascade eye = new HaarCascade("haarcascade_eye.xml");

        //Detect the faces  from the gray scale image and store the locations as rectangle
        //The first dimensional is the channel
        //The second dimension is the index of the rectangle in the specific channel
        MCvAvgComp[][] facesDetected = gray.DetectHaarCascade(
           face,
           1.1,
           10,
           Emgu.CV.CvEnum.HAAR_DETECTION_TYPE.DO_ROUGH_SEARCH,
           new Size(20, 20));

        foreach (MCvAvgComp f in facesDetected[0])
        {
            //draw the face detected in the 0th (gray) channel with blue color
            frame.Draw(f.rect, new Bgr(Color.Blue), 2);
            /*
            //Set the region of interest on the faces
            gray.ROI = f.rect;
            MCvAvgComp[][] eyesDetected = gray.DetectHaarCascade(
               eye,
               1.1,
               10,
               Emgu.CV.CvEnum.HAAR_DETECTION_TYPE.DO_ROUGH_SEARCH,
               new Size(20, 20));
            gray.ROI = Rectangle.Empty;

            foreach (MCvAvgComp e in eyesDetected[0])
            {
                Rectangle eyeRect = e.rect;
                eyeRect.Offset(f.rect.X, f.rect.Y);
                frame.Draw(eyeRect, new Bgr(Color.Red), 2);

            }*/
        }

        pictureBox1.Image = frame.ToBitmap();Application.DoEvents();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        #region if capture is not created, create it now
        if (_capture == null)
        {
            try
            {
                _capture = new Capture();
            }
            catch (NullReferenceException excpt)
            {
                MessageBox.Show(excpt.Message);
            }
        }
        #endregion

        if (_capture != null)
        {
            if (_captureInProgress)
            {  //stop the capture
                button1.Text = "Start Capture";
                Application.Idle -= ProcessFrame;
            }
            else
            {
                //start the capture
                button1.Text = "Stop";
                Application.Idle += ProcessFrame;
            }

            _captureInProgress = !_captureInProgress;
        }
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

我很OK 2024-10-04 21:51:42

我已经设法做到了这一点,但我放置此答案是为了帮助其他遇到同样麻烦的人

gray.ROI = f.rect;

,然后您可以将灰色分配给任何图片框,这将仅显示检测到的区域

I have managed to do that but i am placing this answer to help others if they are in same trouble

gray.ROI = f.rect;

and then you can assign gray to any picture box this will show only detected area

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文