tomateblue

OpenKNN.py

Aug 23rd, 2017
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.23 KB | None | 0 0
  1. import numpy as np
  2. import cv2
  3.  
  4. def CompareImage(img1,img2):
  5.  sift = cv2.xfeatures2d.SURF_create()
  6.  kp1, des1 = sift.detectAndCompute(img1, None)
  7.  kp2, des2 = sift.detectAndCompute(img2, None)
  8.  FLANN_INDEX_KDTREE = 0
  9.  index_params = dict(algorithm=FLANN_INDEX_KDTREE, trees=5)
  10.  search_params = dict(checks=50)
  11.  
  12.  flann = cv2.FlannBasedMatcher(index_params, search_params)
  13.  matches = flann.knnMatch(des1, des2, k=2)
  14.  FLANN_INDEX_KDTREE = 0
  15.  MIN_MATCH_COUNT = 10
  16.  index_params = dict(algorithm=FLANN_INDEX_KDTREE, trees=5)
  17.  search_params = dict(checks=50)
  18.  flann = cv2.FlannBasedMatcher(index_params, search_params)
  19.  matches = flann.knnMatch(des1, des2, k=2)
  20.  good = []
  21.  for m, n in matches:
  22.     if m.distance < 0.6 * n.distance:
  23.         good.append(m)
  24.  
  25.  if len(good) > MIN_MATCH_COUNT:
  26.     src_pts = np.float32([kp1[m.queryIdx].pt for m in good]).reshape(-1, 1, 2)
  27.     dst_pts = np.float32([kp2[m.trainIdx].pt for m in good]).reshape(-1, 1, 2)
  28.  
  29.     M, mask = cv2.findHomography(src_pts, dst_pts, cv2.RANSAC, 5.0)
  30.     matchesMask = mask.ravel().tolist()
  31.     return  [True,"%d/%d" % (len(good), MIN_MATCH_COUNT),matchesMask]
  32.  else:
  33.     matchesMask = None
  34.     return  [False,"%d/%d" % (len(good), MIN_MATCH_COUNT),matchesMask]
Add Comment
Please, Sign In to add comment