Python-opencv3 特征匹配match和drawMatches的使用
来源:互联网 发布:淘宝客服名称大全 编辑:程序博客网 时间:2024/05/21 05:44
The result of matches = bf.match(des1,des2) line is a list of DMatch objects. This DMatch object has following attributes: DMatch.distance - Distance between descriptors. The lower, the better it is. DMatch.trainIdx - Index of the descriptor in train descriptors DMatch.queryIdx - Index of the descriptor in query descriptors DMatch.imgIdx - Index of the train image.
import cv2img1 = cv2.imread('s1.jpg') # queryImageimg2 = cv2.imread('s2.jpg') # trainImage#%% BF+ORB methoddef BF_ORB(): # Initiate ORB detector orb = cv2.ORB_create() # find the keypoints and descriptors with SIFT kp1, des1 = orb.detectAndCompute(img1,None) kp2, des2 = orb.detectAndCompute(img2,None) # create BFMatcher object bf = cv2.BFMatcher(cv2.NORM_HAMMING, crossCheck=True) # Match descriptors. matches = bf.match(des1,des2) # Sort them in the order of their distance. matches = sorted(matches, key = lambda x:x.distance) # Draw first 10 matches. img3 = cv2.drawMatches(img1,kp1,img2,kp2,matches[0:10], None,flags=2) cv2.imshow('result.jpg',img3) cv2.waitKey(0) cv2.destroyAllWindows() cv2.waitKey(1) cv2.waitKey(1) cv2.waitKey(1) cv2.waitKey(1) #%%BF+SIFTdef BF_SIFT(): # Initiate SIFT detector sift = cv2.xfeatures2d.SIFT_create() # find the keypoints and descriptors with SIFT kp1, des1 = sift.detectAndCompute(img1,None) kp2, des2 = sift.detectAndCompute(img2,None) # BFMatcher with default params bf = cv2.BFMatcher() matches = bf.knnMatch(des1,des2, k=2) # Apply ratio test good = [] for m,n in matches: if m.distance < 0.75*n.distance: good.append([m]) # cv2.drawMatchesKnn expects list of lists as matches. img3 = cv2.drawMatchesKnn(img1,kp1,img2,kp2,good[:10],None, flags=2) cv2.imshow('result.jpg',img3) cv2.waitKey(0) cv2.destroyAllWindows() cv2.waitKey(1) cv2.waitKey(1) cv2.waitKey(1) cv2.waitKey(1) def FLANN(): ''' FLANN stands for Fast Library for Approximate Nearest Neighbors. It contains a collection of algorithms optimized for fast nearest neighbor search in large datasets and for high dimensional features. It works more faster than BFMatcher for large datasets. We will see the second example with FLANN based matcher. For FLANN based matcher, we need to pass two dictionaries which specifies the algorithm to be used, its related parameters etc. First one is IndexParams. For various algorithms, the information to be passed is explained in FLANN docs. As a summary, for algorithms like SIFT, SURF etc. you can pass following: index_params = dict(algorithm = FLANN_INDEX_KDTREE, trees = 5) While using ORB, you can pass the following. The commented values are recommended as per the docs, but it didn't provide required results in some cases. Other values worked fine: index_params= dict(algorithm = FLANN_INDEX_LSH, table_number = 6, # 12 key_size = 12, # 20 multi_probe_level = 1) #2 Second dictionary is the SearchParams. It specifies the number of times the trees in the index should be recursively traversed. Higher values gives better precision, but also takes more time. If you want to change the value, pass search_params = dict(checks=100). ''' sift = cv2.xfeatures2d.SIFT_create() # find the keypoints and descriptors with SIFT kp1, des1 = sift.detectAndCompute(img1,None) kp2, des2 = sift.detectAndCompute(img2,None) # FLANN parameters FLANN_INDEX_KDTREE = 0 index_params = dict(algorithm = FLANN_INDEX_KDTREE, trees = 5) search_params = dict(checks=50) # or pass empty dictionary flann = cv2.FlannBasedMatcher(index_params,search_params) matches = flann.knnMatch(des1,des2,k=2) # Need to draw only good matches, so create a mask matchesMask = [[0,0] for i in range(len(matches))] # ratio test as per Lowe's paper for i,(m,n) in enumerate(matches): if m.distance < 0.7*n.distance: matchesMask[i]=[1,0] draw_params = dict(matchColor = (0,255,0), singlePointColor = (255,0,0), matchesMask = matchesMask, flags = 0) img3 = cv2.drawMatchesKnn(img1,kp1,img2,kp2,matches,None,**draw_params) cv2.imshow('result.jpg',img3) cv2.waitKey(0) cv2.destroyAllWindows() cv2.waitKey(1) cv2.waitKey(1) cv2.waitKey(1) cv2.waitKey(1) #%% if __name__ == '__main__': FLANN()
阅读全文
0 0
- Python-opencv3 特征匹配match和drawMatches的使用
- python-opencv-特征点匹配连线(画线)drawMatches
- python的正则匹配,match和find
- OpenCV3中的SURF特征点的寻找和匹配
- 【OpenCV】OpenCV3中的SURF特征点的寻找和匹配
- opencv3中使用FLANN进行特征点匹配
- Python 正则匹配之match和search
- opencv3.1 surf特征匹配
- Python 字符串匹配(match)
- python中match的使用
- opencv3.2 SURF实现特征点匹配
- ORB特征提取匹配opencv3代码实现
- Match的正则匹配
- OPENCV3的匹配
- 人脸的匹配-----直方图匹配和SURF特征匹配
- opencv3 环境安装及实验surf特征的描述符匹配文章整理
- Scala模式匹配(match)使用
- python 中,match和search的区别
- tomcat 8 上传的图片无法访问 forbidden
- AJAX
- UNIX环境高级编程 源代码编译apue.h头文件的问题
- 打开r40m平台Android6.0下的以太网卡(分色排版)
- Ubuntu完美搭建git服务器-完善版
- Python-opencv3 特征匹配match和drawMatches的使用
- eclipse与idea常用快捷键对比
- java堆、栈、堆栈的区别
- 遍历控件属性
- 初学者的学习复习day_7
- 三种数据库常见的URL以及端口
- C语言_初成长_数组
- Java 8的新特性—终极版
- SpringMVC-1