前言
orb特征检测和二进制描述符算法采用了定向的FAST检测方法和旋转的BRIEF描述符。
代码
from skimage import transform as transform
from skimage.feature import (match_descriptors,ORB,plot_matches)
from skimage.color import rgb2gray
from skimage.io import imread
import matplotlib.pyplot as plt
if __name__ == '__main__':
img1 = rgb2gray(imread('../images/me5.jpg'))
img4 = transform.resize(rgb2gray(imread('../images/me6.jpg')),
img1.shape,anti_aliasing=True)
descriptor_extractor = ORB(n_keypoints=200)
descriptor_extractor.detect_and_extract(img1)
keypoints1,descriptors1 = descriptor_extractor.keypoints,descriptor_extractor.descriptors
descriptor_extractor.detect_and_extract(img4)
keypoints4,descriptors4 = descriptor_extractor.keypoints,descriptor_extractor.descriptors
print(len(keypoints1),len(keypoints4))
matches14 = match_descriptors(descriptors1,descriptors4,cross_check=True)
print(matches14.shape)
# matches14 = matches14[5:10]
pig,ax = plt.subplots(1,1,figsize=(15,15))
plot_matches(ax,img1,img4,keypoints1,keypoints4,matches14)
ax.axis('off'),ax.set_title('image1 vs image4',size=10)
plt.show()
结果
基于opencv的orb特征匹配
代码
import matplotlib.pyplot as plt
import cv2
if __name__ == '__main__':
img1 = cv2.imread('../images/books.png',0)
img2 = cv2.imread('../images/book.png',0)
orb = cv2.ORB_create()
kp1,des1 = orb.detectAndCompute(img1,None)
kp2,des2 = orb.detectAndCompute(img2,None)
bf = cv2.BFMatcher(cv2.NORM_HAMMING,crossCheck=True)
matches = bf.match(des1,des2)
matches = sorted(matches,key=lambda x:x.distance)
print(len(matches))
# 画前20个匹配点
img3 = cv2.drawMatches(img1,kp1,img2,kp2,matches[:20],None,flags=2)
plt.figure(figsize=(20,10)),plt.imshow(img3),plt.show()