tomateblue

DetectarPlacasOpenV1

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