当前位置:网站首页>opencv学习笔记六--图像拼接
opencv学习笔记六--图像拼接
2022-07-01 14:52:00 【Cloudy_to_sunny】
opencv学习笔记六--图像拼接
import numpy as np
import cv2
import matplotlib.pyplot as plt#Matplotlib是RGB
class Stitcher:
#拼接函数
def stitch(self, images, ratio=0.75, reprojThresh=4.0,showMatches=False):
#获取输入图片
(imageB, imageA) = images
#检测A、B图片的SIFT关键特征点,并计算特征描述子
(kpsA, featuresA) = self.detectAndDescribe(imageA)
(kpsB, featuresB) = self.detectAndDescribe(imageB)
# 匹配两张图片的所有特征点,返回匹配结果
M = self.matchKeypoints(kpsA, kpsB, featuresA, featuresB, ratio, reprojThresh)
# 如果返回结果为空,没有匹配成功的特征点,退出算法
if M is None:
return None
# 否则,提取匹配结果
# H是3x3视角变换矩阵
(matches, H, status) = M
# 将图片A进行视角变换,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)
# 检测是否需要显示图片匹配
if showMatches:
# 生成匹配图片
vis = self.drawMatches(imageA, imageB, kpsA, kpsB, matches, status)
# 返回结果
return (result, vis)
# 返回匹配结果
return result
#显示函数
def cv_show(self,name,img):
b,g,r = cv2.split(img)
img_rgb = cv2.merge((r,g,b))
plt.imshow(img_rgb)
plt.show()
def cv_show1(self,name,img):
plt.imshow(img)
plt.show()
cv2.imshow(name,img)
cv2.waitKey()
cv2.destroyAllWindows()
#计算特征和描子
def detectAndDescribe(self, image):
# 将彩色图片转换成灰度图
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# 建立SIFT生成器
descriptor = cv2.xfeatures2d.SIFT_create()
# 检测SIFT特征点,并计算描述子
(kps, features) = descriptor.detectAndCompute(image, None)
# 将结果转换成NumPy数组
kps = np.float32([kp.pt for kp in kps])
# 返回特征点集,及对应的描述特征
return (kps, features)
#匹配关键点
def matchKeypoints(self, kpsA, kpsB, featuresA, featuresB, ratio, reprojThresh):
# 建立暴力匹配器
matcher = cv2.BFMatcher()
# 使用KNN检测来自A、B图的SIFT特征匹配对,K=2
rawMatches = matcher.knnMatch(featuresA, featuresB, 2)#1对2匹配
matches = []
for m in rawMatches:
# 当最近距离跟次近距离的比值小于ratio值时,保留此匹配对
if len(m) == 2 and m[0].distance < m[1].distance * ratio:
# 存储两个点在featuresA, featuresB中的索引值
matches.append((m[0].trainIdx, m[0].queryIdx))
# 当筛选后的匹配对大于4时,计算视角变换矩阵
if len(matches) > 4:
# 获取匹配对的点坐标
ptsA = np.float32([kpsA[i] for (_, i) in matches])
ptsB = np.float32([kpsB[i] for (i, _) in matches])
# 计算视角变换矩阵
(H, status) = cv2.findHomography(ptsA, ptsB, cv2.RANSAC, reprojThresh)
# 返回结果
return (matches, H, status)
# 如果匹配对小于4时,返回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
# 联合遍历,画出匹配对
for ((trainIdx, queryIdx), s) in zip(matches, status):
# 当点对匹配成功时,画到可视化图上
if s == 1:
# 画出匹配对
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)
# 返回可视化结果
return vis
# 读取拼接图片
imageA = cv2.imread("left_01.png")
imageB = cv2.imread("right_01.png")
# 把图片拼接成全景图
stitcher = Stitcher()
(result, vis) = stitcher.stitch([imageA, imageB], showMatches=True)


# 显示所有图片
stitcher.cv_show("Image A", imageA)
stitcher.cv_show("Image B", imageB)
stitcher.cv_show("Keypoint Matches", vis)
stitcher.cv_show("Result", result)




边栏推荐
- 基于价值量化的需求优先级排序方法
- Redis installation and setting up SSDB master-slave environment under Ubuntu 14.04
- sqlilabs less10
- In hot summer, please put away this safe gas use guide!
- Cannot link redis when redis is enabled
- tensorflow2-savedmodel convert to pb(frozen_graph)
- Word2vec yyds dry goods inventory
- Generate random numbers (4-bit, 6-bit)
- 111. Minimum depth of binary tree
- [零基础学IoT Pwn] 复现Netgear WNAP320 RCE
猜你喜欢
![[零基础学IoT Pwn] 复现Netgear WNAP320 RCE](/img/f7/d683df1d4b1b032164a529d3d94615.png)
[零基础学IoT Pwn] 复现Netgear WNAP320 RCE

竣达技术丨室内空气环境监测终端 pm2.5、温湿度TVOC等多参数监测

Fundamentals of C language

C 语言基础
![[14. Interval sum (discretization)]](/img/e5/8b29aca7068a6385e8ce90c2742c37.png)
[14. Interval sum (discretization)]

Take you to API development by hand

Semiconductor foundation of binary realization principle

Music player development example (can be set up)
![[Verilog quick start of Niuke series] ~ multi function data processor, calculate the difference between two numbers, use generate... For statement to simplify the code, and use sub modules to realize](/img/30/aea4ae24f418eb971bca77a1d46bef.png)
[Verilog quick start of Niuke series] ~ multi function data processor, calculate the difference between two numbers, use generate... For statement to simplify the code, and use sub modules to realize

【14. 区间和(离散化)】
随机推荐
官宣:Apache Doris 顺利毕业,成为 ASF 顶级项目!
tensorflow2-savedmodel convert to tflite
竣达技术丨室内空气环境监测终端 pm2.5、温湿度TVOC等多参数监测
Research Report on development trend and competitive strategy of global consumer glassware industry
MIT团队使用图神经网络,加速无定形聚合物电解质筛选,促进下一代锂电池技术开发
竣达技术丨多台精密空调微信云监控方案
How to view the state-owned enterprises have unloaded Microsoft office and switched to Kingsoft WPS?
Music player development example (can be set up)
Official announcement: Apache Doris graduated successfully and became the top project of ASF!
生成随机数(4位、6位)
tensorflow2-savedmodel convert to tflite
使用net core 6 c# 的 NPOI 包,读取excel..xlsx单元格内的图片,并存储到指定服务器
期末琐碎知识点再整理
手把手带你入门 API 开发
Research Report on the development trend and competitive strategy of the global CCTV robot industry
[14. Interval sum (discretization)]
[leetcode 324] swing sorting II thinking + sorting
[Verilog quick start of Niuke series] ~ multi function data processor, calculate the difference between two numbers, use generate... For statement to simplify the code, and use sub modules to realize
2022-2-15 learning xiangniuke project - Section 1 filtering sensitive words
MIT team used graph neural network to accelerate the screening of amorphous polymer electrolytes and promote the development of next-generation lithium battery technology