title: Extract bounding box and save it as an image
body: <p>Suppose you have the following image:</p>
  
  <p><img src="https://i.stack.imgur.com/r5z3w.png" alt="Example:"></p>
  
  <p>Now I want to extract each of the independent letters into individual images. Currently, I've recovered the contours and then drew a bounding box, in this case for the character <code>a</code>:</p>
  
  <p><img src="https://i.stack.imgur.com/eZwt8.png" alt="Bounding box for the character &#39;a&#39;"> </p>
  
  <p>After this, I want to extract each of the boxes (in this case for the letter <code>a</code>) and save it to an image file.</p>
  
  <p>Expected result:</p>
  
  <p><img src="https://i.stack.imgur.com/qbpV2.png" alt="Result"></p>
  
  <p>Here's my code so far:</p>
  
  <pre><code>import numpy as np
  import cv2
  
  im = cv2.imread('abcd.png')
  im[im == 255] = 1
  im[im == 0] = 255
  im[im == 1] = 0
  im2 = cv2.cvtColor(im,cv2.COLOR_BGR2GRAY)
  ret,thresh = cv2.threshold(im2,127,255,0)
  contours, hierarchy = cv2.findContours(thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
  
  for i in range(0, len(contours)):
      if (i % 2 == 0):
         cnt = contours[i]
         #mask = np.zeros(im2.shape,np.uint8)
         #cv2.drawContours(mask,[cnt],0,255,-1)
         x,y,w,h = cv2.boundingRect(cnt)
         cv2.rectangle(im,(x,y),(x+w,y+h),(0,255,0),2)
         cv2.imshow('Features', im)
         cv2.imwrite(str(i)+'.png', im)
  
  cv2.destroyAllWindows()
  </code></pre>
  
  <p>Thanks in advance.</p>