使用EmguCv HaarCascade测量特定的身体部位

时间:2014-01-15 11:51:13

标签: c# winforms emgucv

我正在尝试开发一个测量用户肩宽的程序,我遇到了一些我不知道如何解决的问题。

  1. 当我使用笔记本电脑的网络摄像头拍摄用户的全身照片时,系统会给出不正确的测量结果
  2. 我尝试使用相同的距离等捕获相同用户的3张图像,但是当我开始测量它们时,它会给出不一致的测量结果。
  3. 这是我的代码:

        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.CV.Structure;
        using Emgu.Util;
    
        namespace fitting
        {
            public partial class Form1 : Form
            {
                HaarCascade UpperBody = new HaarCascade("haarcascade_mcs_upperbody.xml");
                HaarCascade LowerBody = new HaarCascade("haarcascade_lowerbody.xml");
            Capture camera;
            bool captureProcess = false;
            Image<Bgr, Byte> img;
    
            public Form1()
            {
                InitializeComponent();
            }
    
            void viewImage(object sender, EventArgs e)
            {
                img = camera.QueryFrame();
                if (img == null)
                    return;
                CamImageBox.Image = img;
            }
    
            private void btnCapture_Click(object sender, EventArgs e)
            {
                if (captureProcess == true)
                {
                    string data;
    
                    Application.Idle -= viewImage;
                    captureProcess = false;
                    SaveFileDialog dlg = new SaveFileDialog();
                    if (dlg.ShowDialog() == DialogResult.OK)
                    {
                        img.ToBitmap().Save(dlg.FileName + ".bmp", System.Drawing.Imaging.ImageFormat.Bmp);
                        data = dlg.FileName + ".bmp";
                    }
                    measureImage();
                }
            }
    
            void measureImage()
            {
                OpenFileDialog dlg2 = new OpenFileDialog();
                dlg2.Filter = "Image|*.Bmp";
                if (dlg2.ShowDialog() == DialogResult.OK)
                {
                    Image<Bgr, Byte> frame = new Image<Bgr, byte>(dlg2.FileName);
                    Image<Gray, Byte> Gray_Frame = frame.Convert<Gray, Byte>();
    
    
    /////////////////////////LOWER BODY DETECTION////////////////////////////////
                    MCvAvgComp[][] LowerBodyDetect = Gray_Frame.DetectHaarCascade(
                        LowerBody,
                        1.985603925968,
                        0,
                        Emgu.CV.CvEnum.HAAR_DETECTION_TYPE.DO_CANNY_PRUNING,
                        new Size());
    
    ///////////////////HERE IS THE UPPER BODY DETECTION/////////////////////////
    
                    MCvAvgComp[][] UpperBodyDetect = Gray_Frame.DetectHaarCascade(
                        UpperBody,
                        1.3,
                        5,
                        Emgu.CV.CvEnum.HAAR_DETECTION_TYPE.DO_CANNY_PRUNING,
                        new Size());
    
    
    /////////////////////////DRAWING OF RECTANGLE ON DETECTED UPPER BODY///////////////////
                    try
                    {
                        frame.Draw(UpperBodyDetect[0][0].rect, new Bgr(Color.Red), 2);
                        double width = (UpperBodyDetect[0][0].rect.Width);
                        textBox1.Text = (Convert.ToString(width));
                    }
                    catch (Exception e)
                    {
                        MessageBox.Show(e.Message);
                    }
    ///////////////////////DRAWING OF RECTANGLE ON DETECTED LOWER BODY///////////////////
                    try
                    {
                        frame.Draw(LowerBodyDetect[0][0].rect, new Bgr(Color.Green), 2);
                    }
                    catch (Exception e)
                    {
                        MessageBox.Show(e.Message);
                    }
                    CamImageBox.Image = frame;
                }
            }
    
            private void Form1_Load(object sender, EventArgs e)
            {
                bool useCam = true;
    
                if (!useCam)
                    measureImage();
                else {
                    try
                    {
                        camera = new Capture();
                    }
                    catch (Exception exc)
                    {
                        MessageBox.Show(exc.Message);
                        return;
                    }
                    Application.Idle += viewImage;
                    captureProcess = true;
                }
            }
        }
    } 
    

0 个答案:

没有答案