主頁(yè) > 知識(shí)庫(kù) > OpenCV全景圖像拼接的實(shí)現(xiàn)示例

OpenCV全景圖像拼接的實(shí)現(xiàn)示例

熱門標(biāo)簽:北瀚ai電銷機(jī)器人官網(wǎng)手機(jī)版 所得系統(tǒng)電梯怎樣主板設(shè)置外呼 朝陽(yáng)手機(jī)外呼系統(tǒng) 市場(chǎng)上的電銷機(jī)器人 小蘇云呼電話機(jī)器人 佛山400電話辦理 北京電銷外呼系統(tǒng)加盟 地圖標(biāo)注面積 儋州電話機(jī)器人

本文主要介紹了OpenCV全景圖像拼接的實(shí)現(xiàn)示例,分享給大家,具體如下:

left_01.jpg

right_01.jpg

Stitcher.py

import numpy as np
import cv2
 
class Stitcher:
 
    #拼接函數(shù)
    def stitch(self, images, ratio=0.75, reprojThresh=4.0,showMatches=False):
        #獲取輸入圖片
        (imageB, imageA) = images
        #檢測(cè)A、B圖片的SIFT關(guān)鍵特征點(diǎn),并計(jì)算特征描述子
        (kpsA, featuresA) = self.detectAndDescribe(imageA)
        (kpsB, featuresB) = self.detectAndDescribe(imageB)
 
        # 匹配兩張圖片的所有特征點(diǎn),返回匹配結(jié)果
        M = self.matchKeypoints(kpsA, kpsB, featuresA, featuresB, ratio, reprojThresh)
 
        # 如果返回結(jié)果為空,沒(méi)有匹配成功的特征點(diǎn),退出算法
        if M is None:
            return None
 
        # 否則,提取匹配結(jié)果
        # H是3x3視角變換矩陣      
        (matches, H, status) = M
        # 將圖片A進(jìn)行視角變換,result是變換后圖片
        result = cv2.warpPerspective(imageA, H, (imageA.shape[1] + imageB.shape[1], imageA.shape[0]))
        self.cv_show('result', result)
        # 將圖片B傳入result圖片最左端
        result[0:imageB.shape[0], 0:imageB.shape[1]] = imageB
        self.cv_show('result', result)
        # 檢測(cè)是否需要顯示圖片匹配
        if showMatches:
            # 生成匹配圖片
            vis = self.drawMatches(imageA, imageB, kpsA, kpsB, matches, status)
            # 返回結(jié)果
            return (result, vis)
 
        # 返回匹配結(jié)果
        return result
    def cv_show(self,name,img):
        cv2.imshow(name, img)
        cv2.waitKey(0)
        cv2.destroyAllWindows()
 
    def detectAndDescribe(self, image):
        # 將彩色圖片轉(zhuǎn)換成灰度圖
        gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
 
        # 建立SIFT生成器
        descriptor = cv2.xfeatures2d.SIFT_create()
        # 檢測(cè)SIFT特征點(diǎn),并計(jì)算描述子
        (kps, features) = descriptor.detectAndCompute(image, None)
 
        # 將結(jié)果轉(zhuǎn)換成NumPy數(shù)組
        kps = np.float32([kp.pt for kp in kps])
 
        # 返回特征點(diǎn)集,及對(duì)應(yīng)的描述特征
        return (kps, features)
 
    def matchKeypoints(self, kpsA, kpsB, featuresA, featuresB, ratio, reprojThresh):
        # 建立暴力匹配器
        matcher = cv2.BFMatcher()
  
        # 使用KNN檢測(cè)來(lái)自A、B圖的SIFT特征匹配對(duì),K=2
        rawMatches = matcher.knnMatch(featuresA, featuresB, 2)
 
        matches = []
        for m in rawMatches:
            # 當(dāng)最近距離跟次近距離的比值小于ratio值時(shí),保留此匹配對(duì)
            if len(m) == 2 and m[0].distance  m[1].distance * ratio:
            # 存儲(chǔ)兩個(gè)點(diǎn)在featuresA, featuresB中的索引值
                matches.append((m[0].trainIdx, m[0].queryIdx))
 
        # 當(dāng)篩選后的匹配對(duì)大于4時(shí),計(jì)算視角變換矩陣
        if len(matches) > 4:
            # 獲取匹配對(duì)的點(diǎn)坐標(biāo)
            ptsA = np.float32([kpsA[i] for (_, i) in matches])
            ptsB = np.float32([kpsB[i] for (i, _) in matches])
 
            # 計(jì)算視角變換矩陣
            (H, status) = cv2.findHomography(ptsA, ptsB, cv2.RANSAC, reprojThresh)
 
            # 返回結(jié)果
            return (matches, H, status)
 
        # 如果匹配對(duì)小于4時(shí),返回None
        return None
 
    def drawMatches(self, imageA, imageB, kpsA, kpsB, matches, status):
        # 初始化可視化圖片,將A、B圖左右連接到一起
        (hA, wA) = imageA.shape[:2]
        (hB, wB) = imageB.shape[:2]
        vis = np.zeros((max(hA, hB), wA + wB, 3), dtype="uint8")
        vis[0:hA, 0:wA] = imageA
        vis[0:hB, wA:] = imageB
 
        # 聯(lián)合遍歷,畫(huà)出匹配對(duì)
        for ((trainIdx, queryIdx), s) in zip(matches, status):
            # 當(dāng)點(diǎn)對(duì)匹配成功時(shí),畫(huà)到可視化圖上
            if s == 1:
                # 畫(huà)出匹配對(duì)
                ptA = (int(kpsA[queryIdx][0]), int(kpsA[queryIdx][1]))
                ptB = (int(kpsB[trainIdx][0]) + wA, int(kpsB[trainIdx][1]))
                cv2.line(vis, ptA, ptB, (0, 255, 0), 1)
 
        # 返回可視化結(jié)果
        return vis

