本文实例为大家分享了python将两张图片生成全景图片的具体代码,供大家参考,具体内容如下
1、全景图片的介绍
全景图通过广角的表现手段以及绘画、相片、视频、三维模型等形式,尽可能多表现出周围的环境。360全景,即通过对专业相机捕捉整个场景的图像信息或者使用建模软件渲染过后的图片,使用软件进行图片拼合,并用专门的播放器进行播放,即将平面照片或者计算机建模图片变为360 度全观,用于虚拟现实浏览,把二维的平面图模拟成真实的三维空间,呈现给观赏者。
2、如何实现
2.1、实现原理
主要是利用sift的特征提取与匹配,参考链接
2.2、实现代码
# -*- coding:utf-8 -*-
u'''
Created on 2019年6月14日
@author: wuluo
'''
__author__ = 'wuluo'
__version__ = '1.0.0'
__company__ = u'重庆交大'
__updated__ = '2019-06-14'
import numpy as np
import cv2 as cv
from PIL import Image
from matplotlib import pyplot as plt
print('cv version: ', cv.__version__)
def pinjie():
 top, bot, left, right = 100, 100, 0, 500
 img1 = cv.imread('G:/2018and2019two/qianrushi/wuluo1.png')
 cv.imshow("img1", img1)
 img2 = cv.imread('G:/2018and2019two/qianrushi/wuluo2.png')
 cv.imshow("img2", img2)
 srcImg = cv.copyMakeBorder(
  img1, top, bot, left, right, cv.BORDER_CONSTANT, value=(0, 0, 0))
 testImg = cv.copyMakeBorder(
  img2, top, bot, left, right, cv.BORDER_CONSTANT, value=(0, 0, 0))
 img1gray = cv.cvtColor(srcImg, cv.COLOR_BGR2GRAY)
 img2gray = cv.cvtColor(testImg, cv.COLOR_BGR2GRAY)
 sift = cv.xfeatures2d_SIFT().create()
 # find the keypoints and descriptors with SIFT
 kp1, des1 = sift.detectAndCompute(img1gray, None)
 kp2, des2 = sift.detectAndCompute(img2gray, None)
 # FLANN parameters
 FLANN_INDEX_KDTREE = 1
 index_params = dict(algorithm=FLANN_INDEX_KDTREE, trees=5)
 search_params = dict(checks=50)
 flann = cv.FlannBasedMatcher(index_params, search_params)
 matches = flann.knnMatch(des1, des2, k=2)
 
 # Need to draw only good matches, so create a mask
 matchesMask = [[0, 0] for i in range(len(matches))]
 good = []
 pts1 = []
 pts2 = []
 # ratio test as per Lowe's paper
 for i, (m, n) in enumerate(matches):
  if m.distance < 0.7 * n.distance:
   good.append(m)
   pts2.append(kp2[m.trainIdx].pt)
   pts1.append(kp1[m.queryIdx].pt)
   matchesMask[i] = [1, 0]
 draw_params = dict(matchColor=(0, 255, 0),
      singlePointColor=(255, 0, 0),
      matchesMask=matchesMask,
      flags=0)
 img3 = cv.drawMatchesKnn(img1gray, kp1, img2gray,
        kp2, matches, None, **draw_params)
 #plt.imshow(img3, ), plt.show()
 rows, cols = srcImg.shape[:2]
 MIN_MATCH_COUNT = 10
 if len(good) > MIN_MATCH_COUNT:
  src_pts = np.float32(
   [kp1[m.queryIdx].pt for m in good]).reshape(-1, 1, 2)
  dst_pts = np.float32(
   [kp2[m.trainIdx].pt for m in good]).reshape(-1, 1, 2)
  M, mask = cv.findHomography(src_pts, dst_pts, cv.RANSAC, 5.0)
  warpImg = cv.warpPerspective(testImg, np.array(
   M), (testImg.shape[1], testImg.shape[0]), flags=cv.WARP_INVERSE_MAP)
  for col in range(0, cols):
   if srcImg[:, col].any() and warpImg[:, col].any():
    left = col
    break
  for col in range(cols - 1, 0, -1):
   if srcImg[:, col].any() and warpImg[:, col].any():
    right = col
    break
  res = np.zeros([rows, cols, 3], np.uint8)
  for row in range(0, rows):
   for col in range(0, cols):
    if not srcImg[row, col].any():
     res[row, col] = warpImg[row, col]
    elif not warpImg[row, col].any():
     res[row, col] = srcImg[row, col]
    else:
     srcImgLen = float(abs(col - left))
     testImgLen = float(abs(col - right))
     alpha = srcImgLen / (srcImgLen + testImgLen)
     res[row, col] = np.clip(
      srcImg[row, col] * (1 - alpha) + warpImg[row, col] * alpha, 0, 255)
  # opencv is bgr, matplotlib is rgb
  res = cv.cvtColor(res, cv.COLOR_BGR2RGB)
  # show the result
  plt.figure()
  plt.imshow(res)
  plt.show()
 else:
  print("Not enough matches are found - {}/{}".format(len(good), MIN_MATCH_COUNT))
  matchesMask = None
if __name__ == "__main__":
 pinjie()
3、运行效果
原始的两张图:
效果图:
原始图,水杯没有处理好,导致此处效果不好。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
                                免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件!
                                如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
                            
                        暂无“python如何将两张图片生成为全景图片”评论...
                                    RTX 5090要首发 性能要翻倍!三星展示GDDR7显存
三星在GTC上展示了专为下一代游戏GPU设计的GDDR7内存。
首次推出的GDDR7内存模块密度为16GB,每个模块容量为2GB。其速度预设为32 Gbps(PAM3),但也可以降至28 Gbps,以提高产量和初始阶段的整体性能和成本效益。
据三星表示,GDDR7内存的能效将提高20%,同时工作电压仅为1.1V,低于标准的1.2V。通过采用更新的封装材料和优化的电路设计,使得在高速运行时的发热量降低,GDDR7的热阻比GDDR6降低了70%。
更新动态
2025年11月01日
                                2025年11月01日
                    - 小骆驼-《草原狼2(蓝光CD)》[原抓WAV+CUE]
- 群星《欢迎来到我身边 电影原声专辑》[320K/MP3][105.02MB]
- 群星《欢迎来到我身边 电影原声专辑》[FLAC/分轨][480.9MB]
- 雷婷《梦里蓝天HQⅡ》 2023头版限量编号低速原抓[WAV+CUE][463M]
- 群星《2024好听新歌42》AI调整音效【WAV分轨】
- 王思雨-《思念陪着鸿雁飞》WAV
- 王思雨《喜马拉雅HQ》头版限量编号[WAV+CUE]
- 李健《无时无刻》[WAV+CUE][590M]
- 陈奕迅《酝酿》[WAV分轨][502M]
- 卓依婷《化蝶》2CD[WAV+CUE][1.1G]
- 群星《吉他王(黑胶CD)》[WAV+CUE]
- 齐秦《穿乐(穿越)》[WAV+CUE]
- 发烧珍品《数位CD音响测试-动向效果(九)》【WAV+CUE】
- 邝美云《邝美云精装歌集》[DSF][1.6G]
- 吕方《爱一回伤一回》[WAV+CUE][454M]
 
                         
                        
