生活随笔
收集整理的這篇文章主要介紹了
SIFT算法 特征匹配
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
目錄
- 一、SIFT算法
- DOG尺度空間構(gòu)造(Difference of Gaussian)
- 關(guān)鍵點(diǎn)搜索與定位
- 方向賦值、關(guān)鍵點(diǎn)描述
- 二、特征匹配
一、SIFT算法
參考鏈接 【OpenCV】SIFT原理與源碼分析
DOG尺度空間構(gòu)造(Difference of Gaussian)
首先是對(duì)原特征圖下采樣可以得到金字塔形狀的多分辨率空間,作為特征金字塔,該特征金字塔可以方便提取出不同尺度的特征(感覺這里也可以叫多尺度空間)
多尺度空間:利用高斯核對(duì)圖像進(jìn)行卷積操作,不同的高斯核參數(shù)σ\sigmaσ對(duì)應(yīng)不同的形狀,相當(dāng)于多尺度空間。利用高斯核對(duì)圖像做卷積操作就是高斯模糊,卷積和形狀越矮越扁,模糊程度就越大。關(guān)鍵特征在不斷模糊的過(guò)程中相對(duì)其他位置應(yīng)該是更容易保留下來(lái)的,因此在后面對(duì)特征圖做差時(shí)可以形成極值點(diǎn)。
分別對(duì)每層特征金字塔中的特征圖,用不同的高斯核進(jìn)行卷積操作,就得到了高斯特征金字塔,再對(duì)相鄰的兩層作差,進(jìn)一步得到DOG金字塔
關(guān)鍵點(diǎn)搜索與定位
關(guān)鍵特征在不同的高斯模糊下更容易保留,在DOG特征圖中表現(xiàn)為極值點(diǎn)。
關(guān)鍵點(diǎn)搜索:也比較復(fù)雜
提高定位精度:曲線擬合
方向賦值、關(guān)鍵點(diǎn)描述
方向賦值與與關(guān)鍵點(diǎn)描述,與HOG特征類似,需要計(jì)算每個(gè)點(diǎn)的梯度,計(jì)算梯度直方圖,最后得到的特征向量。
二、特征匹配
Feature Matching + Homography to find Objects
SIFT圖像匹配及其python實(shí)現(xiàn)
對(duì)兩個(gè)簡(jiǎn)單圖像進(jìn)行特征匹配,并利用隨機(jī)抽樣一致(RANSAC)濾除錯(cuò)誤匹配
Python 代碼
img
= cv2
.imread
('./rl2_s.png')
img2
= cv2
.imread
('./rl_s.png')
gray
= cv2
.cvtColor
(img
, cv2
.COLOR_BGR2GRAY
)
gray2
= cv2
.cvtColor
(img2
, cv2
.COLOR_BGR2GRAY
)
surf
= cv2
.xfeatures2d
.SURF_create
(400)
kp1
, des1
= surf
.detectAndCompute
(gray
, mask
=None)
kp2
, des2
= surf
.detectAndCompute
(gray2
, mask
=None)anot1
= cv2
.drawKeypoints
(gray
, kp1
, None)
anot2
= cv2
.drawKeypoints
(gray2
, kp2
, None)
plt
.subplot
(121)
plt
.imshow
(anot1
)
plt
.subplot
(122)
plt
.imshow
(anot2
)
matcher
= cv2
.BFMatcher
()
raw_matches
= matcher
.knnMatch
(des1
, des2
, k
=2)
good_matches
= []
for m1
, m2
in raw_matches
:if m1
.distance
< 0.85 * m2
.distance
:good_matches
.append
([m1
])
assert len(good_matches
) > 4, "Too few matches."
kp1_array
= np
.float32
([kp1
[m
[0].queryIdx
].pt
for m
in good_matches
]).reshape
(-1, 1, 2)
kp2_array
= np
.float32
([kp2
[m
[0].trainIdx
].pt
for m
in good_matches
]).reshape
(-1, 1, 2)
H
, status
= cv2
.findHomography
(kp1_array
, kp2_array
, cv2
.RANSAC
, ransacReprojThreshold
=4)
good_matches
= [good_matches
[i
] for i
in range(len(good_matches
)) if status
[i
] == 1]
imgOut
= cv2
.warpPerspective
(gray2
, H
, (gray
.shape
[1], gray
.shape
[0]), flags
=cv2
.INTER_LINEAR
+ cv2
.WARP_INVERSE_MAP
)
print(H
)
matches
= cv2
.drawMatchesKnn
(anot1
, kp1
, anot2
, kp2
, good_matches
, None, flags
= 2)plt
.figure
()
plt
.imshow
(matches
)plt
.show
()
總結(jié)
以上是生活随笔為你收集整理的SIFT算法 特征匹配的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。