網(wǎng)站首頁(yè) 編程語(yǔ)言 正文
首先對(duì)圖片進(jìn)行預(yù)處理,是圖片的分配率大小在合適的范圍內(nèi),避免圖片太大占滿整個(gè)電腦屏幕。
from PIL import Image
def produceImage(file_in, width, height, file_out):
image = Image.open(file_in)
resized_image = image.resize((height, width), Image.ANTIALIAS)
resized_image.save(file_out)
if __name__ == '__main__':
file_in = 'right2.png'#輸入文件的文件名
width = 500#文件大小
height = 500
file_out = 'right11.png'#生成文件的文件名
produceImage(file_in, width, height, file_out)
輸出圖片之后就可以對(duì)兩張圖片進(jìn)行拼接了。
兩張圖像要能拼接在一起成為一張圖像,就需要這兩張圖像中存在有重合的部分。通過(guò)這些重合的部分使用sift特征點(diǎn)匹配的算法,來(lái)尋找到重合部分的特征點(diǎn)。
需要注意的是,雖然sift算法比Harris角點(diǎn)的效果更好,但是也會(huì)出現(xiàn)錯(cuò)誤點(diǎn),并非完美的匹配方法。
在以下的代碼中,MyStitcher類里面的內(nèi)容就是對(duì)圖像實(shí)現(xiàn)拼接的主要過(guò)程。
import cv2
import numpy as np
class MyStitcher:
# 拼接函數(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]))
# 將圖片B傳入result圖片最左端
result[0:imageB.shape[0], 0:imageB.shape[1]] = imageB
# 檢測(cè)是否需要顯示圖片匹配
if showMatches:
# 生成匹配圖片
vis = self.drawMatches(imageA, imageB, kpsA, kpsB, matches, status)
# 返回結(jié)果
return (result, vis)
# 返回匹配結(jié)果
return result
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.DescriptorMatcher_create("BruteForce")
# 使用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)合遍歷,畫出匹配對(duì)
for ((trainIdx, queryIdx), s) in zip(matches, status):
# 當(dāng)點(diǎn)對(duì)匹配成功時(shí),畫到可視化圖上
if s == 1:
# 畫出匹配對(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
# 讀取拼接圖片
imageA = cv2.imread("left11.png")
imageB = cv2.imread("right11.png")
# 把圖片拼接成全景圖
mystitcher = MyStitcher()
(result, vis) = mystitcher.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()
原文鏈接:https://blog.csdn.net/weixin_44382897/article/details/109596470
相關(guān)推薦
- 2022-04-16 C語(yǔ)言實(shí)現(xiàn)順序循環(huán)隊(duì)列實(shí)例_C 語(yǔ)言
- 2023-02-01 Python中列表遍歷使用range和enumerate的區(qū)別講解_python
- 2022-05-20 MybatisCodeHelpPro生成持久層代碼
- 2022-10-03 使用useImperativeHandle時(shí)父組件第一次沒(méi)拿到子組件的問(wèn)題_React
- 2023-07-22 使用log4j2為日志增加代碼行號(hào)
- 2022-06-28 KVM基礎(chǔ)命令詳解_Kvm
- 2022-02-14 記關(guān)于Android開發(fā)中使用System.currentTimeMillis()不準(zhǔn)確的問(wèn)題
- 2022-01-27 插入數(shù)據(jù)庫(kù)某個(gè)字段之前判斷是否重復(fù)
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲(chǔ)小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過(guò)濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡(jiǎn)單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對(duì)象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支