title: How to identify a closed rectangle contour with odometer reading in python opencv?
body: <p>I am trying to use <code>cv2</code> for identifying the odometer reading - I am trying to extract a contour where the odometer reading is present. I am unable to identify the region accurately. I am trying to get a area / rectangle contour from an image. The code is for max area from a region. Can anyone help me here ? </p>
  
  <p>Following is the code </p>
  
  <pre><code>import numpy as np
  import matplotlib.pyplot as plt 
  
  # assuming you have the result image store in median
  median = cv2.imread("odo_2.jpg", 0)
  image_gray = median
  
  binary = cv2.bitwise_not(image_gray)
  edged = cv2.Canny(binary, 50, 80, 255)
  
  #threshold = cv2.adaptiveThreshold(edged,255,cv2.ADAPTIVE_THRESH_GAUSSIAN_C,cv2.THRESH_BINARY,11,2)
  
  
  
  contours = cv2.findContours(edged, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)[-2]
  
  
  
  rect_cnts = []
  for cnt in contours:
      peri = cv2.arcLength(cnt, True)
      approx = cv2.approxPolyDP(cnt, 0.04 * peri, True)
      (x, y, w, h) = cv2.boundingRect(cnt)
      ar = w / float(h)
      if (len(approx) == 4) &amp; (ar &gt;= 0.95 and ar &lt;= 1.05) : # shape filtering condition
          pass 
      else :
          rect_cnts.append(cnt)
  
  
  max_area = 0
  football_square = None
  for cnt in rect_cnts:
      (x, y, w, h) = cv2.boundingRect(cnt)
      if max_area &lt; w*h:
          max_area = w*h
          football_square = cnt
  
  # Draw the result
  image = cv2.cvtColor(image_gray, cv2.COLOR_GRAY2RGB)
  cv2.drawContours(image, [football_square], -1, (0, 0,255), 3)
  cv2.imshow("Result Preview", image)
  #cv2.imshow("Result Preview", edged)
  cv2.waitKey(0)
  
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/JnNSu.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/JnNSu.jpg" alt="Image Input"></a></p>