基于深度学习Superpoint 的Python图像全景拼接

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)
import cv2 as cv import numpy as np def cv_show(name,img): cv.imshow(name,img) cv.waitKey(0) cv.destroyAllWindows() def detectAndDescribe(image): gray = cv.cvtColor(image,cv.COLOR_BGR2GRAY) sift = cv.xfeatures2d.SIFT_create() (kps,features)=sift.detectAndCompute(image,None)#这里的kps是一个特征点对象,,属性有.pt关键点坐标 #.angle关键点方向 .response响应强度 .size该点的直径大小 kps = np.float32([kp.pt for kp in kps])#此刻得到kps特征点对象中每一个特征点的坐标。 return (kps,features) def matchKeypoints(kpsA,kpsB,features1,features2,ratio): bf = cv.BFMatcher() rawMatches = bf.knnMatch(features1,features2,2)#rawMatcher是一个Dmatch型对象,属性有.distance描述符间距离 #.trainIdx样本图像特征点标识符,.queryIdx测试图像的特征点标识符,.imgIdx训练图像的索引 matches = [] for m,n in rawMatches: if m.distance 4: pts1 = np.float32([kpsA[i] for (_,i) in matches])#将测试图像的坐标储存到Pts1里 pts2 = np.float32([kpsB[i] for (i,_) in matches])#将样本图像的坐标储存到pts2里 # 计算视角变换矩阵H #参数一,测试图像的特征点坐标,参数二,样本图像的特征点坐标,参数三,RANSAC算法: #RANSCA原理, 因为拟合一条直线只需要两个点,因此我们每次随机选取两个点,做出直线,划定一个距离,判断落在直线周围距离范围点的个数, # 不断的迭代,直到找出拟合的直线,使得点落在上面最多的拟合曲线 #参数四:参数范围1~10,原图像的点经过变换后点与目标图像上对应点的误差,超过了就是outlier (H, status) = cv.findHomography(pts1, pts2, cv.RANSAC, 5) return (matches, H, status) return None imageA = cv.imread("E:/opencv/picture/right1.jpg") imageB = cv.imread("E:/opencv/picture/left1.png") (kpsA,features1)=detectAndDescribe(imageA) (kpsB,features2)=detectAndDescribe(imageB) M = matchKeypoints(kpsA, kpsB, features1, features2, 0.75) (matches, H, status) = M # 将图片A进行视角变换,result是变换后图片 result = cv.warpPerspective(imageA, H, (imageA.shape[1] + imageB.shape[1], imageB.shape[0])) cv_show('result1',result) result[0:imageB.shape[0], 0:imageB.shape[1]] = imageB cv_show('result2', result) 经常遇到的一个错误: new style getargs format but argument is not a tuple 针对这句代码:result = cv.warpPerspective(imageA,M,[imageA.shape[1]+imageB.shape[1],max(imageA.shape[0],imageB.shape[0])]) 原因是size那个参数应该是tuple(),而不是list[]。即应该是()而不是[]。 下面讲一下这个案例的大体过程: 1.首先我们是通过SIFT算法找到两张图(right,left)的特征点及特征向量,并把特征点的坐标储存起来。 2.通过蛮力匹配算法的得到kWmatches对象,将kWmatches对象的queryIdx和trainIdx给存起来,其中features1对应的图像为样本图像 3.求出样本图像的特征点坐标和测试图像的特征点坐标,找出这两坐标矩阵的H变换公式(利用RANSAC算法),将H变换公式对right图像做透视变换,得到拼接后的右边图像 4.将left原图赋给result对应的ROI区域,大功告成。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值