title: How to group contours and draw a single bounding rectangle
body: <p>I need to group <code>contours</code> and draw a single <code>bounding rectangle</code> that encloses all the contours, something like this</p>
  
  <p><a href="https://i.stack.imgur.com/kchZb.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/kchZb.png" alt="enter image description here"></a></p>
  
  <pre><code>from matplotlib import pyplot as plt
  import cv2 as cv
  
  img = cv.imread('shapes1.png', 0)
  imgRGB = cv.cvtColor(img.copy(), cv.COLOR_GRAY2RGB)
  
  _, ctrs, _ = cv.findContours(img, cv.RETR_EXTERNAL, cv.CHAIN_APPROX_SIMPLE)
  
  boxes = []
  for ctr in ctrs:
      x, y, w, h = cv.boundingRect(ctr)
      boxes.append([x, y, w, h])
  
  for box in boxes:
      top_left     = (box[0], box[1])
      bottom_right = (box[0] + box[2], box[1] + box[3])
      cv.rectangle(imgRGB, top_left, bottom_right, (0,255,0), 2)
  
  fig = plt.figure(figsize = (10, 10))
  ax  = fig.add_subplot(111)
  ax.imshow(imgRGB, cmap='gray')
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/c2dEo.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/c2dEo.png" alt="enter image description here"></a></p>
  
  <p>Is there any straight forward way to do it rather than merging all bounding rectangles programmatically</p>