python+OpenCV實(shí)現(xiàn)圖像拼接
本文實(shí)例為大家分享了利用python和OpenCV實(shí)現(xiàn)圖像拼接,供大家參考,具體內(nèi)容如下
python+OpenCV實(shí)現(xiàn)image stitching
在最新的OpenCV官方文檔中可以找到C++版本的Stitcher類的說明, 但是python版本的還沒有及時(shí)更新, 本篇對python版本的實(shí)現(xiàn)做一個(gè)簡單的介紹.
由于官方文檔中還沒有python版本的Stitcher類的說明, 因此只能自己去GitHub源碼上找, 以下是stitching的樣例:
from __future__ import print_function
import cv2 as cv
import numpy as np
import argparse
import sys
modes = (cv.Stitcher_PANORAMA, cv.Stitcher_SCANS)
parser = argparse.ArgumentParser(description='Stitching sample.')
parser.add_argument('--mode',
type = int, choices = modes, default = cv.Stitcher_PANORAMA,
help = 'Determines configuration of stitcher. The default is `PANORAMA` (%d), '
'mode suitable for creating photo panoramas. Option `SCANS` (%d) is suitable '
'for stitching materials under affine transformation, such as scans.' % modes)
parser.add_argument('--output', default = 'result.jpg',
help = 'Resulting image. The default is `result.jpg`.')
parser.add_argument('img', nargs='+', help = 'input images')
args = parser.parse_args()
# read input images
imgs = []
for img_name in args.img:
img = cv.imread(img_name)
if img is None:
print("can't read image " + img_name)
sys.exit(-1)
imgs.append(img)
stitcher = cv.Stitcher.create(args.mode)
status, pano = stitcher.stitch(imgs)
if status != cv.Stitcher_OK:
print("Can't stitch images, error code = %d" % status)
sys.exit(-1)
cv.imwrite(args.output, pano);
print("stitching completed successfully. %s saved!" % args.output)
上面寫了一大堆, 然鵝, 直接拿來用的話, 用下面的代碼可以了, 簡單粗暴
import numpy as np
import cv2
from cv2 import Stitcher
if __name__ == "__main__":
img1 = cv2.imread('1.jpg')
img2 = cv2.imread('2.jpg')
stitcher = cv2.createStitcher(False)
#stitcher = cv2.Stitcher.create(cv2.Stitcher_PANORAMA), 根據(jù)不同的OpenCV版本來調(diào)用
(_result, pano) = stitcher.stitch((img1, img2))
cv2.imshow('pano',pano)
cv2.waitKey(0)
效果如下:
原圖:


拼接后的圖像:

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Python調(diào)用API接口實(shí)現(xiàn)人臉識別
本文主要介紹了Python調(diào)用API接口實(shí)現(xiàn)人臉識別,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-02-02
關(guān)于pyqt5控件自適應(yīng)窗口超詳細(xì)知識點(diǎn)匯總
這篇文章主要介紹了關(guān)于pyqt5控件自適應(yīng)窗口超詳細(xì)知識點(diǎn)匯總,有了布局,再在布局中放置各種控件,我們就能讓控件實(shí)現(xiàn)自適應(yīng)的效果,需要的朋友可以參考下2023-03-03
python如何調(diào)用php文件中的函數(shù)詳解
這篇文章主要給大家介紹了關(guān)于python如何調(diào)用php文件中函數(shù)的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-12-12
python數(shù)據(jù)結(jié)構(gòu)之遞歸方法講解
這篇文章主要介紹了python數(shù)據(jù)結(jié)構(gòu)之遞歸講解,遞歸是解決問題的一種方法,它將問題不斷地分成更小的子問題,直到子問題可以用普通的方法解決。通常情況下,遞歸會使用一個(gè)不停調(diào)用自己的函數(shù),下面來看看文章對此的詳細(xì)介紹吧2021-12-12
python 實(shí)現(xiàn)簡單的計(jì)算器(gui界面)
這篇文章主要介紹了python 如何實(shí)現(xiàn)簡單的計(jì)算器,幫助大家更好的理解和使用python,感興趣的朋友可以了解下2020-11-11
深入解析神經(jīng)網(wǎng)絡(luò)從原理到實(shí)現(xiàn)
這篇文章主要介紹了深入解析神經(jīng)網(wǎng)絡(luò)從原理到實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-07-07
Matplotlib實(shí)現(xiàn)各種條形圖繪制
這篇文章主要介紹了Matplotlib實(shí)現(xiàn)各種條形圖繪制,文章通過利用 plt.bar 方法實(shí)現(xiàn)各種條形圖繪制,內(nèi)容詳細(xì)具有一定的參考價(jià)值,需要的小伙伴可以參考一下2022-03-03

