先贴两张原图
SIFT暴力匹配
img_s = cv2.imread("0.png")
img_d = cv2.imread("1.jpg")
# 根据图片大小,做适当的缩放
# img_s = cv2.resize(img_s, None, fx=0.2, fy=0.2)
img_d = cv2.resize(img_d, None, fx=0.5, fy=0.5)
gray_s = cv2.cvtColor(img_s, cv2.COLOR_BGR2GRAY)
gray_d = cv2.cvtColor(img_d, cv2.COLOR_BGR2GRAY)
# 使用SIFT特征检测
detect = cv2.SIFT.create()
kp_s, des_s = detect.detectAndCompute(gray_s, None)
kp_d, des_d = detect.detectAndCompute(gray_d, None)
# 使用暴力匹配器
# NORM_L1和NORM_L2是专门给SIFT使用的
# True表示交叉查找(即用图1的特征匹配图2的,图2也要用特征匹配图1的,要匹配两轮)
bf = cv2.BFMatcher(cv2.NORM_L1, True)
# 进行匹配
matches = bf.match(des_s, des_d)
# 进行匹配的结果的视觉呈现
img_result = cv2.drawMatches(img_s, kp_s, img_d, kp_d, matches, None)
cv2.imshow("img", img_result)
cv2.waitKey()
ORB暴力匹配
img_s = cv2.imread("0.png")
img_d = cv2.imread("1.jpg")
# img_s = cv2.resize(img_s, None, fx=0.2, fy=0.2)
img_d = cv2.resize(img_d, None, fx=0.5, fy=0.5)
gray_s = cv2.cvtColor(img_s, cv2.COLOR_BGR2GRAY)
gray_d = cv2.cvtColor(img_d, cv2.COLOR_BGR2GRAY)
gray_s = img_s
gray_d = img_d
# 使用ORB特征检测,检测10000个特征点
detect = cv2.ORB.create(nfeatures=10000)
kp_s, des_s = detect.detectAndCompute(gray_s, None)
kp_d, des_d = detect.detectAndCompute(gray_d, None)
# NORM_HAMMING和NORM_HAMMING2是专门给ORB使用的
bf = cv2.BFMatcher(cv2.NORM_HAMMING, True)
matches = bf.match(des_s, des_d)
img_result = cv2.drawMatches(img_s, kp_s, img_d, kp_d, matches, None)
cv2.imshow("img", img_result)
cv2.waitKey()