title: How to find the contours of a frame from an Android camera and convert them to box2d bodies?
body: <p>Using OpenFrameworks, OpenCV and Box2D I was able to achieve it with a good framerate. Using Android seems a much more complicated task (partly because I am a JAVA newbie).</p>
  
  <p>This is how I started:</p>
  
  <ol>
  <li><p>Use the "OpenCV Sample - image manipulations" and delete everything except the "canny" effect, which produces a nice black &amp; white image that is perfect to find the contours.</p>
  
  <pre><code>public Mat onCameraFrame(CvCameraViewFrame inputFrame)
  {
      mRgba = inputFrame.rgba();
      Imgproc.Canny(mRgbaInnerWindow, mIntermediateMat, 50, 100);
      Imgproc.cvtColor(mIntermediateMat, mRgbaInnerWindow, Imgproc.COLOR_GRAY2BGRA, 4);
  
      return mRgba;
  }
  </code></pre></li>
  <li><p>From the "OpenCV Sample - color-blob-detection" I grabbed the logic to find the contours in a Mat:</p>
  
  <pre><code>// These two lines are actually in the function onCameraViewStarted
  mHierarchy = new Mat();
  CONTOUR_COLOR = new Scalar(255,0,0,255);
  
  // These lines are in function onCameraFrame
  List&lt;MatOfPoint&gt; contours = new ArrayList&lt;MatOfPoint&gt;();        
  Imgproc.findContours(mRgbaInnerWindow, contours, mHierarchy, Imgproc.RETR_EXTERNAL,     
  Imgproc.CHAIN_APPROX_SIMPLE);
  Imgproc.drawContours(mIntermediateMat, contours, -1, CONTOUR_COLOR);
  </code></pre>
  
  <p>So, my current function looks like this and it doesn't work:</p>
  
  <pre><code>public Mat onCameraFrame(CvCameraViewFrame inputFrame)
  {
      mRgba = inputFrame.rgba();
  
      if ((mRgbaInnerWindow == null) || (mGrayInnerWindow == null) || (mRgba.cols() != mSizeRgba.width) || (mRgba.height() != mSizeRgba.height))
          CreateAuxiliaryMats();
  
      Imgproc.Canny(mRgbaInnerWindow, mIntermediateMat, 50, 100);
      //Imgproc.cvtColor(mIntermediateMat, mRgbaInnerWindow, Imgproc.COLOR_GRAY2BGRA, 4);
  
      List&lt;MatOfPoint&gt; contours = new ArrayList&lt;MatOfPoint&gt;();
  
      Imgproc.findContours(mRgbaInnerWindow, contours, mHierarchy, Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_SIMPLE);
      //Imgproc.drawContours(mIntermediateMat, contours, -1, CONTOUR_COLOR);
  
      return mRgba;
  }
  </code></pre></li>
  <li><p>Now, this is where I'm stucked. I keep getting exceptions and I think I'm not using the right dimensions or transforming the Mat to the right color space. This post has some insight but I don't know if it's correct: <a href="https://stackoverflow.com/questions/12457477/opencv-on-android-findcontours-throws-exception">OpenCV on Android findContours throws Exception</a></p></li>
  </ol>