ImageStiching.py

from Stitcher import Stitcher
import cv2
 
# 讀取拼接圖片
imageA = cv2.imread("left_01.jpg")
imageB = cv2.imread("right_01.jpg")
 
# 把圖片拼接成全景圖
stitcher = Stitcher()
(result, vis) = stitcher.stitch([imageA, imageB], showMatches=True)
 
# 顯示所有圖片
cv2.imshow("Image A", imageA)
cv2.imshow("Image B", imageB)
cv2.imshow("Keypoint Matches", vis)
cv2.imshow("Result", result)
cv2.waitKey(0)
cv2.destroyAllWindows()

運(yùn)行結(jié)果:

如遇以下錯(cuò)誤:

cv2.error: OpenCV(3.4.3) C:\projects\opencv-python\opencv_contrib\modules\xfeatures2d\src\sift.cpp:1207: error: (-213:The function/feature is not implemented) This algorithm is patented and is excluded in this configuration; Set OPENCV_ENABLE_NONFREE CMake option and rebuild the library in function ‘cv::xfeatures2d::SIFT::create'

如果運(yùn)行OpenCV程序提示算法版權(quán)問(wèn)題可以通過(guò)安裝低版本的opencv-contrib-python解決:

pip install --user opencv-contrib-python==3.3.0.10

到此這篇關(guān)于OpenCV全景圖像拼接的實(shí)現(xiàn)示例的文章就介紹到這了,更多相關(guān)OpenCV 圖像拼接內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

您可能感興趣的文章:
  • python opencv進(jìn)行圖像拼接
  • opencv2基于SURF特征提取實(shí)現(xiàn)兩張圖像拼接融合
  • python+OpenCV實(shí)現(xiàn)圖像拼接
  • python opencv 圖像拼接的實(shí)現(xiàn)方法
  • OpenCV實(shí)現(xiàn)多圖像拼接成一張大圖
  • opencv實(shí)現(xiàn)多張圖像拼接
  • Opencv使用Stitcher類圖像拼接生成全景圖像

標(biāo)簽:云南 江蘇 寧夏 金融催收 酒泉 龍巖 定西 商丘

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《OpenCV全景圖像拼接的實(shí)現(xiàn)示例》,本文關(guān)鍵詞  OpenCV,全景,圖像,拼接,的,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問(wèn)題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無(wú)關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《OpenCV全景圖像拼接的實(shí)現(xiàn)示例》相關(guān)的同類信息!
  • 本頁(yè)收集關(guān)于OpenCV全景圖像拼接的實(shí)現(xiàn)示例的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章