使用stitcher需要注意,图像太大会报错而且计算慢。
特点和适用范围:图像需有足够重合相同特征区域。
优点:适应部分倾斜/尺度变换和畸变情形,拼接效果好,使用简单,可以一次拼接多张图片。
缺点:需要有足够的相同特征区域进行匹配,速度较慢(和图像大小有关)。
原图(可下载)


代码(两张图片拼接)
  
   - 
    
     
    
    
     
      import sys
     
    
- 
    
     
    
    
     
      import cv2
     
    
- 
    
     
    
    
      
     
    
- 
    
     
    
    
     
      if __name__ == 
      "__main__":
     
    
- 
    
     
    
    
     
          img1 = cv2.imread(
      'C:/Users/Guaguan/Desktop/img/1.jpg')    
      # 图片绝对路径,
     
    
- 
    
     
    
    
     
          img2 = cv2.imread(
      'C:/Users/Guaguan/Desktop/img/2.jpg')
     
    
- 
    
     
    
    
      
     
    
- 
    
     
    
    
         
      # stitcher = cv2.createStitcher(False) # 老的OpenCV版本,用这一个
     
    
- 
    
     
    
    
     
          stitcher = cv2.Stitcher.create(cv2.Stitcher_PANORAMA)  
      # 我的是OpenCV4
     
    
- 
    
     
    
    
      
     
    
- 
    
     
    
    
     
          (status, pano) = stitcher.stitch((img1, img2))
     
    
- 
    
     
    
    
         
      if status != cv2.Stitcher_OK:
     
    
- 
    
     
    
    
     
              print(
      "不能拼接图片, error code = %d" % status)
     
    
- 
    
     
    
    
     
              sys.exit(
      -1)
     
    
- 
    
     
    
    
     
          print(
      "拼接成功.")
     
    
- 
    
     
    
    
     
          cv2.imshow(
      'pano', pano)
     
    
- 
    
     
    
    
         
      # cv2.imwrite("pano.jpg", pano)
     
    
- 
    
     
    
    
     
          cv2.waitKey(
      0)
     
    
拼接结果

原图



代码(多个图像自动拼接)
  
   - 
    
     
    
    
     
      import os
     
    
- 
    
     
    
    
     
      import sys
     
    
- 
    
     
    
    
     
      import cv2
     
    
- 
    
     
    
    
     
      import win32ui
     
    
- 
    
     
    
    
      
     
    
- 
    
     
    
    
      
     
    
- 
    
     
    
    
     
      # ? python基于Stitcher图像拼接
     
    
- 
    
     
    
    
      
     
    
- 
    
     
    
    
      
     
    
- 
    
     
    
    
     
      def imgstitcher(imgs):  
      # 传入图像数据 列表[] 实现图像拼接
     
    
- 
    
     
    
    
     
          stitcher = cv2.Stitcher.create(cv2.Stitcher_PANORAMA)
     
    
- 
    
     
    
    
     
          _result, pano = stitcher.stitch(imgs)
     
    
- 
    
     
    
    
      
     
    
- 
    
     
    
    
         
      if _result != cv2.Stitcher_OK:
     
    
- 
    
     
    
    
     
              print(
      "不能拼接图片, error code = %d" % _result)
     
    
- 
    
     
    
    
     
              sys.exit(
      -1)
     
    
- 
    
     
    
    
      
     
    
- 
    
     
    
    
     
          output = 
      'result' + 
      '.png'
     
    
- 
    
     
    
    
     
          cv2.imwrite(output, pano)
     
    
- 
    
     
    
    
     
          print(
      "拼接成功. %s 已保存!" % output)
     
    
- 
    
     
    
    
      
     
    
- 
    
     
    
    
      
     
    
- 
    
     
    
    
     
      if __name__ == 
      "__main__":
     
    
- 
    
     
    
    
         
      # imgPath为图片所在的文件夹相对路径
     
    
- 
    
     
    
    
     
          imgPath = 
      'C:/Users/Guaguan/Desktop/img'
     
    
- 
    
     
    
    
         
     
    
- 
    
     
    
    
     
          imgList = os.listdir(imgPath)
     
    
- 
    
     
    
    
     
          imgs = []
     
    
- 
    
     
    
    
         
      for imgName 
      in imgList:
     
    
- 
    
     
    
    
     
              pathImg = os.path.join(imgPath, imgName)
     
    
- 
    
     
    
    
     
              img = cv2.imread(pathImg)
     
    
- 
    
     
    
    
             
      if img 
      is 
      None:
     
    
- 
    
     
    
    
     
                  print(
      "图片不能读取:" + imgName)
     
    
- 
    
     
    
    
     
                  sys.exit(
      -1)
     
    
- 
    
     
    
    
     
              imgs.append(img)
     
    
- 
    
     
    
    
      
     
    
- 
    
     
    
    
     
          imgstitcher(imgs)    
      # 拼接
     
    
- 
    
     
    
    
      
     
    
- 
    
     
    
    
     
          cv2.waitKey(
      0)
     
    
- 
    
     
    
    
     
          cv2.destroyAllWindows()
     
    
结果
转载:https://blog.csdn.net/qq_36623595/article/details/116018818
查看评论
					