Mat cv::findHomography (
InputArray srcPoints,
InputArray dstPoints,
int method = 0,
double ransacReprojThreshold = 3,
OutputArray mask = noArray(),
const int maxIters = 2000,
const double confidence = 0.995
参数详解:
srcPoints 源平面中点的坐标矩阵,可以是CV_32FC2类型,也可以是vector类型
dstPoints 目标平面中点的坐标矩阵,可以是CV_32FC2类型,也可以是vector类型
method 计算单应矩阵所使用的方法。不同的方法对应不同的参数,具体如下:
0 - 利用所有点的常规方法
RANSAC - RANSAC-基于RANSAC的鲁棒算法
LMEDS - 最小中值鲁棒算法
RHO - PROSAC-基于PROSAC的鲁棒算法
ransacReprojThreshold
将点对视为内点的最大允许重投影错误阈值(仅用于RANSAC和RHO方法)。如果∥ dstPoints i− convertPointsHomogeneous (H∗srcPointsi)∥> ransacReprojThreshold 则点被认为是个外点(即错误匹配点对)。若srcPoints和dstPoints是以像素为单位的,则该参数通常设置在1到10的范围内。
angle:角度,表示关键点的方向,为了保证方向不变形,SIFT算法通过对关键点周围邻域进行梯度运算,求得该点方向。-1为初值。
class_id:当要对图片进行分类时,我们可以用class_id对每个特征点进行区分,未设定时为-1,需要靠自己设定
octave:代表是从金字塔哪一层提取的得到的数据。
pt:关键点点的坐标
response:响应程度,代表该点强壮大小,更确切的说,是该点角点的程度。
size:该点直径的大小
3. match与KnnMatch返回值解释
二者的返回值都是DMatch的数据结构。
match:
bf = cv.BFMatcher_create()
matches = bf.match(des1, des2)
for matche in matches:
print(matche)
<DMatch 0x7fcf509b90b0>
<DMatch 0x7fcf509b90d0>
<DMatch 0x7fcf509b90f0>
<DMatch 0x7fcf509b9110>
主要含有三个非常重要的数据:queryIdx,trainIdx,distance
queryIdx:测试图像的特征点描述符的下标(第几个特征点描述符),同时也是描述符对应特征点的下标。
trainIdx:样本图像的特征点描述符下标,同时也是描述符对应特征点的下标。
distance:代表这对匹配的特征点描述符的欧式距离,数值越小也就说明两个特征点越相似。
Match匹配是“最佳”匹配,所以返回值list中的元素类型都是单个Dmatch,而 KnnMatch在设定参数 k = 2 后,就会对同一点进行最佳匹配和次佳匹配,所以返回值list中的元素类型都是一对Dmatch,也即[Dmatch, Dmatch]
1. Stitcher.py
主要用于定义Stitcher类,封装了拼接两幅视角不同的图像的基本流程和方法。
import numpy as np
import cv2
class Stitcher:
def stitch(self, images, ratio=0.75, reprojThresh=4.0,showMatches=False):
(imageB, imageA) = images
(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
(matches, H, status) = M
result = cv2.warpPerspective(imageA, H, (imageA.shape[1] + imageB.shape[1], imageA.shape[0]))
self.cv_show('result', 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):
cv2.imshow(name, img)
cv2.waitKey(0)
cv2.destroyAllWindows()
def detectAndDescribe(self, image):
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
descriptor = cv2.SIFT_create()
(kps, features) = descriptor.detectAndCompute(image, None)
kps = np.float32([kp.pt for kp in kps])
return (kps, features)
def matchKeypoints(self, kpsA, kpsB, featuresA, featuresB, ratio, reprojThresh):
matcher = cv2.BFMatcher()
rawMatches = matcher.knnMatch(featuresA, featuresB, 2)
matches = []
for m in rawMatches:
if len(m) == 2 and m[0].distance < m[1].distance * ratio:
matches.append((m[0].trainIdx, m[0].queryIdx))
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)
return None
def drawMatches(self, imageA, imageB, kpsA, kpsB, matches, status):
(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, 0, 255), 1)
return vis
2. ImageStiching.py
主函数,控制程序执行流程
from Stitcher import Stitcher
import cv2
imageA = cv2.imread("left_01.png")
imageB = cv2.imread("right_01.png")
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()
四、效果展示
右边图像:
将右边图像进行映射变换(单应)并进行填充后的结果:
可以看出上述图像有少许失真。
拼接成功的图像:
左边图像无需处理,直接填入即可。
两张原图像和Knn特征匹配后的结果:
图像拼接只需要改动其中一张图像的“视角”,也就是只需要对其中一张图像进行单应变换。且最终结果就是直接拼接。
注意detectAndCompute方法返回的kps中返回的不止是坐标信息还有角度、大小等信息
注意KnnMatch的返回值信息