使用stitcher需要注意,圖像太大會報錯而且計算慢。
特點和適用范圍:圖像需有足夠重合相同特征區(qū)域。
優(yōu)點:適應部分傾斜/尺度變換和畸變情形,拼接效果好,使用簡單,可以一次拼接多張圖片。
缺點:需要有足夠的相同特征區(qū)域進行匹配,速度較慢(和圖像大小有關)。
原圖(可下載)
![](/d/20211017/f46c4e6286e35600a4af0c5afd596830.gif)
![](/d/20211017/9868b11a617499843bf21465dbf1dfad.gif)
代碼(兩張圖片拼接)
import sys
import cv2
if __name__ == "__main__":
img1 = cv2.imread('C:/Users/Guaguan/Desktop/img/1.jpg') # 圖片絕對路徑,
img2 = cv2.imread('C:/Users/Guaguan/Desktop/img/2.jpg')
# stitcher = cv2.createStitcher(False) # 老的OpenCV版本,用這一個
stitcher = cv2.Stitcher.create(cv2.Stitcher_PANORAMA) # 我的是OpenCV4
(status, pano) = stitcher.stitch((img1, img2))
if status != cv2.Stitcher_OK:
print("不能拼接圖片, error code = %d" % status)
sys.exit(-1)
print("拼接成功.")
cv2.imshow('pano', pano)
# cv2.imwrite("pano.jpg", pano)
cv2.waitKey(0)
拼接結果
![](/d/20211017/ae6ae195e5b9d80ffea25ced59b74a2b.gif)
原圖
![](/d/20211017/b32cfe58f31fbfa054a8d3083b5e3b74.gif)
![](/d/20211017/d16d6b08604fc51ace5a406066b16ee6.gif)
![](/d/20211017/9092ba200ce073f59dc620fe6aed30ee.gif)
代碼(多個圖像自動拼接)
import os
import sys
import cv2
import win32ui
# ? python基于Stitcher圖像拼接
def imgstitcher(imgs): # 傳入圖像數(shù)據(jù) 列表[] 實現(xiàn)圖像拼接
stitcher = cv2.Stitcher.create(cv2.Stitcher_PANORAMA)
_result, pano = stitcher.stitch(imgs)
if _result != cv2.Stitcher_OK:
print("不能拼接圖片, error code = %d" % _result)
sys.exit(-1)
output = 'result' + '.png'
cv2.imwrite(output, pano)
print("拼接成功. %s 已保存!" % output)
if __name__ == "__main__":
# imgPath為圖片所在的文件夾相對路徑
imgPath = 'C:/Users/Guaguan/Desktop/img'
imgList = os.listdir(imgPath)
imgs = []
for imgName in imgList:
pathImg = os.path.join(imgPath, imgName)
img = cv2.imread(pathImg)
if img is None:
print("圖片不能讀?。? + imgName)
sys.exit(-1)
imgs.append(img)
imgstitcher(imgs) # 拼接
cv2.waitKey(0)
cv2.destroyAllWindows()
結果
![](/d/20211017/d1ad281929595f120e76aebe2932e7c3.gif)
到此這篇關于python調用stitcher類自動實現(xiàn)多個圖像拼接融合的文章就介紹到這了,更多相關python圖像拼接融合內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
您可能感興趣的文章:- python 圖像增強算法實現(xiàn)詳解
- python 基于opencv實現(xiàn)圖像增強
- 用Python給圖像算法做個簡單應用界面
- python+opencv圖像分割實現(xiàn)分割不規(guī)則ROI區(qū)域方法匯總
- python-opencv實現(xiàn)視頻指定幀數(shù)間隔圖像的保存功能
- Python深度學習之圖像標簽標注軟件labelme詳解
- python使用matplotlib顯示圖像失真的解決方案
- python實現(xiàn)求純色彩圖像的邊框
- python數(shù)字圖像處理之估計噪聲參數(shù)
- Python深度學習之使用Albumentations對圖像做增強