python-应用OpenCV和Python进行SIFT算法的实现
程序员文章站
2022-07-11 16:16:44
如下图为进行测试的q和h,分别验证基于BFmatcher、FlannBasedMatcher等的SIFT算法 代码如下: 效果如下: ......
如下图为进行测试的q和h,分别验证基于bfmatcher、flannbasedmatcher等的sift算法
代码如下:
import numpy as np import cv2 from matplotlib import pyplot as plt imgname1 = 'g:/q.jpg' imgname2 = 'g:/h.jpg' sift=cv2.xfeatures2d.sift_create() img1 = cv2.imread(imgname1) gray1 = cv2.cvtcolor(img1, cv2.color_bgr2gray) #灰度处理图像 kp1, des1 = sift.detectandcompute(img1,none) #des是描述子 img2 = cv2.imread(imgname2) gray2 = cv2.cvtcolor(img2, cv2.color_bgr2gray)#灰度处理图像 kp2, des2 = sift.detectandcompute(img2,none) #des是描述子 #hmerge = np.hstack((gray1, gray2)) #水平拼接 #cv2.imshow("gray", hmerge) #拼接显示为gray #cv2.waitkey(0) #img3 = cv2.drawkeypoints(img1,kp1,img1,color=(255,0,255)) #画出特征点,并显示为红色圆圈 #img4 = cv2.drawkeypoints(img2,kp2,img2,color=(255,0,255)) #画出特征点,并显示为红色圆圈 #hmerge = np.hstack((img3, img4)) #水平拼接 #cv2.imshow("point", hmerge) #拼接显示为gray #cv2.waitkey(0) # bfmatcher解决匹配 bf = cv2.bfmatcher() matches = bf.knnmatch(des1,des2, k=2) # 调整ratio good = [] for m,n in matches: if m.distance < 0.75*n.distance: good.append([m]) img5 = cv2.drawmatchesknn(img1,kp1,img2,kp2,good,none,flags=2) cv2.imshow("bfmatch", img5) cv2.waitkey(0) cv2.destroyallwindows()
效果如下:
上一篇: Python中的字典与集合