title: Combining nearby bounding boxes along one axis
body: <p>Here, I use Google Vision API to detect text from the following image. The red box indicates samples of a combined bounding box that I would like to obtain.</p>
  
  <p><a href="https://i.stack.imgur.com/o3UH0.jpg" rel="noreferrer"><img src="https://i.stack.imgur.com/o3UH0.jpg" alt="enter image description here"></a></p>
  
  <p>Basically, I get the text output and bounding box from the above image. Here, I would like to merge the bounding boxes and texts that are located along the same row (left to right). For example, the first line will get merged together:</p>
  
  <pre><code>[{'description': 'บริษัทไปรษณีย์ไทย',
    'vertices': [(528, 202), (741, 202), (741, 222), (528, 222)]},
   {'description': 'จํากัด',
   'vertices': [(754, 204), (809, 204), (809, 222), (754, 222)]},
   ...
  </code></pre>
  
  <p>to</p>
  
  <pre><code>[{'description': 'บริษัทไปรษณีย์ไทยจำกัด',
    'vertices': [(528, 202), (809, 202), (809, 222), (528, 222)]},
   ...
  </code></pre>
  
  <p>These following rows</p>
  
  <pre><code> {'description': 'RP',
    'vertices': [(729, 1072), (758, 1072), (758, 1091), (729, 1091)]},
   {'description': '8147',
    'vertices': [(768, 1072), (822, 1072), (822, 1092), (768, 1092)]},
   {'description': '3609',
    'vertices': [(834, 1073), (889, 1073), (889, 1093), (834, 1093)]},
   {'description': '7',
    'vertices': [(900, 1073), (911, 1073), (911, 1092), (900, 1092)]},
   {'description': 'TH',
  </code></pre>
  
  <p>will get merged together.</p>
  
  <h2>Current approach</h2>
  
  <p>I looked into
  - <a href="https://stackoverflow.com/questions/55376338/how-to-join-nearby-bounding-boxes-in-opencv-python">Solution using OpenCV</a> 
  - <a href="https://answers.opencv.org/question/194536/python-merge-overlapping-bounding-boxes-replace-by-surrounding-bounding-box/" rel="noreferrer">Non-max suppression algorithm</a></p>
  
  <p>but cannot produce one specific for my need since it relies on percentage of overlapping pixels. If someone can help, that would be great!</p>
  
  <p>Please try to use bounding box data here: <a href="https://gist.github.com/titipata/fd44572f7f6c3cc1dfbac05fb86f6081" rel="noreferrer">https://gist.github.com/titipata/fd44572f7f6c3cc1dfbac05fb86f6081</a></p>