特征点匹配返回匹配坐标点python

复制代码
import cv2 as cv
import numpy as np
from matplotlib import pyplot as plt
from scipy import stats


def drawMatchesKnn_cv(img1_gray, kp1, img2_gray, kp2, goodMatch):
    h1, w1 = img1_gray.shape[:2]
    h2, w2 = img2_gray.shape[:2]

    vis = np.zeros((max(h1, h2), w1 + w2, 3), np.uint8)
    vis[:h1, :w1] = img1_gray
    vis[:h2, w1:w1 + w2] = img2_gray

    p1 = [kpp.queryIdx for kpp in goodMatch]
    p2 = [kpp.trainIdx for kpp in goodMatch]

    post1 = np.int32([kp1[pp].pt for pp in p1])
    post2 = np.int32([kp2[pp].pt for pp in p2]) + (w1, 0)

    for (x1, y1), (x2, y2) in zip(post1, post2):
        cv.line(vis, (x1, y1), (x2, y2), (0, 0, 255))

    cv.namedWindow("match", cv.WINDOW_NORMAL)
    cv.imshow("match", vis)



img1_gray = cv.imread("D:/dht/left1.png", 0)
img2_gray = cv.imread("D:/dht/right1.png", 0)
img1_gray = cv.resize(img1_gray, (1800, 2400))
img2_gray = cv.resize(img2_gray, (1800, 2400))

# sift = cv.SIFT()
# sift = cv.xfeatures2d.SIFT_create()
sift = cv.SIFT_create()
# sift = cv.SURF()
kp1, des1 = sift.detectAndCompute(img1_gray, None)
kp2, des2 = sift.detectAndCompute(img2_gray, None)
# 设置Flannde参数
FLANN_INDEX_KDTREE = 0
indexParams = dict(algorithm=FLANN_INDEX_KDTREE, trees=5)
searchParams = dict(checks=50)
flann = cv.FlannBasedMatcher(indexParams, searchParams)
matches = flann.knnMatch(des1, des2, k=2)
# bf = cv.BFMatcher(cv.NORM_L2)
# matches = bf.knnMatch(des1,des2,k=2)
# 设置好初始匹配值
destinationx = []
destinationy = []
matchesMask = [[0, 0] for i in range(len(matches))]
for i, (m, n) in enumerate(matches):
    if m.distance < 0.5 * n.distance:  # 舍弃小于0.5的匹配结果
        matchesMask[i] = [1, 0]
        # matchesMask[i] = [1, 0]
        pt1 = kp1[m.queryIdx].pt  # trainIdx    是匹配之后所对应关键点的序号,第一个载入图片的匹配关键点序号
        pt2 = kp2[n.trainIdx].pt  # queryIdx  是匹配之后所对应关键点的序号,第二个载入图片的匹配关键点序号
        destinationx.append(int(pt1[0]-pt2[0]))
        destinationy.append(int(pt1[1]-pt2[1]))
        # print(kpts1)
        print(i, pt1, pt2)


# counts = np.bincount(destinationx)
print(destinationx)
#print(stats.mode(destinationx)[0][0])

#返回众数
# print(np.argmax(counts))

# counts = np.bincount(destinationy)
#返回众数
print(destinationy)
#print(stats.mode(destinationy)[0][0])

drawParams=dict(matchColor=(0,0,255),singlePointColor=(255,0,0),matchesMask=matchesMask,flags=0) #给特征点和匹配的线定义颜色
resultimage=cv.drawMatchesKnn(img1_gray,kp1,img2_gray,kp2,matches,None,**drawParams) #画出匹配的结果
plt.imshow(resultimage)
plt.show()
相关推荐
埃菲尔铁塔_CV算法21 分钟前
基于 TOF 图像高频信息恢复 RGB 图像的原理、应用与实现
人工智能·深度学习·数码相机·算法·目标检测·计算机视觉
天天爱吃肉821835 分钟前
ZigBee通信技术全解析:从协议栈到底层实现,全方位解读物联网核心无线技术
python·嵌入式硬件·物联网·servlet
Allen_LVyingbo1 小时前
Python常用医疗AI库以及案例解析(2025年版、上)
开发语言·人工智能·python·学习·健康医疗
jndingxin1 小时前
OpenCV中超分辨率(Super Resolution)模块类cv::dnn_superres::DnnSuperResImpl
人工智能·opencv·dnn
智能砖头1 小时前
LangChain 与 LlamaIndex 深度对比与选型指南
人工智能·python
Trent19852 小时前
影楼精修-智能修图Agent
图像处理·人工智能·计算机视觉·aigc
风逸hhh3 小时前
python打卡day58@浙大疏锦行
开发语言·python
烛阴4 小时前
一文搞懂 Python 闭包:让你的代码瞬间“高级”起来!
前端·python
JosieBook4 小时前
【Java编程动手学】Java中的数组与集合
java·开发语言·python
Blossom.1185 小时前
机器学习在智能供应链中的应用:需求预测与物流优化
人工智能·深度学习·神经网络·机器学习·计算机视觉·机器人·语音识别