import cv2
import numpy as np
def drawMatchesKnn_cv2(img1_gray,kp1,img2_gray,kp2,goodMatch):
h1, w1 = img1_gray.shape[:2]
h2, w2 = img2_gray.shape[:2]
vis = np.zeros((max(h1, h2), w1 + w2, 3), np.uint8)
vis[:h1, :w1] = img1_gray
vis[:h2, w1:w1 + w2] = img2_gray
p1 = [kpp.queryIdx for kpp in goodMatch]
p2 = [kpp.trainIdx for kpp in goodMatch]
post1 = np.int32([kp1[pp].pt for pp in p1])
post2 = np.int32([kp2[pp].pt for pp in p2]) + (w1, 0)
for (x1, y1), (x2, y2) in zip(post1, post2):
cv2.line(vis, (x1, y1), (x2, y2), (0,0,255))
cv2.namedWindow("match",cv2.WINDOW_NORMAL)
cv2.imshow("match", vis)
img1_gray = cv2.imread("D:\\05.jpg")
img2_gray = cv2.imread("D:\\06.jpg")
sift = cv2.SIFT()
#sift = cv2.SURF()
kp1, des1 = sift.detectAndCompute(img1_gray, None)
kp2, des2 = sift.detectAndCompute(img2_gray, None)
# BFmatcher with default parms
bf = cv2.BFMatcher(cv2.NORM_L2)
matches = bf.knnMatch(des1, des2, k = 2)
goodMatch = []
for m,n in matches:
if m.distance < 0.50*n.distance:
goodMatch.append(m)
drawMatchesKnn_cv2(img1_gray,kp1,img2_gray,kp2,goodMatch[:20])
cv2.waitKey(0)
cv2.destroyAllWindows()
sift特征匹配效果:
SURF特征匹配效果:
- 1. Sift特征和Surf特征提取特征的方法略有差异,在整个匹配流程上一样
- 2. knnMatch(des1, des2,k = 2) 函数执行特征点匹配, k = 2 定义基准图像上的一个点会在另一幅图像上有2个匹配结果。
- 3. 不论Sift还是Surf都是强制匹配,不能保证匹配的点就是准确的,只能保证相对正确。
- 4. goodMatch是经过筛选的优质配对,如果2个配对中第一匹配的距离小于第二匹配的距离的0.5,基本可以说明这个第一配对是两幅图像中独特的,不重复的特征点。当然并不能保证goodMatch保留的就是最优匹配。
- 5. OpenCV3中有drawMatchesKnn()函数,可以直接拼接显示配对点,OpenCV2中没有这个函数,定义的drawMatchesKnn_cv2()实现了配对显示部分功能。