Python+OpenCV:特征匹配(Feature Matching)

Basics of Brute-Force Matcher

Brute-Force matcher is simple. It takes the descriptor of one feature in first set and is matched with all other features in second set using some distance calculation. And the closest one is returned.

For BF matcher, first we have to create the BFMatcher object using cv.BFMatcher(). It takes two optional params:

First one is normType. It specifies the distance measurement to be used. By default, it is cv.NORM_L2. It is good for SIFT, SURF etc (cv.NORM_L1 is also there).

For binary string based descriptors like ORB, BRIEF, BRISK etc, cv.NORM_HAMMING should be used, which used Hamming distance as measurement. If ORB is using WTA_K == 3 or 4, cv.NORM_HAMMING2 should be used.

Second param is boolean variable, crossCheck which is false by default. If it is true, Matcher returns only those matches with value (i,j) such that i-th descriptor in set A has j-th descriptor in set B as the best match and vice-versa.

That is, the two features in both sets should match each other. It provides consistent result, and is a good alternative to ratio test proposed by D.Lowe in SIFT paper.

Once it is created, two important methods are BFMatcher.match() and BFMatcher.knnMatch(). First one returns the best match.

Second method returns k best matches where k is specified by the user. It may be useful when we need to do additional work on that.

Like we used cv.drawKeypoints() to draw keypoints, cv.drawMatches() helps us to draw the matches.

It stacks two images horizontally and draw lines from first image to second image showing best matches.

There is also cv.drawMatchesKnn which draws all the k best matches. If k=2, it will draw two match-lines for each keypoint. So we have to pass a mask if we want to selectively draw it.

Let's see one example for each of SIFT and ORB (Both use different distance measurements).

Brute-Force Matching with ORB Descriptors

####################################################################################################
# 图像特征匹配(Feature Matching)
def lmc_cv_image_feature_matching():"""函数功能: 图像特征匹配(Feature Matching)。"""# 读取图像image1 = lmc_cv.imread('D:/99-Research/Python/Image/Brochure01.jpg', lmc_cv.IMREAD_GRAYSCALE)image2 = lmc_cv.imread('D:/99-Research/Python/Image/Brochure02.jpg', lmc_cv.IMREAD_GRAYSCALE)# image1 = lmc_cv.imread('D:/99-Research/Python/Image/pcb_rotated_01.png', lmc_cv.IMREAD_GRAYSCALE)# image2 = lmc_cv.imread('D:/99-Research/Python/Image/pcb_rotated_02.png', lmc_cv.IMREAD_GRAYSCALE)# Initiate ORB detectororb = lmc_cv.ORB_create()# find the keypoints and descriptors with ORBkeypoints1, descriptors1 = orb.detectAndCompute(image1, None)keypoints2, descriptors2 = orb.detectAndCompute(image2, None)# create BFMatcher objectbf_matcher = lmc_cv.BFMatcher(lmc_cv.NORM_HAMMING, crossCheck=True)# Match descriptors.matches = bf_matcher.match(descriptors1, descriptors2)# Sort them in the order of their distance.matches = sorted(matches, key=lambda x: x.distance)# Draw first 50 matches.result_image = lmc_cv.drawMatches(image1, keypoints1, image2, keypoints2, matches[:50], None,flags=lmc_cv.DrawMatchesFlags_NOT_DRAW_SINGLE_POINTS)# 显示图像pyplot.figure('Feature Matching')pyplot.subplot(1, 1, 1)pyplot.imshow(result_image, 'gray')pyplot.title('Feature Matching')pyplot.xticks([])pyplot.yticks([])# 根据用户输入保存图像if ord("q") == (lmc_cv.waitKey(0) & 0xFF):# 销毁窗口pyplot.close('all')return

What is this Matcher Object?

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.

Brute-Force Matching with SIFT Descriptors and Ratio Test

