orb关键点匹配判定出现问题,只能在及小范围匹配出一个点
使用orb对文件夹内图片顺序进行特征点匹配,且前两张图片相对静止,然而匹配上的关键点只有左上方的的一个,关键点数量太少,后续程序很容易因为匹配关键点丢失而出现错误。请问是我代码中的范围设定出现了问题还是其他的有问题。
import cv2
import numpy
import os
from math import sqrt, pow, acos
def cv_show(name, img):
cv2.imshow(name, img)
cv2.waitKey(10000)
cv2.destroyAllWindows()
def orb_features(img):
orb = cv2.ORB_create(
5000, # 特征点数目
1.2, # 金字塔层级之间的缩放比例
8, # 金字塔图像层级系数
31, # 边缘阈值
0, # 原图在金字塔中的层数
2, # 生成描述子时需要用的像素点数目
cv2.ORB_HARRIS_SCORE, # 使用Harris方法评价特征点
31, # 生成描述子时关键点周围邻域的尺寸
20 # 计算FAST角点时像素值差值的阈值
)
kp = orb.detect(img, None)
kp, des = orb.compute(img, kp)
return kp, des
if __name__ == '__main__':
# f = open("output.txt")
img_path = "D://python//txsc"
for img in range(647):
# print(f"{i}.jpg".rjust(10, "0"))
img_name1 = f"{img}.jpg".rjust(10, "0")
img_name2 = f"{img + 1}.jpg".rjust(10, "0")
img_name1 = os.path.join(img_path, img_name1)
img_name2 = os.path.join(img_path, img_name2)
# 读取图片并截图
img1 = cv2.imread(img_name1, cv2.IMREAD_GRAYSCALE)[350:1450, 450:1450]
img2 = cv2.imread(img_name2, cv2.IMREAD_GRAYSCALE)[350:1450, 450:1450]
# 提取ORB特征点
kp1, des1 = orb_features(img1)
kp2, des2 = orb_features(img2)
# 匹配
matcher = cv2.BFMatcher(cv2.NORM_HAMMING) # 定义特征点匹配的类
matches = matcher.match(des1, des2) # 进行特征点匹配
min_dist = 10000
max_dist = 0
# 提取最大距离和最小距离
for i in range(len(matches)):
dist = matches[i].distance
if dist < min_dist:
min_dist = dist
if dist > max_dist:
max_dist = dist
"""
//将汉明距离较大的匹配点对剔除,并缩小特征点选取范围
vector<DMatch> good_matches;//用于储存剔除完的结果
int matchidx1, matchidx2;//用于储存匹配点在查询子和描述子中的序号
float matchx1, matchy1;//用于储存匹配点在查询子中的坐标
float matchx2, matchy2;//用于储存匹配点在描述子中的坐标
"""
good_matches = []
for i in range(len(matches)):
matchidx1 = matches[i].queryIdx
matchidx2 = matches[i].trainIdx
matchx1, matchy1 = kp1[matchidx1].pt
matchx2, matchy2 = kp2[matchidx2].pt
if (matches[i].distance <= max(3 * min_dist, 50) and
(sqrt(pow((matchx1 - 491), 2) + pow((matchy1 - 331), 2)) > 150) and
(sqrt(pow((matchx1 - 491), 2) + pow((matchy1 - 331), 2)) < 350) and
(sqrt(pow((matchx2 - 491), 2) + pow((matchy2 - 331), 2)) > 150) and
(sqrt(pow((matchx2 - 491), 2) + pow((matchy2 - 331), 2)) < 350) and
(matchy1 < 0.9 * matchx1 - 1.5) and
(matchy1 < -1.83 * matchx1 + 800) and
(matchy2 < 0.9 * matchx2 - 1.5) and
(matchy2 < -1.83 * matchx2 + 800)):
good_matches.append(matches[i])
draw_match(img1, img2, kp1, kp2, good_matches)
def draw_match(img1, img2, kp1, kp2, match):
outimage = cv2.drawMatches(img1, kp1, img2, kp2, match, outImg=None)
cv2.imshow("Match Result", outimage)
cv2.waitKey(0)
静止的图片能够匹配到的特征点只有左上角的1个
特征点匹配范围根据之前程序检测到的(491, 331),R=302
且灰度图和彩色图,改变图像大小都尝试过了并没有影响,匹配点仍然只有一个且存在图像的左上方
大概知道问题出在匹配范围划定的函数那里,因为正在学习真部分函数的用法,也不太了解,请各位帮我看看这里应该怎么调整,或者帮我讲解一下 if (matches[i].distance <= max(3 * min_dist, 50) 之后的sqrt()与pow()具体能达到的功能,在我调整之后能够在识别出的圆的范围内增加能够增加可靠的匹配上的特征点数量
不就是两点(x1,y1),(x2,y2)间距离公式
d=sqrt((x1-x2)*(x1-x2)+(y1-y1)*(y1-y2))
吗?