2023.8.19
為了在暑假內(nèi)實(shí)現(xiàn)深度學(xué)習(xí)的進(jìn)階學(xué)習(xí),特意學(xué)習(xí)一下傳統(tǒng)算法,分享學(xué)習(xí)心得,記錄學(xué)習(xí)日常
SIFT的百科:
SIFT =?Scale Invariant Feature Transform,?尺度不變特征轉(zhuǎn)換
全網(wǎng)最詳細(xì)SIFT算法原理實(shí)現(xiàn)_ssift算法_Tc.小浩的博客-CSDN博客
在環(huán)境配置中要配置opencv:
pip install opencv-contrib-python
SIFT算法的三個(gè)計(jì)算步驟:
? 1,在DOG尺度空間中獲取特征點(diǎn);
? 2,關(guān)鍵點(diǎn)的方向估計(jì)(尋找主方向)
? 3,通過各關(guān)鍵點(diǎn)的特征向量(關(guān)鍵點(diǎn)的描述子生成)
進(jìn)行兩兩比較找出相互匹配的若干對特征點(diǎn),建立兩圖間景物間的對應(yīng)關(guān)系,可以基于SIFT實(shí)現(xiàn)圖像拼接
Code of SIFT and lena:
?注意你是否有l(wèi)ena.png圖像
import cv2 as cv
img = cv.imread('lena.png')
gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY)
sift = cv.xfeatures2d.SIFT_create()
# sift = cv.SIFT_create()
kp = sift.detect(gray, None)
img = cv.drawKeypoints(gray, kp, img)
cv.imshow("SIFT", img)
cv.imwrite('sift_keypoints.jpg', img)
cv.waitKey(0)
cv.destroyAllWindows()
Result is shown in these figrues : SIFT 提取了lena的特征點(diǎn)?
?基于SIFT的圖片實(shí)現(xiàn)圖片拼接:
? ?代碼是Copy大神的,注意有兩個(gè)代碼,運(yùn)行第二個(gè)喔。代碼所用的圖片也附上!
import numpy as np
import cv2
class Stitcher:
# 拼接函數(shù)
def stitch(self, images, ratio=0.75, reprojThresh=4.0, showMatches=False):
# 獲取輸入圖片
(imageB, imageA) = images
# 檢測A、B圖片的SIFT關(guān)鍵特征點(diǎn),并計(jì)算特征描述子
(kpsA, featuresA) = self.detectAndDescribe(imageA)
(kpsB, featuresB) = self.detectAndDescribe(imageB)
# 匹配兩張圖片的所有特征點(diǎn),返回匹配結(jié)果
M = self.matchKeypoints(kpsA, kpsB, featuresA, featuresB, ratio, reprojThresh)
# 如果返回結(jié)果為空,沒有匹配成功的特征點(diǎn),退出算法
if M is None:
return None
# 否則,提取匹配結(jié)果
# H是3x3視角變換矩陣
(matches, H, status) = M
# 將圖片A進(jìn)行視角變換,result是變換后圖片
result = cv2.warpPerspective(imageA, H, (imageA.shape[1] + imageB.shape[1], imageA.shape[0]))
# 將圖片B傳入result圖片最左端
result[0:imageB.shape[0], 0:imageB.shape[1]] = imageB
# 檢測是否需要顯示圖片匹配
if showMatches:
# 生成匹配圖片
vis = self.drawMatches(imageA, imageB, kpsA, kpsB, matches, status)
# 返回結(jié)果
return (result, vis)
# 返回匹配結(jié)果
return result
def detectAndDescribe(self, image):
# 將彩色圖片轉(zhuǎn)換成灰度圖
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# 建立SIFT生成器
descriptor = cv2.xfeatures2d.SIFT_create()
# 檢測SIFT特征點(diǎn),并計(jì)算描述子
(kps, features) = descriptor.detectAndCompute(image, None)
# 將結(jié)果轉(zhuǎn)換成NumPy數(shù)組
kps = np.float32([kp.pt for kp in kps])
print(kps)
# 返回特征點(diǎn)集,及對應(yīng)的描述特征
return (kps, features)
def matchKeypoints(self, kpsA, kpsB, featuresA, featuresB, ratio, reprojThresh):
# 建立暴力匹配器
matcher = cv2.DescriptorMatcher_create("BruteForce")
# 使用KNN檢測來自A、B圖的SIFT特征匹配對,K=2
rawMatches = matcher.knnMatch(featuresA, featuresB, 2)
matches = []
for m in rawMatches:
# 當(dāng)最近距離跟次近距離的比值小于ratio值時(shí),保留此匹配對
if len(m) == 2 and m[0].distance < m[1].distance * ratio:
# 存儲兩個(gè)點(diǎn)在featuresA, featuresB中的索引值
matches.append((m[0].trainIdx, m[0].queryIdx))
# 當(dāng)篩選后的匹配對大于4時(shí),計(jì)算視角變換矩陣
if len(matches) > 4:
# 獲取匹配對的點(diǎn)坐標(biāo)
ptsA = np.float32([kpsA[i] for (_, i) in matches])
ptsB = np.float32([kpsB[i] for (i, _) in matches])
# 計(jì)算視角變換矩陣
(H, status) = cv2.findHomography(ptsA, ptsB, cv2.RANSAC, reprojThresh)
# 返回結(jié)果
return (matches, H, status)
# 如果匹配對小于4時(shí),返回None
return None
def drawMatches(self, imageA, imageB, kpsA, kpsB, matches, status):
# 初始化可視化圖片,將A、B圖左右連接到一起
(hA, wA) = imageA.shape[:2]
(hB, wB) = imageB.shape[:2]
vis = np.zeros((max(hA, hB), wA + wB, 3), dtype="uint8")
vis[0:hA, 0:wA] = imageA
vis[0:hB, wA:] = imageB
# 聯(lián)合遍歷,畫出匹配對
for ((trainIdx, queryIdx), s) in zip(matches, status):
# 當(dāng)點(diǎn)對匹配成功時(shí),畫到可視化圖上
if s == 1:
# 畫出匹配對
ptA = (int(kpsA[queryIdx][0]), int(kpsA[queryIdx][1]))
ptB = (int(kpsB[trainIdx][0]) + wA, int(kpsB[trainIdx][1]))
cv2.line(vis, ptA, ptB, (0, 255, 0), 1)
# 返回可視化結(jié)果
return vis
from Stitcher import Stitcher
import cv2
# 讀取拼接圖片
imageA = cv2.imread("image/left_01.png")
imageB = cv2.imread("image/right_01.png")
# 把圖片拼接成全景圖
stitcher = Stitcher()
(result, vis) = stitcher.stitch([imageA, imageB], showMatches=True)
# 顯示所有圖片
cv2.imshow("Image A", imageA)
cv2.imshow("Image B", imageB)
cv2.imshow("Keypoint Matches", vis)
cv2.imshow("Result", result)
cv2.waitKey(0)
cv2.destroyAllWindows()
?
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 代碼使用的圖片
?效果是這樣:
文章來源:http://www.zghlxwxcb.cn/news/detail-658865.html
?文章來源地址http://www.zghlxwxcb.cn/news/detail-658865.html
到了這里,關(guān)于學(xué)習(xí)筆記:Opencv實(shí)現(xiàn)圖像特征提取算法SIFT的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!