featuredetector.py 文件源码

python
阅读 19 收藏 0 点赞 0 评论 0

项目:bib-tagger 作者: KateRita 项目源码 文件源码
def findMatchesBetweenImages(image_1, image_2):
  """ Return the top 10 list of matches between two input images.

  This function detects and computes SIFT (or ORB) from the input images, and
  returns the best matches using the normalized Hamming Distance.

  Args:
    image_1 (numpy.ndarray): The first image (grayscale).
    image_2 (numpy.ndarray): The second image. (grayscale).

  Returns:
    image_1_kp (list): The image_1 keypoints, the elements are of type
                       cv2.KeyPoint.
    image_2_kp (list): The image_2 keypoints, the elements are of type
                       cv2.KeyPoint.
    matches (list): A list of matches, length 10. Each item in the list is of
                    type cv2.DMatch.

  """
  # matches - type: list of cv2.DMath
  matches = None
  # image_1_kp - type: list of cv2.KeyPoint items.
  image_1_kp = None
  # image_1_desc - type: numpy.ndarray of numpy.uint8 values.
  image_1_desc = None
  # image_2_kp - type: list of cv2.KeyPoint items.
  image_2_kp = None
  # image_2_desc - type: numpy.ndarray of numpy.uint8 values.
  image_2_desc = None

  # WRITE YOUR CODE HERE.
  #init
  sift = SIFT()

  #1. Compute SIFT keypoints and descriptors for both images
  image_1_kp, image_1_desc = sift.detectAndCompute(image_1,None)
  image_2_kp, image_2_desc = sift.detectAndCompute(image_2,None)

  #2. Create a Brute Force Matcher, using the hamming distance (and set crossCheck to true).
  #create BFMatcher object
  bf = cv2.BFMatcher(cv2.NORM_HAMMING, crossCheck=True)

  #3. Compute the matches between both images.
  #match descriptors
  matches = bf.match(image_1_desc,image_2_desc)

  #4. Sort the matches based on distance so you get the best matches.
  matches = sorted(matches, key=lambda x: x.distance)

  #5. Return the image_1 keypoints, image_2 keypoints, and the top 10 matches in a list.

  return image_1_kp, image_2_kp, matches[:10]
  # END OF FUNCTION.
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号