title: openCV problem with detecting contours of shapes fully
body: <p>I am doing this university project where i try to detect UI elements on screenshots of Android applications using openCV. I am not expecting a 100 percent accuracy for this detection of UI elements. </p>
  
  <p>This is my code below. I convert the image to gray scale, apply Gaussian blur and then use adaptive threshold to convert the image to binary. After which i use the find contours method. </p>
  
  <pre><code>ap = argparse.ArgumentParser()
  ap.add_argument("-i","--image", help = "path to an image", required = 
  True)
  args = vars(ap.parse_args())
  
  image = cv2.imread(args["image"])
  gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
  
  cv2.imshow("gray",gray)
  cv2.waitKey(0)
  
  
  blurred = cv2.GaussianBlur(gray, (5,5), 0)
  thresh = cv2.adaptiveThreshold(blurred, 255, 
  cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY_INV, 11, 4)
  
  cv2.imshow("thresh",thresh)
  cv2.waitKey(0)
  cnts = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL, 
  cv2.CHAIN_APPROX_SIMPLE)
  cnts = imutils.grab_contours(cnts)
  
  cv2.drawContours(image, cnts, -1, (0,255,0), 1)
  
  cv2.imshow("contours", image)
  cv2.waitKey(0)
  
  for c in cnts:
  
      area = cv2.contourArea(c)
      print(area)
      if area &gt; 50:
          M = cv2.moments(c)
  
  
          cX = int(M['m10'] / M['m00'])
          cY = int(M['m01'] / M['m00'])
  
  
  
          #cv2.drawContours(image, [c], -1, (0,255,0), 2)  # draw contours on image 
  
          (x,y,w,h) = cv2.boundingRect(c) # for each contour get a 
  bounding rectangle 
          mask = np.zeros(image.shape[:2], dtype = "uint8")    # find 
  shape of the image dimensions and set up a mask 
          mask[y: y + h, x: x + w] = 255      # convert region of 
  interest into white 
          to_display = cv2.bitwise_and(image,image, mask = mask)  # carry 
  out bitwise and 
          #cv2.putText(image, 'center', (c))
  
          cv2.imshow("Image", to_display)
          cv2.waitKey(0)
  </code></pre>
  
  <p>this is the screenshot that i am running my code on.</p>
  
  <p>The leftmost screenshot represents the image after applying a threshold to it. </p>
  
  <p>The middle image represents the image i get after drawing the contours. </p>
  
  <p>The last image shows when i am examining each individual contour. The contour covers the line but does not encapsulate the rectangle. </p>
  
  <p>I have a few questions. </p>
  
  <p>1) Is it possible to sieve out the contours for the white rectangles. What alteration do i have to make to my code to be able to achieve this? </p>
  
  <p>2) I am trying to sieve out the unimportant contours eg. the words and I was thinking if i could use the getArea() function to help me with it. The idea is that i would set a minimum contour size to filter out the smaller contours that account for the words. </p>
  
  <p><a href="https://i.stack.imgur.com/KVKEb.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/KVKEb.png" alt=""></a></p>
  
  <p>This is another image that i have tried to identify the "objects" in this screenshots. </p>
  
  <p>I face the same issue here where i cant identify the white rectangles. I am only identifying the borders of the rectangle.</p>
  
  <p><a href="https://i.stack.imgur.com/fuPnd.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/fuPnd.png" alt="enter image description here"></a></p>
  
  <p>Would appreciate any form of help as I am still new to openCv </p>
  
  <p>Original images before processing: </p>
  
  <p><a href="https://i.stack.imgur.com/bcJM7.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/bcJM7.png" alt="enter image description here"></a></p>