####################################################################################################
# 图像特征匹配(Feature Matching SIFT)
def lmc_cv_image_feature_matching_sift():"""函数功能: 图像特征匹配(Feature Matching)。"""# 读取图像image1 = lmc_cv.imread('D:/99-Research/Python/Image/Brochure01.jpg', lmc_cv.IMREAD_GRAYSCALE)image2 = lmc_cv.imread('D:/99-Research/Python/Image/Brochure02.jpg', lmc_cv.IMREAD_GRAYSCALE)# image1 = lmc_cv.imread('D:/99-Research/Python/Image/pcb_rotated_01.png', lmc_cv.IMREAD_GRAYSCALE)# image2 = lmc_cv.imread('D:/99-Research/Python/Image/pcb_rotated_02.png', lmc_cv.IMREAD_GRAYSCALE)# Initiate SIFT detectorsift = lmc_cv.SIFT_create()# find the keypoints and descriptors with SIFTkeypoints1, descriptors1 = sift.detectAndCompute(image1, None)keypoints2, descriptors2 = sift.detectAndCompute(image2, None)# BFMatcher with default paramsbf_matcher = lmc_cv.BFMatcher()matches = bf_matcher.knnMatch(descriptors1, descriptors2, k=2)# Apply ratio testgood = []for m, n in matches:if m.distance < 0.55 * n.distance:good.append([m])# cv.drawMatchesKnn expects list of lists as matches.result_image = lmc_cv.drawMatchesKnn(image1, keypoints1, image2, keypoints2, good, None,flags=lmc_cv.DrawMatchesFlags_NOT_DRAW_SINGLE_POINTS)# 显示图像pyplot.figure('Feature Matching with SIFT')pyplot.subplot(1, 1, 1)pyplot.imshow(result_image, 'gray')pyplot.title('Feature Matching with SIFT')pyplot.xticks([])pyplot.yticks([])# 根据用户输入保存图像if ord("q") == (lmc_cv.waitKey(0) & 0xFF):# 销毁窗口pyplot.close('all')return

FLANN based Matcher

####################################################################################################
# 图像特征匹配(Feature Matching with FLANN)
def lmc_cv_image_feature_matching_flann():"""函数功能: 图像特征匹配(Feature Matching with FLANN)。"""# 读取图像image1 = lmc_cv.imread('D:/99-Research/Python/Image/Brochure01.jpg', lmc_cv.IMREAD_GRAYSCALE)image2 = lmc_cv.imread('D:/99-Research/Python/Image/Brochure02.jpg', lmc_cv.IMREAD_GRAYSCALE)# Initiate SIFT detectorsift = lmc_cv.SIFT_create()# find the keypoints and descriptors with SIFTkeypoints1, descriptors1 = sift.detectAndCompute(image1, None)keypoints2, descriptors2 = sift.detectAndCompute(image2, None)# FLANN parametersflann_index_kdtree = 1index_params = dict(algorithm=flann_index_kdtree, trees=5)search_params = dict(checks=50)  # or pass empty dictionaryflann = lmc_cv.FlannBasedMatcher(index_params, search_params)matches = flann.knnMatch(descriptors1, descriptors2, k=2)# Need to draw only good matches, so create a maskmatches_mask = [[0, 0] for i in range(len(matches))]# ratio test as per Lowe's paperfor i, (m, n) in enumerate(matches):if m.distance < 0.7 * n.distance:matches_mask[i] = [1, 0]draw_params = dict(matchColor=(0, 255, 0),singlePointColor=(255, 0, 0),matchesMask=matches_mask,flags=lmc_cv.DrawMatchesFlags_DEFAULT)# cv.drawMatchesKnn expects list of lists as matches.result_image = lmc_cv.drawMatchesKnn(image1, keypoints1, image2, keypoints2, matches, None, **draw_params)# 显示图像pyplot.figure('Feature Matching with FLANN')pyplot.subplot(1, 1, 1)pyplot.imshow(result_image, 'gray')pyplot.title('Feature Matching with FLANN')pyplot.xticks([])pyplot.yticks([])# 根据用户输入保存图像if ord("q") == (lmc_cv.waitKey(0) & 0xFF):# 销毁窗口pyplot.close('all')return

