title: Detection from video capture with EmguCV 3.1.0.1
body: <p>I'm trying to implement face detection with video capture using <strong>EmguCV 3.1.0.1</strong> library, installed by NuGet packages of VS15 on PC Windows 10 64-bit OS in WinForms desktop application. </p>
  
  <p>My goal is to get detection and tracking of human face from videocamera and detect smile, but for examples below I will use only face <code>HaarCascade</code> .xml with <code>CascadeClassifier</code>.</p>
  
  <p>So, my using with <code>DirectShowLib</code> library for <code>videoDevice</code> from <code>comboBox1_SelectedIndexChanged</code> <code>SelectedItem</code>:</p>
  
  <pre><code>using Emgu.CV;
  using Emgu.CV.Structure;
  using Emgu.CV.UI;
  using DirectShowLib;
  </code></pre>
  
  <p>Path to HaarCascade xml-s:</p>
  
  <pre><code>    string facePath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "dir\\haarcascade_frontalface_default.xml");
  </code></pre>
  
  <p>Timer:</p>
  
  <pre><code>    private void timer1_Tick(object sender, EventArgs e)
      {
          detectFace();
      }
  </code></pre>
  
  <p>Attempt 1:</p>
  
  <pre><code>    private void detectFace()
      {
          CascadeClassifier face = new CascadeClassifier(facePath);
          Image&lt;Bgr, Byte&gt; currentframe = null;
          Image&lt;Gray, byte&gt; grayFrame = null;
          Capture grabber;
          grabber = new Capture(videoDevice);
          currentframe = grabber.QueryFrame().Resize(500, 320, Emgu.CV.CvEnum.INTER.CV_INTER_CUBIC);
          if (currentframe != null)
          {
              grayFrame = currentframe.Convert&lt;Gray, Byte&gt;();
              Rectangle[] faceDetected = face.DetectMultiScale(grayFrame, 1.1, 10, Size.Empty, Size.Empty);
              foreach (Rectangle faceFound in faceDetected)
              {
                  currentframe.Draw(faceFound, new Bgr(Color.Red), 2);
              }
              pictureBox1.Image = currentframe.ToBitmap();
          }
      }
  </code></pre>
  
  <p>line  <code>currentframe = grabber.QueryFrame().Resize(500, 320, Emgu.CV.CvEnum.INTER.CV_INTER_CUBIC);</code> says:</p>
  
  <blockquote>
    <p>Error CS0234  The type or namespace name 'INTER' does not exist in the
    namespace 'Emgu.CV.CvEnum' (are you missing an assembly reference?)</p>
  </blockquote>
  
  <p>Instead I've tried to use <code>currentframe = grabber.QueryFrame().Resize(500, 320, Emgu.CV.CvEnum.TemplateMatchingType.CcoeffNormed);</code>, with <code>grabber.QueryFrame().MatchTemplate</code> or <code>grabber.QueryFrame().Retrieve</code>, but another error still keeps in same line as:</p>
  
  <blockquote>
    <p>Error    CS1061  'Mat' does not contain a definition for
    'Resize' and no extension method 'Resize' accepting a first argument
    of type 'Mat' could be found (are you missing a using directive or an
    assembly reference?)</p>
  </blockquote>
  
  <p>I'm not sure where I have to download required dll-s (if it is a reason of missing?) and which dll-s exactly I should add to references additionally.</p>
  
  <p>Attempt 2:</p>
  
  <pre><code>    private Capture _capture;
      private CascadeClassifier _cascadeClassifier;
      private void detectFace()
      {
          _capture = new Capture(videoDevice); 
          _cascadeClassifier = new CascadeClassifier(facePath);
          using (var imageFrame = _capture.QueryFrame().ToImage())
          {
              if (imageFrame != null)
              {
                  var grayframe = imageFrame.Convert();
                  var faces = _cascadeClassifier.DetectMultiScale(grayframe, 1.1, 10, Size.Empty);
                  foreach (var face in faces)
                  {
                      imageFrame.Draw(face, new Bgr(Color.Red), 3);
                  }
              }
              pictureBox1.Image = imageFrame.ToBitmap();
          }
      }
  </code></pre>
  
  <p>line <code>using (var imageFrame = _capture.QueryFrame().ToImage())</code>:</p>
  
  <blockquote>
    <p>Error CS0411  The type arguments for method 'Mat.ToImage(bool)' cannot be inferred from the usage. Try specifying the
    type arguments explicitly.</p>
  </blockquote>
  
  <p>line  <code>var grayframe = imageFrame.Convert();</code>:</p>
  
  <blockquote>
    <p>Error CS0411  The type arguments for method 'Image.Convert()' cannot be inferred from
    the usage. Try specifying the type arguments explicitly.</p>
  </blockquote>
  
  <p>line  <code>imageFrame.Draw(face, new Bgr(Color.Red), 3);</code>:</p>
  
  <blockquote>
    <p>Error CS1503  Argument 2: cannot convert from 'Emgu.CV.Structure.Bgr'
    to 'TColor'</p>
  </blockquote>
  
  <p>Any guide, advice or example would be helpful</p>
  
  <p><strong>Edit by <em>Michal Nawrocik</em> answer below:</strong></p>
  
  <p>Method 1:</p>
  
  <pre><code>private void detectFace()
          {
              CascadeClassifier face = new CascadeClassifier(facePath);
              Image&lt;Bgr, Byte&gt; currentframe = null;
              Image&lt;Gray, byte&gt; grayFrame = null;
              Capture grabber;
              grabber = new Capture(videoDevice);  
              var dstMat = new Mat();
              var frame = grabber.QueryFrame();
              CvInvoke.Resize(frame, dstMat, new Size(500, 320), interpolation: Emgu.CV.CvEnum.Inter.Cubic);
              currentframe = dstMat.ToImage&lt;Bgr, byte&gt;();
              if (currentframe != null)
              {
                  grayFrame = currentframe.Convert&lt;Gray, Byte&gt;();
                  Rectangle[] faceDetected = face.DetectMultiScale(grayFrame, 1.1, 10, Size.Empty, Size.Empty);
                  foreach (Rectangle faceFound in faceDetected)
                  {
                      currentframe.Draw(faceFound, new Bgr(Color.Red), 2);
                  }
                  pictureBox1.Image = currentframe.ToBitmap();
              }
          }
  </code></pre>
  
  <p>Unhandled exception:</p>
  
  <blockquote>
    <p>System.AccessViolationException' occurred in Emgu.CV.World.dll
    Additional information: Attempted to read or write protected memory.
    This is often an indication that other memory is corrupt</p>
  </blockquote>
  
  <p>Method 2:</p>
  
  <pre><code> private Capture _capture;
      private CascadeClassifier _cascadeClassifier;
      private void detectFace()
      {
              _capture = new Capture(videoDevice);   
              _cascadeClassifier = new CascadeClassifier(facePath);
              using (var imageFrame = _capture.QueryFrame().ToImage&lt;Bgr, byte&gt;())
              {
                  if (imageFrame != null)
                  {
                      var grayframe = imageFrame.Convert&lt;Gray, byte&gt;();
                      var faces = _cascadeClassifier.DetectMultiScale(grayframe, 1.1, 10, Size.Empty);  
                      foreach (var face in faces)
                      {
                          imageFrame.Draw(face, new Bgr(Color.Red), 3);
                      }
                  }
                  pictureBox1.Image = imageFrame.ToBitmap();
              }
          }           
      }
  </code></pre>
  
  <p>exception:</p>
  
  <blockquote>
    <p>Emgu.CV.Util.CvException' occurred in Emgu.CV.World.dll Additional
    information: OpenCV: Unrecognized or unsupported array type</p>
  </blockquote>