亲宝软件园·资讯

展开

Python OpenCV drawMatches()

乔卿 人气:0

作用说明

该方法被用于绘制关键点的匹配情况。我们看到的许多匹配结果都是使用这一方法绘制的——一左一右两张图像,匹配的关键点之间用线条链接。

函数原型

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

参数详解

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

结果

加载全部内容

相关教程
猜你喜欢
用户评论