title: How to select correct contour the group of six digits
body: <p>I have a batch of screenshots like here:</p>
  <p><a href="https://i.imgur.com/uNsa7PU.png" rel="nofollow noreferrer"><img src="https://i.imgur.com/uNsa7PU.png" alt="Test screenshot" /></a></p>
  <p>and I try to detect the region with six digits and recognize them. The second part works like a charm. I have a problem detecting the correct region because it can be placed with a shift depending on screen dimensions. For example, crop image looks like this:</p>
  <p><a href="https://i.stack.imgur.com/EQf3A.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/EQf3A.png" alt="Cropped image" /></a></p>
  <p>Seems it looks ok, but I have to add some workaround in code to select the right place.</p>
  <p>My code:</p>
  <pre><code>import cv2
  import numpy as np
  
  from matplotlib import pyplot as plt
  from PIL import Image
  
  rectKernel = cv2.getStructuringElement(cv2.MORPH_RECT, (15, 6))
  sqKernel = cv2.getStructuringElement(cv2.MORPH_RECT, (8, 8))
  
  # Load and resize image to standard size
  img0 = Image.open('./data/test.png')
  img0.thumbnail((720, 1423))
  img = np.array(img0)
  
  # The magic from https://www.pyimagesearch.com/2017/07/17/credit-card-ocr-with-opencv-and-python/
  gray = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
  tophat = cv2.morphologyEx(gray, cv2.MORPH_TOPHAT, rectKernel)
  
  gradX = cv2.Sobel(tophat, ddepth=cv2.CV_32F, dx=1, dy=0, ksize=-1)
  gradX = np.absolute(gradX)
  (minVal, maxVal) = (np.min(gradX), np.max(gradX))
  gradX = (255 * ((gradX - minVal) / (maxVal - minVal)))
  gradX = gradX.astype(&quot;uint8&quot;)
  
  gradX = cv2.morphologyEx(gradX, cv2.MORPH_CLOSE, rectKernel)
  thresh = cv2.threshold(gradX, 0, 255, cv2.THRESH_BINARY | cv2.THRESH_OTSU)[1]
  thresh = cv2.morphologyEx(thresh, cv2.MORPH_CLOSE, sqKernel)
  
  cnts, _ = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
  locs = []
  
  for (i, c) in enumerate(cnts):
      (x, y, w, h) = cv2.boundingRect(c)
      ar = w / h
      if x &gt; 140 and x &lt; 220 and w &gt; 100 and h &gt; 12 and h &lt; 20 and ar &gt;= 4 and ar &lt;= 6:
          locs.append((x, y, w, h))
  
  # Calculate the crop rectangle 
  LEFT_TOP = (181, 316)
  RIGHT_BOTTOM = (299, 346)
  if len(locs) &gt; 0:
      (x, y, w, h) = locs[0]
      LEFT_TOP = (x - 5, y - 5) # workaround place
      RIGHT_BOTTOM = (x + w + 5, y + h) # workaround place
  print(LEFT_TOP, RIGHT_BOTTOM)
  img1 = img0.crop(LEFT_TOP + RIGHT_BOTTOM)
  </code></pre>
  <p>Selected contour looks like:</p>
  <p><a href="https://i.imgur.com/9ZsvIwk.png" rel="nofollow noreferrer"><img src="https://i.imgur.com/9ZsvIwk.png" alt="Screenshot with contour" /></a></p>
  <p>It selects a contour smaller than the actual region. Why? How to fix it?</p>
  <p>Thank you!</p>
  <p>Test file:
  <a href="https://i.stack.imgur.com/u8D3B.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/u8D3B.png" alt="Test png for testing" /></a></p>