pip install opencv-python==3.4.2.16
pip install opencv-contrib-python==3.4.2.16
参考
https://github.com/kushalvyas/Python-Multiple-Image-Stitching
https://github.com/MagicLeapResearch/SuperPointPretrainedNetwork
https://github.com/syinari0123/SuperPoint-VO
用superpoint方法代替surf提取图像特征,进行Python版本的图像拼接。
匹配效果很好,只是python没有图像拼接的需要的光束平差法,融合,曝光补偿模块。而这些在C++早已集成好了,似乎没人用python造轮子了。因此,Python版本图像拼接效果并不好,本博客只是学习记录。
**
源代码及数据链接https://download.csdn.net/download/qq_33591712/11638762
https://download.csdn.net/download/qq_33591712/11592396
**
改动后的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)