opencv使用SIFT算法寻找图片x值y值

网上已经有很多关于SIFT算法的教程了,本篇主要讲应用,毕竟我是做应用的嘛。

本篇教程使用opencv版本为4.5.1。

这是opencv官网使用SIFT的示例代码:

import numpy as np
import cv2 as cv
import matplotlib.pyplot as plt

img1 = cv.imread('box.png',cv.IMREAD_GRAYSCALE)          # queryImage
img2 = cv.imread('box_in_scene.png',cv.IMREAD_GRAYSCALE) # trainImage

# Initiate SIFT detector
sift = cv.SIFT_create()

# find the keypoints and descriptors with SIFT
kp1, des1 = sift.detectAndCompute(img1,None)
kp2, des2 = sift.detectAndCompute(img2,None)

# BFMatcher with default params
bf = cv.BFMatcher()
matches = bf.knnMatch(des1,des2,k=2)

# Apply ratio test
good = []
for m,n in matches:
    if m.distance < 0.75*n.distance:
        good.append([m])

# cv.drawMatchesKnn expects list of lists as matches.
img3 = cv.drawMatchesKnn(img1,kp1,img2,kp2,good,None,flags=cv.DrawMatchesFlags_NOT_DRAW_SINGLE_POINTS)
plt.imshow(img3),plt.show()

这是我自己修改过后可以得出x,y的版本。

import cv2 as cv

dir1 = 'C:\\Users\\luwei\\Desktop\\01.png'
dir2 = 'C:\\Users\\luwei\\Desktop\\02.png'

print(dir1)
print(dir2)

img1 = cv.imread(dir1,cv.IMREAD_GRAYSCALE)          # queryImage
img2 = cv.imread(dir2,cv.IMREAD_GRAYSCALE) # trainImage

# Initiate SIFT detector
sift = cv.SIFT_create()

# find the keypoints and descriptors with SIFT
kp1, des1 = sift.detectAndCompute(img1,None)
kp2, des2 = sift.detectAndCompute(img2,None)

# BFMatcher with default params
bf = cv.BFMatcher()
matches = bf.knnMatch(des1,des2,k=2)

#寻找匹配最好的点,并记录其标识
kp1idx = []
for m,n in matches:
    if m.distance < 0.1*n.distance:
        kp1idx.append(m.queryIdx)

#获取所有关键点的位置
kp1pos = cv.KeyPoint_convert(kp1)

#获取小图在大图中的x和y值
x = 0
y = 0
for idx in kp1idx:
    x = kp1pos[idx][0] + x
    y = kp1pos[idx][1] + y
x = x / len(kp1idx)
y = y / len(kp1idx)

#转换为整数
x = int(x)
y = int(y)


#输出
img3 = img1.copy()
cv.circle(img3,(x,y),5,(0,0,255))

cv.imwrite('C:\\Users\\luwei\\Desktop\\03.png', img3)

稍加改造就成一个函数了

import cv2 as cv

def GetPic(BigImg,SmallImg):
    # Initiate SIFT detector
    sift = cv.SIFT_create()

    # find the keypoints and descriptors with SIFT
    kp1, des1 = sift.detectAndCompute(BigImg,None)
    kp2, des2 = sift.detectAndCompute(SmallImg,None)

    # BFMatcher with default params
    bf = cv.BFMatcher()
    matches = bf.knnMatch(des1,des2,k=2)

    #寻找匹配最好的点,并记录其标识
    kp1idx = []
    for m,n in matches:
        if m.distance < 0.1*n.distance:
            kp1idx.append(m.queryIdx)

    #获取所有关键点的位置
    kp1pos = cv.KeyPoint_convert(kp1)

    #获取小图在大图中的x和y值
    x = 0
    y = 0
    for idx in kp1idx:
        x = kp1pos[idx][0] + x
        y = kp1pos[idx][1] + y
    x = x / len(kp1idx)
    y = y / len(kp1idx)

    #转换为整数
    x = int(x)
    y = int(y)

    #返回x,y
    return x,y

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值