项目需要,透过可视化的图片连接两张图的相似点检查SIFT之正确率
def drawMatchLine(Img1Path , Img2Path ):
# 读入图片
Img1 = cv2.imread(Img1Path)
Img2 = cv2.imread(Img2Path)
# 创建识别器
detector = cv2.xfeatures2d.SIFT_create(200)
# 计算KeyPoints
kp1 , des1 = detector.detectAndCompute(Img1,None)
kp2 , des2 = detector.detectAndCompute(Img2,None)
# BFMatcher
bf = cv2.BFMatcher()
matchers = bf.knnMatch(des1 , des2 , k=2)
# 相似列表
Match = []
for m,n in matchers:
if m.distance < 0.50*n.distance:
Match.append(m)
# 查看两张图片的宽及高
height1 , width1 = Img1.shape[:2]
height2 , width2 = Img2.shape[:2]
# 像素调整
vis = np.zeros((max(height1, height2), width1 + width2, 3), np.uint8)
vis[:height1, :width1] = Img1
vis[:height2, width1:width1 + width2] = Img2
p1 = [kpp.queryIdx for kpp in Match[:20]]
p2 = [kpp.trainIdx for kpp in Match[:20]]
post1 = np.int32([kp1[pp].pt for pp in p1])
post2 = np.int32([kp2[pp].pt for pp in p2]) + (width1, 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)
cv2.waitKey(0)
cv2.destroyAllWindows()
SIFT算法列出的特征点要再对其阀域做探讨,虽然保存特征点的文件大小不大,但若需要遍历大量资料库时仍需考虑特征点之准确及数量。
有问题欢迎留言讨论指教,谢谢!