python如何将两张图片生成为全景图片
作者:无落 发布时间:2021-08-03 23:42:26
标签:python,全景图片
本文实例为大家分享了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、运行效果
原始的两张图:
效果图:
原始图,水杯没有处理好,导致此处效果不好。
来源:https://blog.csdn.net/qq_43433255/article/details/92211849
0
投稿
猜你喜欢
- 1.使用Paramiko登陆到单台交换机实验拓扑云彩桥接到本机环回接口:192.168.1.1/24三层交换机IP:192.168.1.2/
- 这几年来,我们这个行业一直都在谈用户体验,以用户为中心的设计。“用户体验”是指用户访问网站的界面、功能、相关信息的可读性、操作的方便性,交互
- 本文实例讲述了Python实现小数转化为百分数的格式化输出方法。分享给大家供大家参考,具体如下:比如将 0.1234 转化为 12.34%
- 2007年1月,国务院 * 了中央“一号文件”,文件中对加快农业信息化建设有了更明确的部署,为新农村建
- 监测主机存活的端口#!/usr/bin/env python# coding-utfimport argparseimport socket
- 随着网页制作热潮的兴起,Dreamweaver 4.0强大的功能深受众多网页制作者的喜爱。特别是Dreamweaver 4.0中有许多第三方
- 本篇主要将react全家桶的产品非常精炼的提取了核心内容,精华程度堪比精油。各位大人,既然来了,客官您坐,来人,给客官看茶~~redux前言
- 判断访问是否来自搜索引擎的函数,有兴趣的可以试试! <% '检查当前用户是否是蜘蛛人 Function check(
- 使用apidoc管理RESTful风格Flask项目接口文档方法apidoc项目地址flask扩展包地址文档示例地址1.安装nodejssu
- MySQL分区方便了我们的使用,但是MySQL分区究竟能做些什么,MySQL分区有没有什么限制呢?阅读下文,您就能找到答案。MySQL分区能
- 简单介绍下:CSS 图片拼合 (CSS sprites) 可有效降低图片文件的 HTTP 连接请求数. 多个图片将以一定间距合并为一个大图片
- Lightbox里面的一个函数,能把页面实际的高宽与浏览器可视面积的高宽保存在一个数组中..非常好用.什么是Lightbox?下载light
- 1.为什么写这个?一些简单的页面,无需用比较大的框架来进行爬取,自己纯手写又比较麻烦因此针对这个需求写了talonspider:•1.针对单
- str()将参数转换为字符串类型a = 26print('我有'+str(a)+'个苹果')输出:我有26个
- 在数据库中,字符型的数据是最多的,可以占到整个数据库的80%以上。为此正确处理字符型的数据,对于提高数据库的性能有很大的作用。在字符型数据中
- 那是一杠还是两杠呢?最简单的方式就是拿出钱包,掏出张“新”的50或100,翻到背面的银线上,就会发现两杠的符号。这个是以前跟同事在讨论是一杠
- 本文实例讲述了Python获取任意xml节点值的方法。分享给大家供大家参考。具体实现方法如下:# -*- coding: utf-8 -*-
- 写了网址规范化后,尚奇公司的柳先生建议再深入讨论一下301转向/重定向。下面就谈谈我所了解的301转向在搜索引擎优化方面的应用。什么是301
- Check In/Out功能简介:该功能是专门针对多用户管理而预设的。即多用户使用多帐号管理同一站点。
- 本文实例讲述了Python使用matplotlib绘制正弦和余弦曲线的方法。分享给大家供大家参考,具体如下:一 介绍关键词:绘图库官网:ht