title: Remove outliers lines after findContours in image using python
body: <p>I want to detect all rectangles in image and I use findContours in OpenCv , and I want to delete unnecessary shapes that have been identified by FindContours.</p>
  
  <p>My image <a href="https://i.stack.imgur.com/eLb1s.png" rel="nofollow noreferrer">https://i.stack.imgur.com/eLb1s.png</a></p>
  
  <p>My result: <a href="https://i.stack.imgur.com/xQqeF.png" rel="nofollow noreferrer">https://i.stack.imgur.com/xQqeF.png</a></p>
  
  <p>My code:</p>
  
  <pre><code>img =cv2.imread('CD/A.png')
  gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
  edges = cv2.Canny(gray,50,150,apertureSize = 3)
  img1=np.ones(img.shape, dtype=np.uint8)*255
  ret,thresh = cv2.threshold(gray,127,255,1)
  (_,contours,h) = cv2.findContours(thresh,1,2)
  
  for cnt in contours:
      approx = cv2.approxPolyDP(cnt,0.01*cv2.arcLength(cnt,True),True)
      if len(approx)==4:
          cv2.drawContours(img1,[cnt],0,(0,255,0),2)
  cv2.imshow('Detected line',img1)
  cv2.waitKey(0)
  cv2.destroyAllWindows()
  </code></pre>
  
  <p>I want to remove these extreme lines that exist within the rectangles :<br>
  <a href="https://i.stack.imgur.com/n9byP.png" rel="nofollow noreferrer">https://i.stack.imgur.com/n9byP.png</a></p>
  
  <p>Need your help guys .</p>