用OpenCV(Python)获取图像的SIFT特征

import cv2 as cv
import numpy as np
import matplotlib.pyplot as plt
img=cv.imread("../Lena.png")
img_gray=cv.cvtColor(img,cv.COLOR_BGR2GRAY)


#创建一个SIFI对象
sift=cv.SIFT_create()

#使用SIFT对象在灰度图像img_gray中检测关键点,结果存储在变量kp中
kp=sift.detect(img_gray,None)

#计算特征
#des(1100,128),一共有1100个关键点,每个关键点勇(128)长度的特征向量表示
#使用SIFT对象计算img_gray中kp的关键点,
# 计算出的关键点和描述符分别存储在keypoints和des中
kp2,des=sift.compute(img_gray,kp)
print("+++++++++++++++++++?++++++++++++++")
print(np.array(kp).shape) #(1100,)
print(np.array(kp2).shape) #(1100,)
print(des.shape) #(1100,128)
print("+++++++++++++++++++?++++++++++++++")

img2=cv.drawKeypoints(img_gray,kp,img)

plt.imshow(img2,cmap="gray")
plt.title("drawKeyPoints")
plt.show()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
1. 导入库 ``` import cv2 import numpy as np ``` 2. 读取图片 ``` img1 = cv2.imread('image1.jpg') img2 = cv2.imread('image2.jpg') ``` 3. 提取关键点和特征向量 ``` sift = cv2.xfeatures2d.SIFT_create() kp1, des1 = sift.detectAndCompute(img1,None) kp2, des2 = sift.detectAndCompute(img2,None) ``` 4. 匹配特征点 ``` bf = cv2.BFMatcher() matches = bf.knnMatch(des1,des2,k=2) ``` 5. 筛选匹配点 ``` good = [] for m,n in matches: if m.distance < 0.75*n.distance: good.append(m) ``` 6. 计算变换矩阵 ``` src_pts = np.float32([ kp1[m.queryIdx].pt for m in good ]).reshape(-1,1,2) dst_pts = np.float32([ kp2[m.trainIdx].pt for m in good ]).reshape(-1,1,2) M, mask = cv2.findHomography(src_pts, dst_pts, cv2.RANSAC,5.0) ``` 7. 图像拼接 ``` h,w = img1.shape[:2] pts = np.float32([ [0,0],[0,h-1],[w-1,h-1],[w-1,0] ]).reshape(-1,1,2) dst = cv2.perspectiveTransform(pts,M) dst = np.int32(dst) offset = np.array([w,0]) dst += offset result_img = cv2.polylines(img2,[dst],True,(255,0,0),3, cv2.LINE_AA) result_img[offset[1]:h+offset[1],:w,:] = img1 ``` 完整代码: ``` import cv2 import numpy as np img1 = cv2.imread('image1.jpg') img2 = cv2.imread('image2.jpg') sift = cv2.xfeatures2d.SIFT_create() kp1, des1 = sift.detectAndCompute(img1,None) kp2, des2 = sift.detectAndCompute(img2,None) bf = cv2.BFMatcher() matches = bf.knnMatch(des1,des2,k=2) good = [] for m,n in matches: if m.distance < 0.75*n.distance: good.append(m) src_pts = np.float32([ kp1[m.queryIdx].pt for m in good ]).reshape(-1,1,2) dst_pts = np.float32([ kp2[m.trainIdx].pt for m in good ]).reshape(-1,1,2) M, mask = cv2.findHomography(src_pts, dst_pts, cv2.RANSAC,5.0) h,w = img1.shape[:2] pts = np.float32([ [0,0],[0,h-1],[w-1,h-1],[w-1,0] ]).reshape(-1,1,2) dst = cv2.perspectiveTransform(pts,M) dst = np.int32(dst) offset = np.array([w,0]) dst += offset result_img = cv2.polylines(img2,[dst],True,(255,0,0),3, cv2.LINE_AA) result_img[offset[1]:h+offset[1],:w,:] = img1 cv2.imshow("Result", result_img) cv2.waitKey(0) cv2.destroyAllWindows() ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值