title: Detect digits with the openCV Bounding Box algorithm in Python
body: <p>I'm working on a software which should recognise several digits. For cropping the digits of an image I am using openCV. The problem I've got is that the bounding box algorithm is not only detecting the digits. It is detecting the structure in a digit too. </p>
  
  <p><a href="https://i.stack.imgur.com/pSSua.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/pSSua.jpg" alt="enter image description here"></a></p>
  
  <p>The simplest way to solve this would be to set a minimum size the structure have to have. This doesn't work because I have to detect digits of any size. 
  Has anybody an idea to solve this problem?</p>
  
  <p>This is the code:</p>
  
  <pre><code>im = cv2.imread('img.jpg')
      gray=cv2.cvtColor(im,cv2.COLOR_BGR2GRAY)
      contours,hierarchy = cv2.findContours(gray,cv2.RETR_LIST,cv2.CHAIN_APPROX_SIMPLE)
      idx = 0
      for cnt in contours:
  
              xe,ye,we,he = cv2.boundingRect(cnt)
              roi=im[ye-100:ye+he+100,xe-100:xe+we+100]
  
              if xe &gt; 30:
                      if ye &gt; 30:
                              if he &gt; 30:
                                      if we &gt; 30:
                                              idx += 1
                                              cv2.imwrite(str(idx) + '.jpg', roi)
                                              cv2.rectangle(im,(xe,ye),(xe+we,ye+he),(200,0,0),2)
  
  
                                              cv2.imwrite('dev.jpg', im)
  </code></pre>