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()
文章來源:http://www.zghlxwxcb.cn/news/detail-628464.html
?文章來源地址http://www.zghlxwxcb.cn/news/detail-628464.html
到了這里,關于特征點匹配返回匹配坐標點python的文章就介紹完了。如果您還想了解更多內容,請在右上角搜索TOY模板網以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網!