计算机视觉 | 基于 ORB 特征检测器和描述符的全景图像拼接算法

时间:2024-04-14 07:08:50
#!/usr/bin/env python # -*- coding:utf-8 -*- """ @Project : Stitcher-全景图像拼接-ORB特征检测器和描述符 @File : Stitcher.py @IDE : PyCharm @Author : 半亩花海 @Date : 2024/04/10 11:29 """ import numpy as np import cv2 class Stitcher: def stitch(self, images, ratio=0.75, reprojThresh=4.0, showMatches=False): # 拼接函数 # 解包输入图片 (imageB, imageA) = images # 将图片转换为灰度图 grayA = cv2.cvtColor(imageA, cv2.COLOR_BGR2GRAY) grayB = cv2.cvtColor(imageB, cv2.COLOR_BGR2GRAY) # 使用ORB特征检测器和描述符 (kpsA, featuresA) = self.detectAndDescribe(grayA) (kpsB, featuresB) = self.detectAndDescribe(grayB) # 匹配特征点 M = self.matchKeypoints(kpsA, kpsB, featuresA, featuresB, ratio, reprojThresh) # 如果匹配结果为空,则返回None if M is None: print("Failed to stitch images. Not enough matches.") return None # 解包匹配结果 (matches, H, status) = M # 进行透视变换,拼接图像 result = cv2.warpPerspective(imageA, H, (imageA.shape[1] + imageB.shape[1], imageA.shape[0])) result[0:imageB.shape[0], 0:imageB.shape[1]] = imageB # 如果需要显示匹配结果,则返回拼接图和匹配可视化图 if showMatches: vis = self.drawMatches(imageA, imageB, kpsA, kpsB, matches, status) return (result, vis) # 否则,只返回拼接图 return result @staticmethod def cv_show(name, img): # 显示图像 cv2.imshow(name, img) cv2.waitKey(0) cv2.destroyAllWindows() @staticmethod def detectAndDescribe(image): # 创建ORB特征检测器 orb = cv2.ORB_create() # 检测特征点并计算描述符 (kps, features) = orb.detectAndCompute(image, None) kps = np.float32([kp.pt for kp in kps]) return (kps, features) @staticmethod def matchKeypoints(kpsA, kpsB, featuresA, featuresB, ratio, reprojThresh): # 创建BFMatcher对象 matcher = cv2.BFMatcher() # 使用KNN匹配 rawMatches = matcher.knnMatch(featuresA, featuresB, 2) # 进行筛选,获取匹配点对 matches = [] for m in rawMatches: if len(m) == 2 and m[0].distance < m[1].distance * ratio: matches.append((m[0].trainIdx, m[0].queryIdx)) # 如果匹配点对数量大于4,则计算透视变换矩阵 if len(matches) > 4: ptsA = np.float32([kpsA[i] for (_, i) in matches]) ptsB = np.float32([kpsB[i] for (i, _) in matches]) (H, status) = cv2.findHomography(ptsA, ptsB, cv2.RANSAC, reprojThresh) return (matches, H, status) # 否则,返回None return None @staticmethod def drawMatches(imageA, imageB, kpsA, kpsB, matches, status): (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 for ((trainIdx, queryIdx), s) in zip(matches, status): 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) return vis if __name__ == "__main__": # 读取拼接图片 imageA = cv2.imread("left_01.png") imageB = cv2.imread("right_01.png") # 把图片拼接成全景图 stitcher = Stitcher() result = stitcher.stitch([imageA, imageB], showMatches=True) if result is not None: # 解包拼接结果 (panorama, matchesVis) = result # 显示拼接前的两幅图像,匹配的关键点和拼接后的图像 cv2.imshow("Image A", imageA) cv2.imshow("Image B", imageB) cv2.imshow("Keypoint Matches", matchesVis) cv2.imshow("Result", panorama) cv2.waitKey(0) cv2.destroyAllWindows()