opencv-python 提取sift特征并匹配的實(shí)例
更新時(shí)間:2019年12月09日 09:57:46 作者:Yan456jie
今天小編就為大家分享一篇opencv-python 提取sift特征并匹配的實(shí)例,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧
我就廢話不多說,直接上代碼吧!
# -*- 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) #轉(zhuǎn)換為灰度圖 gray= cv2.cvtColor(img,cv2.COLOR_BGR2GRAY) #創(chuàng)建sift的類 sift = cv2.SIFT() #在圖像中找到關(guān)鍵點(diǎn) 也可以一步計(jì)算#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 #計(jì)算每個點(diǎn)的sift des = sift.compute(gray,kp) print type(kp),type(des) #des[0]為關(guān)鍵點(diǎn)的list,des[1]為特征向量的矩陣 print type(des[0]), type(des[1]) print des[0],des[1] #可以看出共有885個sift特征,每個特征為128維 print des[1].shape #在灰度圖中畫出這些點(diǎn) 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),是否交叉匹配(默認(rèn)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文件放入當(dāng)前目錄,并導(dǎo)入 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),是否交叉匹配(默認(rèn)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 距離最近的點(diǎn)B(最近)和C(次近),只有當(dāng)B/C # 小于閾值時(shí)(0.75)才被認(rèn)為是匹配,因?yàn)榧僭O(shè)匹配是一一對應(yīng)的,真正的匹配的理想距離為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特征并匹配的實(shí)例就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
簡單介紹Python中的try和finally和with方法
這篇文章主要介紹了Python中的try和finally和with方法,是Python學(xué)習(xí)當(dāng)中的基礎(chǔ)知識,需要的朋友可以參考下2015-05-05python之文件的讀寫和文件目錄以及文件夾的操作實(shí)現(xiàn)代碼
這篇文章主要介紹了python之文件的讀寫和文件目錄以及文件夾的操作實(shí)現(xiàn)代碼,需要的朋友可以參考下2016-08-08深入學(xué)習(xí)Python可變與不可變對象操作實(shí)例
Python中的數(shù)據(jù)類型可以分為可變對象和不可變對象,了解它們之間的區(qū)別對于編寫高效的Python代碼至關(guān)重要,本文將詳細(xì)介紹可變對象和不可變對象的概念,以及如何正確地使用它們來提高代碼的性能和可讀性2023-12-12淺談python 調(diào)用open()打開文件時(shí)路徑出錯的原因
這篇文章主要介紹了淺談python 調(diào)用open()打開文件時(shí)路徑出錯的原因,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-06-06pytorch實(shí)現(xiàn)下載加載mnist數(shù)據(jù)集
這篇文章主要介紹了pytorch實(shí)現(xiàn)下載加載mnist數(shù)據(jù)集方式,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-06-06