title: opencv: fit a minimum enclosing ellipse in a binary image
body: <p>I have used OpenCV and the grabcut implementation to generate a binary mask of the foreground. This is represented as an opencv matrix of CV_8UC1 where the pixels belonging to the foreground has the value 255 and the background is zero (i.e. it is a binary mask). So, an image like the one attached:<img src="https://i.stack.imgur.com/2RgfZ.jpg" alt="enter image description here"></p>
  
  <p>I would like to find the minimum enclosing ellipse for this masked image. The examples that I find online seem a bit complex and I could not translate it to my needs. I tried simply using </p>
  
  <pre><code>// result is my OpenCV array of 
  cv::RotatedRect e = cv::fitEllipse(result);
  
  OpenCV Error: Assertion failed (points.checkVector(2) &gt;= 0 &amp;&amp; 
  (points.depth() == CV_32F || points.depth() == CV_32S)) in fitEllipse, 
  file /home/luca/Downloads/opencv-2.4.10/modules/imgproc
  /src/contours.cpp, line 2019
  
  terminate called after throwing an instance of 'cv::Exception'
  what():  /home/luca/Downloads/opencv-2.4.10/modules/imgproc
  /src/contours.cpp:2019: error: (-215) points.checkVector(2) &gt;= 0 &amp;&amp; 
  (points.depth() == CV_32F || points.depth() == CV_32S) in function 
  fitEllipse
  </code></pre>
  
  <p>The error persists even when I convert it to 32 bit signed int with:</p>
  
  <pre><code>cv::Mat r;
  result.convertTo(r, CV_32S);
  cv::RotatedRect e = cv::fitEllipse(r);
  </code></pre>