赞
踩
import cv2 as cv
import numpy as np
# 读取图片
img1 = cv.imread('1.png')
img2 = cv.imread('2.png')
# # 灰度化
# gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY)
gray1 = cv.cvtColor(img1, cv.COLOR_BGR2GRAY)
gray2 = cv.cvtColor(img2, cv.COLOR_BGR2GRAY)
# # 检测特征点+描述子
sift = cv.xfeatures2d.SIFT_create() # 创建SIFT对象
# # surf = cv.xfeatures2d.SIFT_create() # 创建SURF对象
# # orb = cv.ORB_create() # 创建ORB对象
# # kp, des = sift.detectAndCompute(gray, None) # 对整个图像进行检测,掩码设为None
# # kp, des = surf.detectAndCompute(gray, None) # 对整个图像进行检测,掩码设为None
kp1, des1 = sift.detectAndCompute(gray1, None) # 对整个图像进行检测,掩码设为None
kp2, des2 = sift.detectAndCompute(gray2, None) # 对整个图像进行检测,掩码设为None
#
# # 创建匹配器
bf = cv.BFMatcher(cv.NORM_L1)
match = bf.match(des1, des2) # 获得匹配点
img3 = cv.drawMatches(img1, kp1, img2, kp2, match, None) # 绘制匹配点
# # cv.drawKeypoints(gray, kp, img) # 绘制特征点
#
# # 显示图像
cv.imshow('img1', img1)
cv.imshow('img2', img2)
cv.imshow('img3', img3)
cv.waitKey(0)
import cv2 as cv
import numpy as np
# 读取图片
img1 = cv.imread('luban1.png')
img2 = cv.imread('luban2.png')
# 灰度化
gray1 = cv.cvtColor(img1,cv.COLOR_BGR2GRAY)
gray2 = cv.cvtColor(img2,cv.COLOR_BGR2GRAY)
# 创建sift特征匹配器
sift = cv.xfeatures2d.SIFT_create()
# 计算描述子与特征点
kp1, des1 = sift.detectAndCompute(gray1, None)
kp2, des2 = sift.detectAndCompute(gray2, None)
# 创建匹配器
index_params = dict(algorithm=1, trees=5)
search_params = dict(checks=50)
flann = cv.FlannBasedMatcher(index_params, search_params)
# 对描述子进行匹配
matches = flann.knnMatch(des1, des2, k=2)
good = []
for i, (m, n) in enumerate(matches):
if m.distance < 0.7*n.distance:
good.append(m)
# 绘制匹配点
ret = cv.drawMatchesKnn(img1,kp1,img2,kp2,[good],None)
cv.imshow('ret', ret)
cv.waitKey(0)
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。