title: Using OpenCV to find the bounding box of numbers on an image
body: <p>I'm trying to find the bounding box of the numbers in the middle of the below 3 images.</p>
  
  <p>Here's 3 example cards I'm trying to work with.</p>
  
  <p><a href="https://i.stack.imgur.com/i21i5.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/i21i5.jpg" alt="Card 1"></a></p>
  
  <p><a href="https://i.stack.imgur.com/GAWe0.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/GAWe0.jpg" alt="Card 2"></a></p>
  
  <p><a href="https://i.stack.imgur.com/N9nAl.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/N9nAl.jpg" alt="Card 3"></a></p>
  
  <p>The code I'm using is based (almost a complete copy) of the code provided in the first answer <a href="https://stackoverflow.com/questions/23506105/extracting-text-opencv">here</a>, although converted to Java (answers in C++ are fine) and added parameters for the size of the contours to merge (defined as sizeHorizonal and sizeVertical in my code), which are the two parameters I'm playing with in the images below.</p>
  
  <pre><code>MatOfPoint2f approxCurve = new MatOfPoint2f();
  
  Mat imgMAT = new Mat();
  Utils.bitmapToMat(bmp32, imgMAT);
  
  Mat grad = new Mat();
  
  Imgproc.cvtColor(imgMAT, grad, Imgproc.COLOR_BGR2GRAY);
  
  Mat img_sobel = new Mat();
  Mat img_threshold = new Mat();
  
  Imgproc.Sobel(grad, img_sobel, CvType.CV_8U, 1, 0, 3, 1, 0, Core.BORDER_DEFAULT);
  
  Imgproc.threshold(img_sobel, img_threshold, 0, 255, Imgproc.THRESH_OTSU + Imgproc.THRESH_BINARY);
  
  Mat element = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(sizeHorizontal, sizeVertical));
  
  Imgproc.morphologyEx(img_threshold, img_threshold, Imgproc.MORPH_CLOSE, element);
  
  Imgproc.cvtColor(imgMAT, imgMAT, Imgproc.COLOR_BGR2GRAY);
  
  
  List&lt;MatOfPoint&gt; contours = new ArrayList&lt;&gt;();
  Imgproc.findContours(img_threshold, contours, new Mat(), Imgproc.RETR_CCOMP, Imgproc.CHAIN_APPROX_SIMPLE, new org.opencv.core.Point(0, 0));
  
  for (int i = 0; i &lt; contours.size(); i++) {
  
      //Convert contours(i) from MatOfPoint to MatOfPoint2f
      MatOfPoint2f contour2f = new MatOfPoint2f( contours.get(i).toArray() );
  
      //Processing on mMOP2f1 which is in type MatOfPoint2f
      double approxDistance = Imgproc.arcLength(contour2f, true)*0.02;
      Imgproc.approxPolyDP(contour2f, approxCurve, approxDistance, true);
  
      //Convert back to MatOfPoint
      MatOfPoint points = new MatOfPoint( approxCurve.toArray() );
  
      // Get bounding rect of contour
      org.opencv.core.Rect rect = Imgproc.boundingRect(points);
  
      Imgproc.rectangle(imgMAT, rect.tl(), rect.br(), new Scalar(0, 255, 0), 2);
  }
  </code></pre>
  
  <p>I've got the separate number sections contoured, but I can't find a way to isolate the contours I want. Here's the areas contoured with the parameters for the size input. As you can see for the second image, this is working exactly as I want and has contoured the whole number, rather than each section.</p>
  
  <p>1: Size param input: 17, 5</p>
  
  <p><a href="https://i.stack.imgur.com/pv2E5.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/pv2E5.png" alt="enter image description here"></a></p>
  
  <p>2: Size param input: 23, 7</p>
  
  <p><a href="https://i.stack.imgur.com/Ji7x9.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Ji7x9.png" alt="enter image description here"></a></p>
  
  <p>3: Size param input: 23, 13</p>
  
  <p><a href="https://i.stack.imgur.com/b3kxi.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/b3kxi.png" alt="enter image description here"></a></p>
  
  <p>So, things I need help with:</p>
  
  <ol>
  <li>Isolating the four contours in the middle and finding a way to merge these contours together</li>
  </ol>
  
  <p>I've thought about taking the contours that match a given aspect ratio and cropping to a bounding box encompassing all of them, but there are other surrounding contours with similar ratios.</p>
  
  <ol start="2">
  <li>Finding a way to choose the correct size parameters automatically (as each card type requires different parameters to isolate the numbers)</li>
  </ol>
  
  <p>Short of trying all 3 size inputs and seeing what gives the expected contours, I could use the prevailing colour as an indicator of the card type and then use the parameters for this card type. But, any other suggestions would be helpful, as I feel there's a better way to do this.</p>
  
  <p>Many thanks!</p>