1 import cv2 as cv 2 import numpy as np 3 4 5 # def draw_keypoints(img, keypoints): 6 # for kp in keypoints: 7 # x, y = kp.pt 8 # cv.circle(img, (int(x), int(y)), 2, (0, 255, 0)) 9 10 # 两张键盘图片,用于特征匹配 11 img1 = cv.imread('../images/keyboard1.jpg') 12 img2 = cv.imread('../images/keyboard2.jpg') 13 gray1 = cv.cvtColor(img1, cv.COLOR_BGR2GRAY) 14 gray2 = cv.cvtColor(img2, cv.COLOR_BGR2GRAY) 15 orb = cv.ORB_create() 16 kp1, des1 = orb.detectAndCompute(gray1, None) # 计算特征点和描述符 17 kp2, des2 = orb.detectAndCompute(gray2, None) 18 bf = cv.BFMatcher(cv.NORM_HAMMING, crossCheck=True) # 建立匹配关系 19 matches = bf.match(des1, des2) # 匹配描述符 20 matches = sorted(matches, key=lambda x:x.distance) # 根据距离来排序 21 # img3 = cv.drawMatches(gray1, kp1, gray2, kp2, matches[:10], gray2, flags=2) 22 img3 = cv.drawMatches(img1, kp1, img2, kp2, matches[:10], img2, flags=2) 23 cv.imshow('img3', img3) 24 cv.waitKey(0) 25 cv.destroyAllWindows()
效果:
本来还想看看SIFT和SURF的效果,但是模块没有了,也有说安装opencv-contrib-python的,但是安装后好像还要操作一下,也没写清楚
参考链接:
OpenCV+Python特征提取算法与图像描述符之SIFT / SURF / ORB
OpenCV探索之路(二十三):特征检测和特征匹配方法汇总