py实现surf特征提取

import cv2

def main():
    # 加载图像
    image1 = cv2.imread('image1.jpg', cv2.IMREAD_GRAYSCALE)
    image2 = cv2.imread('image2.jpg', cv2.IMREAD_GRAYSCALE)

    # 创建SURF对象
    surf = cv2.xfeatures2d.SURF_create()

    # 检测特征点和描述符
    keypoints1, descriptors1 = surf.detectAndCompute(image1, None)
    keypoints2, descriptors2 = surf.detectAndCompute(image2, None)

    # 绘制特征点
    result_image1 = cv2.drawKeypoints(image1, keypoints1, None, (0, 255, 0), cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS)
    result_image2 = cv2.drawKeypoints(image2, keypoints2, None, (0, 255, 0), cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS)

    # 显示图像
    cv2.imshow("Image 1", result_image1)
    cv2.imshow("Image 2", result_image2)
    cv2.waitKey(0)
    cv2.destroyAllWindows()

if __name__ == "__main__":
    main()
import cv2
import numpy as np

def main():
    # 加载图像
    image1 = cv2.imread('image1.jpg', cv2.IMREAD_GRAYSCALE)
    image2 = cv2.imread('image2.jpg', cv2.IMREAD_GRAYSCALE)

    # 创建SURF对象
    surf = cv2.xfeatures2d.SURF_create()

    # 检测特征点和描述符
    keypoints1, descriptors1 = surf.detectAndCompute(image1, None)
    keypoints2, descriptors2 = surf.detectAndCompute(image2, None)

    # 创建匹配器
    matcher = cv2.DescriptorMatcher_create(cv2.DescriptorMatcher_FLANNBASED)
    matches = matcher.match(descriptors1, descriptors2)

    # 根据距离排序匹配项
    matches = sorted(matches, key=lambda x: x.distance)

    # 提取前10个最佳匹配项
    good_matches = matches[:10]

    # 绘制匹配点和线条
    result_image = cv2.drawMatches(image1, keypoints1, image2, keypoints2, good_matches, None, flags=cv2.DrawMatchesFlags_NOT_DRAW_SINGLE_POINTS)

    # 显示图像
    cv2.imshow("Matches", result_image)
    cv2.waitKey(0)
    cv2.destroyAllWindows()

if __name__ == "__main__":
    main()

你可能感兴趣的:(opencv,计算机视觉,人工智能)