title: OpenCV - find blob closest to image center
body: <p>Given a thresholded image with a small number (1-3) blobs in it, I'd like to pick the blob closest to the centre, which I expect to be a digit:</p>
  
  <p><img src="https://i.stack.imgur.com/TOKEV.png" alt="Before">
  <img src="https://i.stack.imgur.com/MSnii.png" alt="After"></p>
  
  <p>I'd like to extract the red rectangle as a separate Mat, since it's closest to the centre (green dot).  My first thought is to iterate through all the blobs in the image and take the one with the smallest distance, pixelwise, from its centroid to the image centre.  However, I'm not familiar with the vast majority of OpenCV functions (and there are a lot of them!) so I'm wondering if there's a better way.</p>
  
  <p>I can already see a couple of problems with this, though:</p>
  
  <ol>
  <li>If the box on the outside covered 3 or 4 sides, the centroid might
  be close to the centre even though the box itself is nowhere near
  the centre. Perhaps I should use a higher-order moment to give more
  weight to points far away from the centre?</li>
  <li>OpenCV has no integrated blob-finding (or connected-component labeling) library that I know of.  I could use find contours, but this might be inaccurate for numbers like 6, 8 or 9 with an interior portion.</li>
  </ol>