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í)更新, 本篇對(duì)python版本的實(shí)現(xiàn)做一個(gè)簡(jiǎn)單的介紹.
由于官方文檔中還沒有python版本的Stitcher類的說明, 因此只能自己去GitHub源碼上找, 以下是stitching的樣例:
from __future__ import print_functionimport cv2 as cvimport numpy as npimport argparseimport sysmodes = (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 imagesimgs = []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)
上面寫了一大堆, 然鵝, 直接拿來用的話, 用下面的代碼可以了, 簡(jiǎn)單粗暴
import numpy as npimport cv2from cv2 import Stitcherif __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)
效果如下:
原圖:
拼接后的圖像:
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. React+umi+typeScript創(chuàng)建項(xiàng)目的過程2. ASP.NET Core 5.0中的Host.CreateDefaultBuilder執(zhí)行過程解析3. SharePoint Server 2019新特性介紹4. ASP中常用的22個(gè)FSO文件操作函數(shù)整理5. 三個(gè)不常見的 HTML5 實(shí)用新特性簡(jiǎn)介6. ASP調(diào)用WebService轉(zhuǎn)化成JSON數(shù)據(jù),附j(luò)son.min.asp7. .Net core 的熱插拔機(jī)制的深入探索及卸載問題求救指南8. 無線標(biāo)記語言(WML)基礎(chǔ)之WMLScript 基礎(chǔ)第1/2頁(yè)9. 讀大數(shù)據(jù)量的XML文件的讀取問題10. 解決ASP中http狀態(tài)跳轉(zhuǎn)返回錯(cuò)誤頁(yè)的問題