Python+OpenCV:特征匹配(Feature Matching)相关推荐

  1. python SIFT特征匹配

    python SIFT特征匹配 SIFT(尺度不变特征变换) 兴趣点 描述子 检测兴趣点 匹配描述子 匹配地理标记图像 用局部描述子进行匹配 可视化连接的图像 实验代码 实验结果 结果分析 SIFT( ...

  2. OpenCV特征描述Feature Description

    OpenCV特征描述Feature Description 特征描述Feature Description 目标 代码 结果 特征描述Feature Description 目标 在本教程中,您将学习 ...

  3. OpenCV立体声匹配 stereo matching将L和R图像转换为视差和点云的实例(附完整代码)

    OpenCV立体声匹配 stereo matching将L和R图像转换为视差和点云的实例 OpenCV立体声匹配 stereo matching将L和R图像转换为视差和点云的实例 OpenCV立体声匹 ...

  4. OpenCV模板匹配Template Matching

    OpenCV模板匹配Template Matching 模板匹配Template Matching 目标 理论 什么是模板匹配? OpenCV提供哪些匹配方法? 代码 解释 结果 模板匹配Templa ...

  5. opencv 特征匹配和

    import numpy as np import cv2 as cv from matplotlib import pyplot as plt MIN_MATCH_COUNT = 10 # 设置最小 ...

  6. python opencv特征点检测和匹配教程

    导入相关的包 import cv2 import numpy as np import random import os import matplotlib.pyplot as plt from ti ...

  7. ubuntu使用python opencv_Ubuntu中“利用Opencv + python进行特征匹配”的环境搭建

    2.安装Python插件:点击Vscode左侧第五个方框对应的功能,搜索Python并安装 3.安装pip (以下步骤均在终端Terminal中执行) sudo apt install pytho-p ...

  8. python+OpenCV 特征点检测

    1.Harris角点检测 Harris角点检测算法是一个极为简单的角点检测算法,该算法在1988年就被发明了,算法的主要思想是如果像素周围显示存在多于一个方向的边,我们认为该点为兴趣点.基本原理是根据 ...

  9. python opencv模板匹配多目标_基于opencv的多目标模板匹配

    利用opencv进行多目标模板匹配,只要是利用其matchTemplate函数,但在多目标(这里是讨论目标图片中不同大小模板的匹配),以下贴出代码和图片,供大家参考: #include #includ ...

  10. 特征工程用java或python,机器学习-特征工程-Feature generation 和 Feature selection(示例代码)...

    Feature generation.对于这个技术点,其实没有什么诀窍,就是一个,深刻理解咱们的数据的意义再加上一点点创造力.大家是不是很懵逼,哈哈,这就完啦????哈哈当然不是啦,但是这一块缺失没有 ...

最新文章

  1. oracle-Oracle试题
  2. bootstrap table使用参考
  3. C++设计模式之装饰模式
  4. 29. ExtJs - Struts2 整合(1) - 登录页面
  5. 【POJ - 3320 】Jessica's Reading Problem (尺取,哈希)
  6. 作者:刘昂(1990-),男,中国科学院计算机网络信息中心工程师
  7. Xshell和Xftp5配置连接阿里云服务器
  8. Struts2中的图片验证码
  9. cass光标大小怎么调_cass7period;0鼠标不显示怎么解决quest;
  10. 高通最强芯片855发布!AI性能比华为苹果翻倍,商用5G,标配屏下指纹
  11. webservice wsdl 生成服务
  12. OpenCL 学习step by step (5) 使用二维NDRange workgroup
  13. java 读取html字符串替换字符
  14. composer错误Could not find package 的解决方法
  15. ColorPix——到目前为止最好用的屏幕取色器
  16. Windows 9预览版今秋发布 正式版明年推出
  17. 华为p40还用麒麟990鸿蒙,华为P40渲染图曝光,2K挖孔屏+麒麟990+鸿蒙OS
  18. java bt下载_bt: Java种子下载程序
  19. 采用Cartographer、LIO-SAM构建三维点云地图,采用Octomap构建八叉树地图(三维栅格地图)
  20. 【PHP】如何提高网页加载速度?

热门文章

  1. Linux 命令(100)—— expr 命令
  2. Python全栈开发——subprocess struct
  3. Shiro 整合SpringMVC 并实现权限管理,登录和注销
  4. Mysql 排序null值 排序问题分析
  5. 利用jaxp对xml进行dom解析
  6. Bigendian 奇数内存地址取整形crash
  7. CentOs 设置静态IP 方法[测试没问题]
  8. .net mvc html5,带有.NET MVC 3 Razor Editor的HTML5占位符
  9. 使用C#创建Windows服务
  10. 4Python全站之路系列之正则表达式re模块