python opencv特征点提取和匹配

也可以说是特征点的检测与匹配;
特征点检测的算法主要有sift和surf,找到两张图片里的所有特征点;

img1_gray = cv2.imread("1.jpg")
img2_gray = cv2.imread("2.jpg")
    
sift = cv2.xfeatures2d.SIFT_create()
# sift = cv2.SURF()
    
kp1, des1 = sift.detectAndCompute(img1_gray, None)
kp2, des2 = sift.detectAndCompute(img2_gray, None)

特征点匹配用的是BFMatcher,brute force暴力匹配,就是选取几个最近的,参考链接里的代码选取了两个;

bf = cv2.BFMatcher(cv2.NORM_L2)
matches = bf.knnMatch(des1, des2, k=2)

优化就是限定一个条件,如第二近的点距离要大于最近点距离的两倍才算好的match;

goodMatch = []
for m, n in matches:
    if m.distance < 0.50 * n.distance:
        goodMatch.append(m)

最后是连线可视化。

res = cv2.drawMatches(img1_gray, kp1, img2_gray, kp2, goodMatch[:], None, flags=2)
cv2.imshow('res', res)
cv2.waitKey(0)
cv2.destroyAllWindows()

Reference:
https://blog.csdn.net/dcrmg/article/details/78817988
https://www.cnblogs.com/Jessica-jie/p/8622449.html

你可能感兴趣的:(python opencv特征点提取和匹配)