参考
https://github.com/kushalvyas/Python-Multiple-Image-Stitching
https://github.com/MagicLeapResearch/SuperPointPretrainedNetwork
https://github.com/syinari0123/SuperPoint-VO
用superpoint方法代替surf提取图像特征,进行Python版本的图像拼接。
注意,Python版本图像拼接效果并不好,本博客只是学习记录。
改动后的matchers.py如下:
import cv2
import numpy as np
from sp_extractor import SuperPointFrontend
class matchers:
def __init__(self):
self.surf = cv2.xfeatures2d.SURF_create()
self.detector = SuperPointFrontend(weights_path="superpoint_v1.pth",
nms_dist=4,
conf_thresh=0.015,
nn_thresh=0.7,
cuda=True)
FLANN_INDEX_KDTREE = 0
index_params = dict(algorithm=0, trees=5)
search_params = dict(checks=50)
self.flann = cv2.FlannBasedMatcher(index_params, search_params)
def match(self, i1, i2, direction=None):
imageSet1 = self.getSURFFeatures(i1)
imageSet2 = self.getSURFFeatures(i2)
print "Direction : ", direction
matches = self.flann.knnMatch(
np.asarray(imageSet2['des'],np.float32),
np.asarray(imageSet1['des'],np.float32),
k=2
)
good = []
for i , (m, n) in enumerate(matches):
if m.distance < 0.7*n.distance:
good.append((m.trainIdx, m.queryIdx))
if len(good) > 4:
pointsCurrent = imageSet2['kp']
pointsPrevious = imageSet1['kp']
matchedPointsCurrent = np.float32(
[pointsCurrent[i] for (__, i) in good]
)
matchedPointsPrev = np.float32(
[pointsPrevious[i] for (i, __) in good]
)
H, s = cv2.findHomography(matchedPointsCurrent, matchedPointsPrev, cv2.RANSAC, 4)
return H
return None
def getSURFFeatures(self, im):
gray = cv2.cvtColor(im, cv2.COLOR_BGR2GRAY)
pts, desc, heatmap = self.detector.run(gray)
#kp, des = self.surf.detectAndCompute(gray, None)
#superpoint的pts和desc格式为3*n,改为n*2。
pts=np.delete(pts,2,axis=0)
desc=np.delete(desc,2,axis=0)
pts=np.transpose(pts)
desc=np.transpose(desc)
pts=pts.tolist()#矩阵转换为list,
desc=desc.tolist()
return {'kp':pts, 'des':desc}
```