python如何將兩張圖片生成為全景圖片
本文實(shí)例為大家分享了python將兩張圖片生成全景圖片的具體代碼,供大家參考,具體內(nèi)容如下
1、全景圖片的介紹
全景圖通過(guò)廣角的表現(xiàn)手段以及繪畫(huà)、相片、視頻、三維模型等形式,盡可能多表現(xiàn)出周?chē)沫h(huán)境。360全景,即通過(guò)對(duì)專業(yè)相機(jī)捕捉整個(gè)場(chǎng)景的圖像信息或者使用建模軟件渲染過(guò)后的圖片,使用軟件進(jìn)行圖片拼合,并用專門(mén)的播放器進(jìn)行播放,即將平面照片或者計(jì)算機(jī)建模圖片變?yōu)?60 度全觀,用于虛擬現(xiàn)實(shí)瀏覽,把二維的平面圖模擬成真實(shí)的三維空間,呈現(xiàn)給觀賞者。
2、如何實(shí)現(xiàn)
2.1、實(shí)現(xiàn)原理
主要是利用sift的特征提取與匹配,參考鏈接
2.2、實(shí)現(xiàn)代碼
# -*- 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、運(yùn)行效果
原始的兩張圖:

效果圖:

原始圖,水杯沒(méi)有處理好,導(dǎo)致此處效果不好。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
python3調(diào)用windows dos命令的例子
今天小編就為大家分享一篇python3調(diào)用windows dos命令的例子,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-08-08
Python Tkinter Entry和Text的添加與使用詳解
這篇文章主要介紹了Python Tkinter Entry和Text的添加與使用詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-03-03
python動(dòng)態(tài)進(jìn)度條的實(shí)現(xiàn)代碼
有時(shí)候我們需要使用print打印工作進(jìn)度,正常使用print函數(shù)會(huì)導(dǎo)致刷屏的現(xiàn)象,本文通過(guò)實(shí)例代碼給大家介紹python動(dòng)態(tài)進(jìn)度條的實(shí)現(xiàn)方法,感興趣的朋友跟隨小編一起看看吧2019-07-07
python實(shí)現(xiàn)對(duì)AES加密的視頻數(shù)據(jù)流解密的方法
密碼學(xué)中的高級(jí)加密標(biāo)準(zhǔn)(Advanced?Encryption?Standard,AES),又稱Rijndael加密法,這篇文章主要介紹了用python實(shí)現(xiàn)對(duì)AES加密的視頻數(shù)據(jù)流解密,需要的朋友可以參考下2023-02-02
pandas按某2列進(jìn)行分層隨機(jī)抽樣的實(shí)現(xiàn)
本文主要介紹了pandas按某2列進(jìn)行分層隨機(jī)抽樣的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2024-12-12
Django ManyToManyField 跨越中間表查詢的方法
今天小編就為大家分享一篇Django ManyToManyField 跨越中間表查詢的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-12-12
在Python中執(zhí)行和調(diào)用JavaScript的多種方法小結(jié)
JavaScript(JS)是一種常用的腳本語(yǔ)言,通常用于網(wǎng)頁(yè)開(kāi)發(fā),但有時(shí)也需要在Python中執(zhí)行或調(diào)用JavaScript代碼,本文將詳細(xì)介紹Python中執(zhí)行和調(diào)用JavaScript的多種方法,每種方法都將附有示例代碼,方便理解如何在Python中與JavaScript進(jìn)行互動(dòng),需要的朋友可以參考下2023-11-11

