opencv-python提取sift特征并匹配的实例-创新互联

我就废话不多说,直接上代码吧!

创新互联主要从事成都网站制作、网站建设、网页设计、企业做网站、公司建网站等业务。立足成都服务陆港,10多年网站建设经验,价格优惠、服务专业,欢迎来电咨询建站服务:18982081108
# -*- 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_path2 = '../../data/home.jpg'
  #读取图像
  img = cv2.imread(img_path2)
  #转换为灰度图
  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数据类型分析 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)
  # 蛮力匹配算法,有两个参数,距离度量(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()

另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。

文章名称:opencv-python提取sift特征并匹配的实例-创新互联
网站链接:https://www.cdcxhl.com/article2/dhhiic.html

成都网站建设公司_创新互联,为您提供网站收录服务器托管营销型网站建设ChatGPT网页设计公司网站排名

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联

手机网站建设