小編給大家分享一下python如何實(shí)現(xiàn)圖像拼接,希望大家閱讀完這篇文章之后都有所收獲,下面讓我們一起去探討吧!
創(chuàng)新互聯(lián)建站專注于靖江企業(yè)網(wǎng)站建設(shè),響應(yīng)式網(wǎng)站設(shè)計(jì),電子商務(wù)商城網(wǎng)站建設(shè)。靖江網(wǎng)站建設(shè)公司,為靖江等地區(qū)提供建站服務(wù)。全流程按需網(wǎng)站制作,專業(yè)設(shè)計(jì),全程項(xiàng)目跟蹤,創(chuàng)新互聯(lián)建站專業(yè)和態(tài)度為您提供的服務(wù)1.待拼接的圖像
2. 基于SIFT特征點(diǎn)和RANSAC方法得到的圖像特征點(diǎn)匹配結(jié)果
3.圖像變換結(jié)果
4.代碼及注意事項(xiàng)
import cv2 import numpy as np def cv_show(name, image): cv2.imshow(name, image) cv2.waitKey(0) cv2.destroyAllWindows() def detectAndCompute(image): image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) sift = cv2.xfeatures2d.SIFT_create() (kps, features) = sift.detectAndCompute(image, None) kps = np.float32([kp.pt for kp in kps]) # 得到的點(diǎn)需要進(jìn)一步轉(zhuǎn)換才能使用 return (kps, features) def matchKeyPoints(kpsA, kpsB, featuresA, featuresB, ratio = 0.75, reprojThresh = 4.0): # ratio是最近鄰匹配的推薦閾值 # reprojThresh是隨機(jī)取樣一致性的推薦閾值 matcher = cv2.BFMatcher() rawMatches = matcher.knnMatch(featuresA, featuresB, 2) matches = [] for m in rawMatches: if len(m) == 2 and m[0].distance < ratio * m[1].distance: matches.append((m[0].queryIdx, m[0].trainIdx)) kpsA = np.float32([kpsA[m[0]] for m in matches]) # 使用np.float32轉(zhuǎn)化列表 kpsB = np.float32([kpsB[m[1]] for m in matches]) (M, status) = cv2.findHomography(kpsA, kpsB, cv2.RANSAC, reprojThresh) return (M, matches, status) # 并不是所有的點(diǎn)都有匹配解,它們的狀態(tài)存在status中 def stich(imgA, imgB, M): result = cv2.warpPerspective(imgA, M, (imgA.shape[1] + imgB.shape[1], imgA.shape[0])) result[0:imageA.shape[0], 0:imageB.shape[1]] = imageB cv_show('result', result) def drawMatches(imgA, imgB, kpsA, kpsB, matches, status): (hA, wA) = imgA.shape[0:2] (hB, wB) = imgB.shape[0:2] # 注意這里的3通道和uint8類型 drawImg = np.zeros((max(hA, hB), wA + wB, 3), 'uint8') drawImg[0:hB, 0:wB] = imageB drawImg[0:hA, wB:] = imageA for ((queryIdx, trainIdx),s) in zip(matches, status): if s == 1: # 注意將float32 --> int pt1 = (int(kpsB[trainIdx][0]), int(kpsB[trainIdx][1])) pt2 = (int(kpsA[trainIdx][0]) + wB, int(kpsA[trainIdx][1])) cv2.line(drawImg, pt1, pt2, (0, 0, 255)) cv_show("drawImg", drawImg) # 讀取圖像 imageA = cv2.imread('./right_01.png') cv_show("imageA", imageA) imageB = cv2.imread('./left_01.png') cv_show("imageB", imageB) # 計(jì)算SIFT特征點(diǎn)和特征向量 (kpsA, featuresA) = detectAndCompute(imageA) (kpsB, featuresB) = detectAndCompute(imageB) # 基于最近鄰和隨機(jī)取樣一致性得到一個(gè)單應(yīng)性矩陣 (M, matches, status) = matchKeyPoints(kpsA, kpsB, featuresA, featuresB) # 繪制匹配結(jié)果 drawMatches(imageA, imageB, kpsA, kpsB, matches, status) # 拼接 stich(imageA, imageB, M)
看完了這篇文章,相信你對(duì)“python如何實(shí)現(xiàn)圖像拼接”有了一定的了解,如果想了解更多相關(guān)知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝各位的閱讀!
文章名稱:python如何實(shí)現(xiàn)圖像拼接-創(chuàng)新互聯(lián)
網(wǎng)頁路徑:http://jinyejixie.com/article16/jeggg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供App開發(fā)、域名注冊(cè)、云服務(wù)器、微信公眾號(hào)、響應(yīng)式網(wǎng)站、網(wǎng)站策劃
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容