主頁 > 知識庫 > Opencv Python實(shí)現(xiàn)兩幅圖像匹配

Opencv Python實(shí)現(xiàn)兩幅圖像匹配

熱門標(biāo)簽:百度地圖標(biāo)注位置怎么修改 梅州外呼業(yè)務(wù)系統(tǒng) 北京電信外呼系統(tǒng)靠譜嗎 無錫客服外呼系統(tǒng)一般多少錢 大連crm外呼系統(tǒng) 洪澤縣地圖標(biāo)注 老人電話機(jī)器人 高德地圖標(biāo)注是免費(fèi)的嗎 地圖標(biāo)注視頻廣告

本文實(shí)例為大家分享了Opencv Python實(shí)現(xiàn)兩幅圖像匹配的具體代碼,供大家參考,具體內(nèi)容如下

原圖

import cv2

img1 = cv2.imread('SURF_2.jpg', cv2.IMREAD_GRAYSCALE)
img1 = cv2.resize(img1,dsize=(600,400))
img2 = cv2.imread('SURF_1.jpg', cv2.IMREAD_GRAYSCALE)
img2 = cv2.resize(img2,dsize=(600,400))
image1 = img1.copy()
image2 = img2.copy()


#創(chuàng)建一個(gè)SURF對象
surf = cv2.xfeatures2d.SURF_create(25000)
#SIFT對象會使用Hessian算法檢測關(guān)鍵點(diǎn),并且對每個(gè)關(guān)鍵點(diǎn)周圍的區(qū)域計(jì)算特征向量。該函數(shù)返回關(guān)鍵點(diǎn)的信息和描述符
keypoints1,descriptor1 = surf.detectAndCompute(image1,None)
keypoints2,descriptor2 = surf.detectAndCompute(image2,None)
# print('descriptor1:',descriptor1.shape(),'descriptor2',descriptor2.shape())
#在圖像上繪制關(guān)鍵點(diǎn)
image1 = cv2.drawKeypoints(image=image1,keypoints = keypoints1,outImage=image1,color=(255,0,255),flags=cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS)
image2 = cv2.drawKeypoints(image=image2,keypoints = keypoints2,outImage=image2,color=(255,0,255),flags=cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS)
#顯示圖像
cv2.imshow('surf_keypoints1',image1)
cv2.imshow('surf_keypoints2',image2)
cv2.waitKey(20)


matcher = cv2.FlannBasedMatcher()
matchePoints = matcher.match(descriptor1,descriptor2)
# print(type(matchePoints),len(matchePoints),matchePoints[0])

#提取強(qiáng)匹配特征點(diǎn)
minMatch = 1
maxMatch = 0
for i in range(len(matchePoints)):
    if minMatch > matchePoints[i].distance:
        minMatch = matchePoints[i].distance
    if maxMatch  matchePoints[i].distance:
        maxMatch = matchePoints[i].distance
    print('最佳匹配值是:',minMatch)
    print('最差匹配值是:',maxMatch)

#獲取排雷在前邊的幾個(gè)最優(yōu)匹配結(jié)果
goodMatchePoints = []
for i in range(len(matchePoints)):
    if matchePoints[i].distance  minMatch + (maxMatch-minMatch)/16:
        goodMatchePoints.append(matchePoints[i])

#繪制最優(yōu)匹配點(diǎn)
outImg = None
outImg = cv2.drawMatches(img1,keypoints1,img2,keypoints2,goodMatchePoints,outImg,
                         matchColor=(0,255,0),flags=cv2.DRAW_MATCHES_FLAGS_DEFAULT)
cv2.imshow('matche',outImg)
cv2.waitKey(0)
cv2.destroyAllWindows()

原圖

#coding=utf-8
import cv2
from matplotlib import pyplot as plt

img=cv2.imread('xfeatures2d.SURF_create2.jpg',0)
# surf=cv2.SURF(400)   #Hessian閾值400
# kp,des=surf.detectAndCompute(img,None)
# leng=len(kp)
# print(leng)
# 關(guān)鍵點(diǎn)太多,重取閾值

surf=cv2.cv2.xfeatures2d.SURF_create(50000)   #Hessian閾值50000
kp,des=surf.detectAndCompute(img,None)
leng=len(kp)
print(leng)

img2=cv2.drawKeypoints(img,kp,None,(255,0,0),4)
plt.imshow(img2)
plt.show()

# 下面是U-SURF算法,關(guān)鍵點(diǎn)朝向一致,運(yùn)算速度加快。
surf.upright=True
kp=surf.detect(img,None)
img3=cv2.drawKeypoints(img,kp,None,(255,0,0),4)

plt.imshow(img3)
plt.show()

#檢測關(guān)鍵點(diǎn)描述符大小,改64維成128維
surf.extended=True
kp,des=surf.detectAndCompute(img,None)
dem1=surf.descriptorSize()
print(dem1)
shp1=des.shape()
print(shp1)

效果圖

import cv2
from matplotlib import pyplot as plt

leftImage = cv2.imread('xfeatures2d.SURF_create_1.jpg')
rightImage = cv2.imread('xfeatures2d.SURF_create_2.jpg')

# 創(chuàng)造sift
sift = cv2.xfeatures2d.SIFT_create()
kp1, des1 = sift.detectAndCompute(leftImage, None)
kp2, des2 = sift.detectAndCompute(rightImage, None)  # 返回關(guān)鍵點(diǎn)信息和描述符

FLANN_INDEX_KDTREE = 0
indexParams = dict(algorithm=FLANN_INDEX_KDTREE, trees=5)
searchParams = dict(checks=50)  # 指定索引樹要被遍歷的次數(shù)

flann = cv2.FlannBasedMatcher(indexParams, searchParams)
matches = flann.knnMatch(des1, des2, k=2)
matchesMask = [[0, 0] for i in range(len(matches))]
print("matches", matches[0])
for i, (m, n) in enumerate(matches):
    if m.distance  0.07 * n.distance:
        matchesMask[i] = [1, 0]

drawParams = dict(matchColor=(0, 255, 0), singlePointColor=None,
                  matchesMask=matchesMask, flags=2)  # flag=2只畫出匹配點(diǎn),flag=0把所有的點(diǎn)都畫出
resultImage = cv2.drawMatchesKnn(leftImage, kp1, rightImage, kp2, matches, None, **drawParams)
plt.imshow(resultImage)
plt.show()

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

您可能感興趣的文章:
  • opencv-python圖像配準(zhǔn)(匹配和疊加)的實(shí)現(xiàn)
  • Python使用Opencv實(shí)現(xiàn)圖像特征檢測與匹配的方法
  • Python和OpenCV進(jìn)行多尺度模板匹配實(shí)現(xiàn)
  • OpenCV-Python模板匹配人眼的實(shí)例
  • OpenCV-Python實(shí)現(xiàn)多模板匹配
  • python基于OpenCV模板匹配識別圖片中的數(shù)字
  • Python開發(fā)之基于模板匹配的信用卡數(shù)字識別功能
  • Python+Opencv實(shí)現(xiàn)圖像匹配功能(模板匹配)

標(biāo)簽:泉州 怒江 吉林 長春 清遠(yuǎn) 岳陽 安慶 洛陽

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《Opencv Python實(shí)現(xiàn)兩幅圖像匹配》,本文關(guān)鍵詞  Opencv,Python,實(shí)現(xiàn),兩幅,圖像,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《Opencv Python實(shí)現(xiàn)兩幅圖像匹配》相關(guān)的同類信息!
  • 本頁收集關(guān)于Opencv Python實(shí)現(xiàn)兩幅圖像匹配的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章