title: SIFT match computer vision
body: <p>I need to determine the location of yogurts in the supermarket. Source photo looks like <a href="https://i.stack.imgur.com/nZBK5.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/nZBK5.jpg" alt="enter image description here" /></a></p>
  <p>With template:</p>
  <p><a href="https://i.stack.imgur.com/cbX9e.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/cbX9e.jpg" alt="enter image description here" /></a></p>
  <p>I using SIFT to extract key points of template:</p>
  <pre><code>img1 = cv.imread('train.jpg')
  sift = cv.SIFT_create()# queryImage
  kp1, des1 = sift.detectAndCompute(img1, None)
  path = glob.glob(&quot;template.jpg&quot;)
  cv_img = []
  l=0
  
  for img in path:
      img2 = cv.imread(img) # trainImage
      # Initiate SIFT detector
  
      # find the keypoints and descriptors with SIFT
  
      kp2, des2 = sift.detectAndCompute(img2,None)
      # FLANN parameters
      FLANN_INDEX_KDTREE = 1
      index_params = dict(algorithm = FLANN_INDEX_KDTREE, trees = 5)
      search_params = dict(checks=50)   # or pass empty dictionary
      flann = cv.FlannBasedMatcher(index_params,search_params)
      matches = flann.knnMatch(des1,des2,k=2)
      # Need to draw only good matches, so create a mask
  
      # ratio test as per Lowe's paper
  
      if (l &lt; len(matches)):
          l = len(matches)
          image = img2
          match = matches
  
          
          
      h_query, w_query, _= img2.shape
  
      matchesMask = [[0,0] for i in range(len(match))]
      good_matches = []
      good_matches_indices = {}
      for i,(m,n) in enumerate(match):
          if m.distance &lt; 0.7*n.distance:
              matchesMask[i]=[1,0]
              good_matches.append(m)
              good_matches_indices[len(good_matches) - 1] = i
  
      
      bboxes = []
      
      
      
      src_pts = np.float32([ kp1[m.queryIdx].pt for m in good ]).reshape(-1,2)
      dst_pts = np.float32([ kp2[m.trainIdx].pt for m in good ]).reshape(-1,2)
  
      model, inliers = initialize_ransac(src_pts, dst_pts)
      
      n_inliers = np.sum(inliers)
      matched_indices = [good_matches_indices[idx] for idx in inliers.nonzero()[0]]
      
      print(len(matched_indices))
      model, inliers = ransac(
          (src_pts, dst_pts),
          AffineTransform, min_samples=4,
          residual_threshold=4, max_trials=20000
      )
  
      n_inliers = np.sum(inliers)
      print(n_inliers)
      matched_indices = [good_matches_indices[idx] for idx in inliers.nonzero()[0]]
      print(matched_indices)
      
  
      q_coordinates = np.array([(0, 0), (h_query, w_query)])
      coords = model.inverse(q_coordinates)
      print(coords)
      
      h_query, w_query,_ = img2.shape
      q_coordinates = np.array([(0, 0), (h_query, w_query)])
      coords = model.inverse(q_coordinates)
      print(coords)
  #     bboxes_list.append((i, coords))
                  
  
      M, mask = cv.findHomography(src_pts, dst_pts, cv.RANSAC, 2)
  
      draw_params = dict(matchColor = (0,255,0),
                          singlePointColor = (255,0,0),
                          matchesMask = matchesMask,
                          flags = cv.DrawMatchesFlags_DEFAULT)
  
      img3 = cv.drawMatchesKnn(img1,kp1,image,kp2,match,None,**draw_params)
  
      plt.imshow(img3),plt.show()
  </code></pre>
  <p>Result of SIFT looks like</p>
  <p><a href="https://i.stack.imgur.com/jBsse.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/jBsse.png" alt="enter image description here" /></a></p>
  <p>The question is what is the best way to clasterise points to obtain rectangles, representing each yogurt? I tried RANSAC, but this method doesn't work in this case.</p>