2016-04-20 103 views
0

我正在做一個項目,當你微笑時拍照,但我沒有做微笑檢測本身Emgu CV微笑檢測

(我的項目正在當你的微笑照片)

我怎麼微笑檢測,這樣我可以拍照?

這是我的項目源代碼:

public partial class Form1 : Form 
{ 
    private Capture capture;   
    private bool captureInProgress; 
    private HaarCascade haar; 
    private HaarCascade mouth; 

    public Form1() 
    { 
     InitializeComponent(); 
    } 

    private void Form1_Load(object sender, EventArgs e) 
    { 
     haar = new HaarCascade("haarcascade_frontalface_alt_tree.xml"); 
     mouth = new HaarCascade("haarcascade_mcs_mouth.xml"); 

    } 
    private void ProcessFrame(object sender, EventArgs arg) 
    { 
     Image<Bgr, Byte> ImageFrame = capture.QueryFrame(); 

     if (ImageFrame !=null) 
     { 
      Image<Gray, byte> grayFrame = ImageFrame.Convert<Gray, byte>(); 
      Image<Gray, Byte> gray = ImageFrame.Convert<Gray, Byte>(); 
      var faces = grayFrame.DetectHaarCascade(haar, 1.4, 4, HAAR_DETECTION_TYPE.DO_CANNY_PRUNING, new Size(25, 25))[0]; 
      foreach (var face in faces) 
      { 
       ImageFrame.Draw(face.rect, new Bgr(Color.Green), 3); 

       MCvAvgComp[][] mouthsDetected = gray.DetectHaarCascade(mouth, 1.1, 10, Emgu.CV.CvEnum.HAAR_DETECTION_TYPE.DO_CANNY_PRUNING, new Size(20, 20)); 
       gray.ROI = Rectangle.Empty; 

       foreach (MCvAvgComp e in mouthsDetected[0]) 
       { 
        Rectangle mouthRect = e.rect; 
        mouthRect.Offset(face.rect.X, face.rect.Y); 
        ImageFrame.Draw(mouthRect, new Bgr(Color.Red), 2); 
       } 
      } 

     } 

     CamImageBox.Image = ImageFrame;  

    } 

    private void btnstart_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) 
      { //if camera is getting frames then stop the capture and set button Text 
       // "Start" for resuming capture 
       btnStart.Text = "Başlat"; // 
       Application.Idle -= ProcessFrame; 
      } 
      else 
      { 
       //if camera is NOT getting frames then start the capture and set button 
       // Text to "Stop" for pausing capture 
       btnStart.Text = "Durdur"; 
       Application.Idle += ProcessFrame; 
      } 

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

回答

1

如果你問有關查看相應的圖片,從你的處理,

Emgu CV建議顯示目的而使用ImageBox控制,爲以下原因。這可以與您在上面的代碼示例中使用的ImageFrame(Image)對象結合使用。

ImageBox是用於顯示圖像的高性能控件。只要有可能,它會顯示一個與Image對象共享內存的位圖,因此不需要內存副本(非常快)。

當圖像顯示時,用戶將能夠檢查圖像像素值,視頻幀率,顏色類型。

只需輕點幾下鼠標即可執行簡單的圖像操作。

轉換爲位圖

Image類具有ToBitmap()函數返回一個位圖對象時,可以容易地使用Windows窗體PictureBox控件顯示。

檢測口/笑臉

您將要提供Haarcascade XML的嘴,並相應地捕捉。看下面的code,它可以在嘴邊畫一個矩形,

CascadeClassifier mouth = new CascadeClassifier(Application.StartupPath + "/haarcascade_mcs_mouth.xml"); 
Image<Bgr, Byte> currentframe= null; 
Image<Gray, byte> grayFrame = null; 
Capture grabber = new Capture(); 

currentframe = grabber.QueryFrame().Resize(500, 320, Emgu.CV.CvEnum.INTER.CV_INTER_CUBIC); 

      if (currentframe != null) 
      { 
       grayFrame = currentframe.Convert<Gray, Byte>(); 

       Rectangle[] mouthDetected = mouth.DetectMultiScale(grayFrame, 1.1, 10, Size.Empty, Size.Empty); 

       // to draw rectangle 
       foreach (Rectangle mouthFound in mouthDetected) 
       { 
        ... 
       } 
      }