当前位置:   article > 正文

图像拼接_result[0:image1.shape[0], 0:image1.shape[1]] = ima

result[0:image1.shape[0], 0:image1.shape[1]] = image1

 上面是两个原始图像;接下来进行拼接!

  1. import numpy as np
  2. import cv2
  3. class Stitcher:
  4. #拼接函数
  5. def stitch(self, images, ratio=0.75, reprojThresh=4.0,showMatches=False):
  6. #获取输入图片
  7. (imageB, imageA) = images
  8. #检测A、B图片的SIFT关键特征点,并计算特征描述子
  9. (kpsA, featuresA) = self.detectAndDescribe(imageA)
  10. (kpsB, featuresB) = self.detectAndDescribe(imageB)
  11. # 匹配两张图片的所有特征点,返回匹配结果
  12. M = self.matchKeypoints(kpsA, kpsB, featuresA, featuresB, ratio, reprojThresh)
  13. # 如果返回结果为空,没有匹配成功的特征点,退出算法
  14. if M is None:
  15. return None
  16. # 否则,提取匹配结果
  17. # H是3x3视角变换矩阵
  18. (matches, H, status) = M
  19. # 将图片A进行视角变换,result是变换后图片
  20. result = cv2.warpPerspective(imageA, H, (imageA.shape[1] + imageB.shape[1], imageA.shape[0]))
  21. # self.cv_show('result', result)
  22. # 将图片B传入result图片最左端
  23. result[0:imageB.shape[0], 0:imageB.shape[1]] = imageB
  24. # self.cv_show('result', result)
  25. # 检测是否需要显示图片匹配
  26. if showMatches:
  27. # 生成匹配图片
  28. vis = self.drawMatches(imageA, imageB, kpsA, kpsB, matches, status)
  29. # 返回结果
  30. return (result, vis)
  31. # 返回匹配结果
  32. return result
  33. # def cv_show(self,name,img):
  34. # cv2.imshow(name, img)
  35. # cv2.waitKey(0)
  36. # cv2.destroyAllWindows()
  37. def detectAndDescribe(self, image):
  38. # 将彩色图片转换成灰度图
  39. gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
  40. # 建立SIFT生成器
  41. descriptor = cv2.xfeatures2d.SIFT_create()
  42. # 检测SIFT特征点,并计算描述子
  43. (kps, features) = descriptor.detectAndCompute(image, None)
  44. # 将结果转换成NumPy数组
  45. kps = np.float32([kp.pt for kp in kps])
  46. # 返回特征点集,及对应的描述特征
  47. return (kps, features)
  48. def matchKeypoints(self, kpsA, kpsB, featuresA, featuresB, ratio, reprojThresh):
  49. # 建立暴力匹配器
  50. matcher = cv2.BFMatcher()
  51. # 使用KNN检测来自A、B图的SIFT特征匹配对,K=2
  52. rawMatches = matcher.knnMatch(featuresA, featuresB, 2)
  53. matches = []
  54. for m in rawMatches:
  55. # 当最近距离跟次近距离的比值小于ratio值时,保留此匹配对
  56. if len(m) == 2 and m[0].distance < m[1].distance * ratio:
  57. # 存储两个点在featuresA, featuresB中的索引值
  58. matches.append((m[0].trainIdx, m[0].queryIdx))
  59. # 当筛选后的匹配对大于4时,计算视角变换矩阵
  60. if len(matches) > 4:
  61. # 获取匹配对的点坐标
  62. ptsA = np.float32([kpsA[i] for (_, i) in matches])
  63. ptsB = np.float32([kpsB[i] for (i, _) in matches])
  64. # 计算视角变换矩阵
  65. (H, status) = cv2.findHomography(ptsA, ptsB, cv2.RANSAC, reprojThresh)
  66. # 返回结果
  67. return (matches, H, status)
  68. # 如果匹配对小于4时,返回None
  69. return None
  70. def drawMatches(self, imageA, imageB, kpsA, kpsB, matches, status):
  71. # 初始化可视化图片,将A、B图左右连接到一起
  72. (hA, wA) = imageA.shape[:2]
  73. (hB, wB) = imageB.shape[:2]
  74. vis = np.zeros((max(hA, hB), wA + wB, 3), dtype="uint8")
  75. vis[0:hA, 0:wA] = imageA
  76. vis[0:hB, wA:] = imageB
  77. # 联合遍历,画出匹配对
  78. for ((trainIdx, queryIdx), s) in zip(matches, status):
  79. # 当点对匹配成功时,画到可视化图上
  80. if s == 1:
  81. # 画出匹配对
  82. ptA = (int(kpsA[queryIdx][0]), int(kpsA[queryIdx][1]))
  83. ptB = (int(kpsB[trainIdx][0]) + wA, int(kpsB[trainIdx][1]))
  84. cv2.line(vis, ptA, ptB, (0, 255, 0), 1)
  85. # 返回可视化结果
  86. return vis

接下来进行测试;

  1. from Stitcher import Stitcher
  2. import cv2
  3. # 读取拼接图片
  4. imageA = cv2.imread("left_01.png")
  5. imageB = cv2.imread("right_01.png")
  6. # 把图片拼接成全景图
  7. stitcher = Stitcher()
  8. (result, vis) = stitcher.stitch([imageA, imageB], showMatches=True)
  9. def cv_show(name,img):
  10. cv2.imshow(name,img)
  11. cv2.waitKey(0)
  12. cv2.destroyAllWindows()
  13. # 显示所有图片
  14. # cv2.imshow("Image A", imageA)
  15. # cv2.imshow("Image B", imageB)
  16. cv2.imshow("Keypoint Matches", vis)
  17. # cv2.imshow("Result", result)
  18. cv2.waitKey(0)
  19. cv2.destroyAllWindows()

先看一下两张图片的关键点匹配图;

然后最后拼接后的图片;

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/IT小白/article/detail/81273?site
推荐阅读
相关标签
  

闽ICP备14008679号