当前位置 主页 > 服务器问题 > Linux/apache问题 >
我就废话不多说,直接上代码吧!
# -*- 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) #创建sift的类 sift = cv2.SIFT() #在图像中找到关键点 也可以一步计算#kp, des = sift.detectAndCompute kp = sift.detect(gray,None) print type(kp),type(kp[0]) #Keypoint数据类型分析 /uploads/cj/201912/4041399.html',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) # 蛮力匹配算法,有两个参数,距离度量(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函数,需要将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) # 蛮力匹配算法,有两个参数,距离度量(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函数 # 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特征并匹配的实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持IIS7站长之家。