python提取两幅相似人脸图像的ORB特征,进行关键点匹配,匹配结果以图像形式给出
import cv2
img1 = cv2.imread(r'E:\test.bmp',-1)
img2 = cv2.imread(r'E:\test.bmp',-1)
orb = cv2.ORB_create()
kp1 = orb.detect(img1)
kp2 = orb.detect(img2)
kp1, des1 = orb.compute(img1, kp1)
kp2, des2 = orb.compute(img2, kp2)
print("开始匹配")
bf = cv2.BFMatcher(cv2.NORM_HAMMING)
matches = bf.match(des1, des2)
print("匹配结束")
dist_list = [x.distance for x in matches]
min_dist = min(dist_list)
# 去除差点
good_match = []
for x in matches:
if x.distance <= max(2 * min_dist, 30):
good_match.append(x)
outimage = cv2.drawMatches(img1, kp1, img2, kp2, good_match, outImg=None)
cv2.imshow("ret", outimage)
cv2.waitKey(0)
cv2.destroyAllWindows()