title: EmguCV: Draw contour on object in Motion using Optical Flow?
body: <p>I would like to do motion detection in C# (using EmguCV 3.0) to remove object in motion or in foreground to draw an overlay.</p>
  
  <p>Here is a sample test I done with a Kinect (because It's a depth camera)
  <img src="https://i.stack.imgur.com/4MoGX.jpg" alt="Démo with Kinect"></p>
  
  <p>How can I get started with EmguCV 3.0 ? </p>
  
  <ul>
  <li>I tried many background removal code that do not work</li>
  <li>It seems OpticalFlow is a good start but there si no example in EmguCV 3.0</li>
  <li>If I find the largest blob how can I find its contours ?</li>
  </ul>
  
  <p>Can someone help me to get started ?</p>
  
  <p><strong>EDIT: 17/06/2015</strong></p>
  
  <p>In EmguCV3.0.0 RC I don't see OpticalFlow in the package and documentation:
  <a href="http://www.emgu.com/wiki/files/3.0.0-rc1/document/html/b72c032d-59ae-c36f-5e00-12f8d621dfb8.htm" rel="noreferrer">http://www.emgu.com/wiki/files/3.0.0-rc1/document/html/b72c032d-59ae-c36f-5e00-12f8d621dfb8.htm</a></p>
  
  <p>There is only : DenseOpticalFlow, OpticalFlowDualTVL1 ???</p>
  
  <p>This is a AbsDiff Code:</p>
  
  <pre><code>var grayFrame = frame.Convert&lt;Gray, Byte&gt;();
  var motionFrame = grayFrame.AbsDiff(backFrame)
                             .ThresholdBinary(new Gray(20), new Gray(255))
                             .Erode(2) 
                             .Dilate(2);
  </code></pre>
  
  <p>Result:
  <img src="https://i.stack.imgur.com/Bgr5N.jpg" alt="Demo Diff"></p>
  
  <p>I don't know how to get the motion in white ?</p>
  
  <p>This is the Blob Code:</p>
  
  <pre><code>Image&lt;Bgr, Byte&gt; smoothedFrame = new Image&lt;Bgr, byte&gt;(frame.Size);
  CvInvoke.GaussianBlur(frame, smoothedFrame, new Size(3, 3), 1); //filter out noises
  
  Mat forgroundMask = new Mat();
  fgDetector.Apply(smoothedFrame, forgroundMask);
  
  CvBlobs blobs = new CvBlobs();
  blobDetector.Detect(forgroundMask.ToImage&lt;Gray, byte&gt;(), blobs);
  blobs.FilterByArea(400, int.MaxValue);
  blobTracker.Update(blobs, 1.0, 0, 1);
  
  foreach (var pair in blobs) {
    CvBlob b = pair.Value;
    CvInvoke.Rectangle(frame, b.BoundingBox, new MCvScalar(255.0, 255.0, 255.0), 2);
  }
  </code></pre>
  
  <p>Result:
  <img src="https://i.stack.imgur.com/kLHm9.jpg" alt="Blob Demo"></p>
  
  <p>Why so much false positive ?</p>
  
  <p>This is a MOG2 Code:</p>
  
  <pre><code>forgroundDetector.Apply(frame, forgroundMask);
  motionHistory.Update(forgroundMask);
  var motionMask = GetMotionMask();
  Image&lt;Bgr, Byte&gt; motionImage = new Image&lt;Bgr, byte&gt;(motionMask.Size);
  CvInvoke.InsertChannel(motionMask, motionImage, 0);
  
  Rectangle[] rects;
  using (VectorOfRect boundingRect = new VectorOfRect()) {
    motionHistory.GetMotionComponents(segMask, boundingRect);
    rects = boundingRect.ToArray();
  }
  
  foreach (Rectangle comp in rects) { ...
  </code></pre>
  
  <p>Result:
  <img src="https://i.stack.imgur.com/PQWpo.jpg" alt="MOG2 Demo"></p>
  
  <p>If I select the biggest Area how can I get the contour of the object ?</p>