title: Shape detection with opencv/python
body: <p>I'm trying to teach my test automation framework to detect a selected item in an app using opencv (the framework grabs frames/screenshots from the device under test). Selected items are always a certain size and always have blue border which helps but they contain different thumbnail images. See the example image provided.</p>
  
  <p>I have done a lot of Googling and reading on the topic and I'm close to getting it to work expect for one scenario which is image C in the example image. <a href="https://i.stack.imgur.com/CbclA.png" rel="nofollow noreferrer">example image</a> This is where there is a play symbol on the selected item.</p>
  
  <p>My theory is that OpenCV gets confused in this case because the play symbol is basically circle with a triangle in it and I'm asking it to find a rectangular shape.</p>
  
  <p>I found this to be very helpful: <a href="https://www.learnopencv.com/blob-detection-using-opencv-python-c/" rel="nofollow noreferrer">https://www.learnopencv.com/blob-detection-using-opencv-python-c/</a></p>
  
  <p>My code looks like this:</p>
  
  <pre><code>import cv2
  import numpy as np
  
  img = "testimg.png"
  
  values = {"min threshold": {"large": 10, "small": 1},
            "max threshold": {"large": 200, "small": 800},
            "min area": {"large": 75000, "small": 100},
            "max area": {"large": 80000, "small": 1000},
            "min circularity": {"large": 0.7, "small": 0.60},
            "max circularity": {"large": 0.82, "small": 63},
            "min convexity": {"large": 0.87, "small": 0.87},
            "min inertia ratio": {"large": 0.01, "small": 0.01}}
  size = "large"
  
  # Read image
  im = cv2.imread(img, cv2.IMREAD_GRAYSCALE)
  
  # Setup SimpleBlobDetector parameters.
  params = cv2.SimpleBlobDetector_Params()
  
  # Change thresholds
  params.minThreshold = values["min threshold"][size]
  params.maxThreshold = values["max threshold"][size]
  
  # Filter by Area.
  params.filterByArea = True
  params.minArea = values["min area"][size]
  params.maxArea = values["max area"][size]
  
  # Filter by Circularity
  params.filterByCircularity = True
  params.minCircularity = values["min circularity"][size]
  params.maxCircularity = values["max circularity"][size]
  
  
  # Filter by Convexity
  params.filterByConvexity = False
  params.minConvexity = values["min convexity"][size]
  
  # Filter by Inertia
  params.filterByInertia = False
  params.minInertiaRatio = values["min inertia ratio"][size]
  
  # Create a detector with the parameters
  detector = cv2.SimpleBlobDetector(params)
  
  # Detect blobs.
  keypoints = detector.detect(im)
  
  for k in keypoints:
      print k.pt
      print k.size
  
  # Draw detected blobs as red circles.
  # cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS ensures
  # the size of the circle corresponds to the size of blob   
  im_with_keypoints = cv2.drawKeypoints(im, keypoints, np.array([]), (0, 0, 255),
                                        cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS)
  
  # Show blobs
  cv2.imshow("Keypoints", im_with_keypoints)
  cv2.waitKey(0)
  </code></pre>
  
  <p>How do I get OpenCV to only look at the outer shape defined by the blue border and ignore the inner shapes (the play symbol and of course the thumbnail image)? I'm sure it must be do-able somehow.</p>