国产成人精品亚洲777人妖,欧美日韩精品一区视频,最新亚洲国产,国产乱码精品一区二区亚洲

您的位置:首頁技術文章
文章詳情頁

python實現(xiàn)圖像拼接

瀏覽:2日期:2022-08-03 18:37:27

本文實例為大家分享了python實現(xiàn)圖像拼接的具體代碼,供大家參考,具體內容如下

1.待拼接的圖像

python實現(xiàn)圖像拼接

python實現(xiàn)圖像拼接

2. 基于SIFT特征點和RANSAC方法得到的圖像特征點匹配結果

python實現(xiàn)圖像拼接

3.圖像變換結果

python實現(xiàn)圖像拼接

4.代碼及注意事項

import cv2import 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]) # 得到的點需要進一步轉換才能使用 return (kps, features) def matchKeyPoints(kpsA, kpsB, featuresA, featuresB, ratio = 0.75, reprojThresh = 4.0): # ratio是最近鄰匹配的推薦閾值 # reprojThresh是隨機取樣一致性的推薦閾值 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轉化列表 kpsB = np.float32([kpsB[m[1]] for m in matches]) (M, status) = cv2.findHomography(kpsA, kpsB, cv2.RANSAC, reprojThresh) return (M, matches, status) # 并不是所有的點都有匹配解,它們的狀態(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)# 計算SIFT特征點和特征向量(kpsA, featuresA) = detectAndCompute(imageA)(kpsB, featuresB) = detectAndCompute(imageB)# 基于最近鄰和隨機取樣一致性得到一個單應性矩陣(M, matches, status) = matchKeyPoints(kpsA, kpsB, featuresA, featuresB)# 繪制匹配結果drawMatches(imageA, imageB, kpsA, kpsB, matches, status)# 拼接stich(imageA, imageB, M)

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持好吧啦網。

標簽: Python 編程
相關文章:
主站蜘蛛池模板: 苍梧县| 建始县| 遵义市| 壶关县| 调兵山市| 四子王旗| 乌鲁木齐县| 苏州市| 德钦县| 吴堡县| 鲁山县| 望谟县| 道孚县| 同江市| 盱眙县| 雅江县| 上虞市| 措勤县| 宝山区| 肃北| 平舆县| 芮城县| 龙山县| 阿勒泰市| 调兵山市| 高碑店市| 阿图什市| 莱芜市| 长沙县| 绍兴市| 油尖旺区| 金山区| 平昌县| 平武县| 无棣县| 中山市| 龙井市| 黎平县| 林甸县| 平舆县| 湖南省|