title: How to cut a selection on Opencv images
body: <p>Tell me how to cut the selected area in Opencv. At the moment, the area is highlighted and I want everything to be removed. White background.</p>
  
  <p>Screenshot (<a href="https://i.stack.imgur.com/jVcUf.jpg" rel="nofollow noreferrer">https://i.stack.imgur.com/jVcUf.jpg</a>)</p>
  
  <pre><code>import cv2
  import numpy as np
  img = cv2.imread("57.png")
  gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
  
  
  th, threshed = cv2.threshold(gray, 100, 255, 
  cv2.THRESH_OTSU|cv2.THRESH_BINARY_INV)
  morphed = cv2.morphologyEx(threshed, cv2.MORPH_OPEN, np.ones((2,2)))
  
  cnts = cv2.findContours(morphed, cv2.RETR_EXTERNAL, 
  cv2.CHAIN_APPROX_SIMPLE[-2]
  
  nh, nw = img.shape[:2]
  for cnt in cnts:
      x,y,w,h = bbox = cv2.boundingRect(cnt)
      if h &lt; 0.4 * nh:
          continue
      cv2.rectangle(img, (x,y), (x+w, y+h), (255, 0, 255), 1, cv2.LINE_AA)
  
  cv2.imshow("gray", img) 
  </code></pre>
  
  <p>All that I could do, I tried to select the outlines of all the objects and delete everything less than the main text (you need to leave only 9 digits)</p>