该方法被用于绘制关键点的匹配情况。我们看到的许多匹配结果都是使用这一方法绘制的——一左一右两张图像,匹配的关键点之间用线条链接。
cv.drawMatches( img1, keypoints1, img2, keypoints2, matches1to2, outImg[, matchColor[, singlePointColor[, matchesMask[, flags]]]]) -> outImg cv.drawMatches( img1, keypoints1, img2, keypoints2, matches1to2, outImg, matchesThickness[, matchColor[, singlePointColor[, matchesMask[, flags]]]] ) -> outImg cv.drawMatchesKnn( img1, keypoints1, img2, keypoints2, matches1to2, outImg[, matchColor[, singlePointColor[, matchesMask[, flags]]]]) -> outImg
matchColor==Scalar::all(-1)
时,代表取随机颜色。singlePointColor==Scalar::all(-1)
时,代表取随机颜色。def bf_match(img_path1, img_path2): # 读取两张图像 img1 = cv2.imread(img_path1, cv2.IMREAD_GRAYSCALE) img2 = cv2.imread(img_path2, cv2.IMREAD_GRAYSCALE) # 计算两张图像的SIFT描述符 kp1, des1, _ = sift_algorithm(img_path1) kp2, des2, _ = sift_algorithm(img_path2) # 创建BFMatcher实例 bf = cv2.BFMatcher() # 获得最佳匹配 matches = bf.match(des1, des2) # 绘制匹配结果 # matches = sorted(matches, key = lambda x:x.distance) match_result = cv2.drawMatches(img1, kp1, img2, kp2, matches, None, flags=cv2.DrawMatchesFlags_NOT_DRAW_SINGLE_POINTS) # 显示绘制结果 plt.imshow(match_result) plt.show() return match_result