全称为Speed-up robust features(加速健壮特征),主要功能是完成两个图片之中的物体匹配。
import cv2
import numpy as np
import logging as log
from torchvision.transforms import Resize
import glob
#img1=cv2.imread('D:/projects/video_flicker/video/result_45.jpg',cv2.IMREAD_GRAYSCALE)
#img2=cv2.imread('D:/projects/video_flicker/video/result_30.jpg',cv2.IMREAD_GRAYSCALE)
img1=cv2.imread('D:/projects/video_flicker/test/test_03.jpg',cv2.IMREAD_GRAYSCALE)
img2=cv2.imread('D:/projects/video_flicker/test/test_04.jpg',cv2.IMREAD_GRAYSCALE)
#提取特征点
#创建SURF对象
surf=cv2.xfeatures2d.SURF_create(10000)#返回关键点信息和描述符
image1=img1.copy()
image2=img2.copy()
keypoint1,descriptor1=surf.detectAndCompute(image1,None)
keypoint2,descriptor2=surf.detectAndCompute(image2,None)
#在图像上绘制关键点(关键点利用Hessian算法找到)
#DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS绘制特征点的时候绘制一个个带方向的圆
image1=cv2.drawKeypoints(image=image1,keypoints=keypoint1,outImage=image1,color=(255,0,255),flags=cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS)
image2=cv2.drawKeypoints(image=image2,keypoints=keypoint2,outImage=image2,color=(255,0,255),flags=cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS)
cv2.imshow('surf_keypoints1',image1)
cv2.imshow('surf_keypoints2',image2)
#特征点匹配
matcher=cv2.FlannBasedMatcher()
matchePoints=matcher.match(descriptor1,descriptor2)
print(type(matchePoints),len(matchePoints),matchePoints[0])
#提取最强匹配
minMatch=1
maxMatch=0
#for i in range(len(matchePoints)):
#print(matchePoints[i].queryIdx,matchePoints[i].trainIdx)#只是打印索引,无法寻找点
#queryIdx为查询点索引,trainIdx为被查询点索引
for i in range(len(matchePoints)):
if minMatch>matchePoints[i].distance:
minMatch=matchePoints[i].distance
if maxMatch
需要python3.7作为开发环境,安装opencv-contrib-python3.4.2.17(之后的版本都被SURF算法申请了专利保护)
上图为特征点匹配结果,下图在原图中确认匹配点位置(绿色),以便后续工作。
zjy----2022.6.30