python+OpenCV實現(xiàn)圖像拼接
本文實例為大家分享了利用python和OpenCV實現(xiàn)圖像拼接,供大家參考,具體內(nèi)容如下
python+OpenCV實現(xiàn)image stitching
在最新的OpenCV官方文檔中可以找到C++版本的Stitcher類的說明, 但是python版本的還沒有及時更新, 本篇對python版本的實現(xiàn)做一個簡單的介紹.
由于官方文檔中還沒有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)文章
如何將numpy二維數(shù)組中的np.nan值替換為指定的值
這篇文章主要介紹了將numpy二維數(shù)組中的np.nan值替換為指定的值操作,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-05-05
用Python將PDF文件轉(zhuǎn)存為圖片的實現(xiàn)方法
在Python中,將PDF文件轉(zhuǎn)換為圖片格式使用專門的庫來處理PDF文檔,并將其每一頁導(dǎo)出為常見的圖像格式,這可以通過PyMuPDF庫中的fitz模塊或pdf2image庫實現(xiàn),本文給大家介紹了用Python將PDF文件轉(zhuǎn)存為圖片的方法,需要的朋友可以參考下2024-04-04
python3用PIL把圖片轉(zhuǎn)換為RGB圖片的實例
今天小編就為大家分享一篇python3用PIL把圖片轉(zhuǎn)換為RGB圖片的實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-07-07

