opencv-python 提取sift特征并匹配的實例
更新時間:2019年12月09日 09:57:46 作者:Yan456jie
今天小編就為大家分享一篇opencv-python 提取sift特征并匹配的實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
我就廢話不多說,直接上代碼吧!
# -*- coding: utf-8 -*- import cv2 import numpy as np from find_obj import filter_matches,explore_match from matplotlib import pyplot as plt def getSift(): ''' 得到并查看sift特征 ''' img_path1 = '../../data/home.jpg' #讀取圖像 img = cv2.imread(img_path1) #轉換為灰度圖 gray= cv2.cvtColor(img,cv2.COLOR_BGR2GRAY) #創(chuàng)建sift的類 sift = cv2.SIFT() #在圖像中找到關鍵點 也可以一步計算#kp, des = sift.detectAndCompute kp = sift.detect(gray,None) print type(kp),type(kp[0]) #Keypoint數(shù)據(jù)類型分析 http://www.cnblogs.com/cj695/p/4041399.html print kp[0].pt #計算每個點的sift des = sift.compute(gray,kp) print type(kp),type(des) #des[0]為關鍵點的list,des[1]為特征向量的矩陣 print type(des[0]), type(des[1]) print des[0],des[1] #可以看出共有885個sift特征,每個特征為128維 print des[1].shape #在灰度圖中畫出這些點 img=cv2.drawKeypoints(gray,kp) #cv2.imwrite('sift_keypoints.jpg',img) plt.imshow(img),plt.show() def matchSift(): ''' 匹配sift特征 ''' img1 = cv2.imread('../../data/box.png', 0) # queryImage img2 = cv2.imread('../../data/box_in_scene.png', 0) # trainImage sift = cv2.SIFT() kp1, des1 = sift.detectAndCompute(img1, None) kp2, des2 = sift.detectAndCompute(img2, None) # 蠻力匹配算法,有兩個參數(shù),距離度量(L2(default),L1),是否交叉匹配(默認false) bf = cv2.BFMatcher() #返回k個最佳匹配 matches = bf.knnMatch(des1, des2, k=2) # cv2.drawMatchesKnn expects list of lists as matches. #opencv2.4.13沒有drawMatchesKnn函數(shù),需要將opencv2.4.13\sources\samples\python2下的common.py和find_obj文件放入當前目錄,并導入 p1, p2, kp_pairs = filter_matches(kp1, kp2, matches) explore_match('find_obj', img1, img2, kp_pairs) # cv2 shows image cv2.waitKey() cv2.destroyAllWindows() def matchSift3(): ''' 匹配sift特征 ''' img1 = cv2.imread('../../data/box.png', 0) # queryImage img2 = cv2.imread('../../data/box_in_scene.png', 0) # trainImage sift = cv2.SIFT() kp1, des1 = sift.detectAndCompute(img1, None) kp2, des2 = sift.detectAndCompute(img2, None) # 蠻力匹配算法,有兩個參數(shù),距離度量(L2(default),L1),是否交叉匹配(默認false) bf = cv2.BFMatcher() #返回k個最佳匹配 matches = bf.knnMatch(des1, des2, k=2) # cv2.drawMatchesKnn expects list of lists as matches. #opencv3.0有drawMatchesKnn函數(shù) # Apply ratio test # 比值測試,首先獲取與A 距離最近的點B(最近)和C(次近),只有當B/C # 小于閾值時(0.75)才被認為是匹配,因為假設匹配是一一對應的,真正的匹配的理想距離為0 good = [] for m, n in matches: if m.distance < 0.75 * n.distance: good.append([m]) img3 = cv2.drawMatchesKnn(img1, kp1, img2, kp2, good[:10], None, flags=2) cv2.drawm plt.imshow(img3), plt.show() matchSift()
以上這篇opencv-python 提取sift特征并匹配的實例就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
簡單介紹Python中的try和finally和with方法
這篇文章主要介紹了Python中的try和finally和with方法,是Python學習當中的基礎知識,需要的朋友可以參考下2015-05-05python之文件的讀寫和文件目錄以及文件夾的操作實現(xiàn)代碼
這篇文章主要介紹了python之文件的讀寫和文件目錄以及文件夾的操作實現(xiàn)代碼,需要的朋友可以參考下2016-08-08pytorch實現(xiàn)下載加載mnist數(shù)據(jù)集
這篇文章主要介紹了pytorch實現(xiàn)下載加載mnist數(shù)據(jù)集方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-06-06