title: Multiple object recognition opencv
body: <p>I am solving a problem of finding the objects on image given template.
  Example of image:
  <a href="https://i.stack.imgur.com/55Kmz.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/55Kmz.png" alt="enter image description here" /></a></p>
  <p>Example of template</p>
  <p><a href="https://i.stack.imgur.com/q8a5F.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/q8a5F.png" alt="enter image description here" /></a></p>
  <p>So far I've come up with the following approach:</p>
  <ol>
  <li>Use some detected, e.g. <code>sift</code> for finding keypoints</li>
  <li>Match keypoints</li>
  <li>Cluster them</li>
  </ol>
  <p>It looks like</p>
  <pre><code>sift = cv2.SIFT_create()
  # find the keypoints and descriptors with SIFT
  kp1, des1 = sift.detectAndCompute(img,None)
  kp2, des2 = sift.detectAndCompute(query,None)
  # BFMatcher with default params
  bf = cv2.BFMatcher()
  matches = bf.knnMatch(des1,des2,k=2)
  # Apply ratio test
  good = []
  for m,n in matches:
      if m.distance &lt; 0.5*n.distance:
          good.append([m])
  # cv.drawMatchesKnn expects list of lists as matches.
  img3 = cv2.drawMatchesKnn(img,kp1,query,kp2,good,None,flags=cv2.DrawMatchesFlags_NOT_DRAW_SINGLE_POINTS)
  plt.imshow(img3)
  plt.show()
  </code></pre>
  <p>with the outcome</p>
  <p><a href="https://i.stack.imgur.com/Do9R7.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Do9R7.png" alt="enter image description here" /></a></p>
  <p>But I am stuck here. How could I use these matches to actually find the bboxes of objects present on image. I've tried to create grid, based on keypoints and size of template:</p>
  <p><a href="https://i.stack.imgur.com/c3HKj.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/c3HKj.png" alt="enter image description here" /></a></p>
  <p>And then using <code>cv2.matchTemplate</code> find the objects in area around each cell (window shifting), but it didn't work quite well. How should I deal with it?</p>