OpenCV实践之LATCH描述符匹配算法

LATCH描述符简介

Learned Arrangements of Three Patch Codes(LATCH)是2015年CVPR关于二值化描述符优化变种二值特征描述子。经典二值描述子LBP等主要通过计算特征点局部窗口内n个邻域点对进行比较值来形成bit串,形成bit串后通过计算汉明码来提升计算速度。相关二值改进描述子使用滤波算法来降低局部邻域噪声的干扰,但是也一定程度降低局部邻域的唯一性。LATCH作者提出计算窗口内像素块比较值来形成bit串,同时作者提出如何定位像素块方法。当然,LATCH描述符匹配性能要高于先前的二值描述特征,低于非二值描述特征;计算耗时高于二值化特征,低于非二值化特征。因此,LATCH描述符处于一个折中方案策略,感觉速度不如ORB、AKAZE等算法,性能不如SIFT、SURF等算法。不过,可能通过组合特征+描述符会发挥其作用。相关具体细节参考论文LATCH或者作者官网

OpenCV代码LATCH匹配:
// LATCH_match.cpp : 定义控制台应用程序的入口点。
#include "stdafx.h"
#include <iostream>
#include "opencv2/opencv_modules.hpp"
#ifdef HAVE_OPENCV_XFEATURES2D

#include <opencv2/features2d.hpp>
#include <opencv2/xfeatures2d.hpp>
#include <opencv2/imgcodecs.hpp>
#include <opencv2/opencv.hpp>
#include <vector>

// If you find this code useful, please add a reference to the following paper in your work:
// Gil Levi and Tal Hassner, "LATCH: Learned Arrangements of Three Patch Codes", arXiv preprint arXiv:1501.03719, 15 Jan. 2015

using namespace std;
using namespace cv;

const float inlier_threshold = 2.5f; // Distance threshold to identify inliers
const float nn_match_ratio = 0.8f;   // Nearest neighbor matching ratio

int main(void)
{	//-- 待匹配图像对读取
	Mat img1 = imread("..//LATCH_match//images//img1.png", IMREAD_GRAYSCALE);
	Mat img2 = imread("..//LATCH_match//images//img3.png", IMREAD_GRAYSCALE);
    //-- 读取同质矩阵
	Mat homography;
	FileStorage fs("..//LATCH_match//images//H1to3p.xml", FileStorage::READ);

	fs.getFirstTopLevelNode() >> homography;
	//-- 初始化特征点与描述子
	vector<KeyPoint> kpts1, kpts2;
	Mat desc1, desc2;

	Ptr<cv::ORB> orb_detector = cv::ORB::create(10000);
	// opencv-contrib xfeatures2d 初始化LATCH描述符
	Ptr<xfeatures2d::LATCH> latch = xfeatures2d::LATCH::create();

	/** 
	@param images Image set.
	@param keypoints Input collection of keypoints. Keypoints for which a descriptor cannot be
	computed are removed. Sometimes new keypoints can be added, for example: SIFT duplicates keypoint
	with several dominant orientations (for each orientation).
	@param descriptors Computed descriptors. In the second variant of the method descriptors[i] are
	descriptors computed for a keypoints[i]. Row j is the keypoints (or keypoints[i]) is the
	descriptor for keypoint j-th keypoint.
	*/
	//CV_WRAP virtual void compute(InputArrayOfArrays images,
	//	CV_OUT CV_IN_OUT std::vector<std::vector<KeyPoint> >& keypoints,
	//	OutputArrayOfArrays descriptors);

	///** Detects keypoints and computes the descriptors */
	//CV_WRAP virtual void detectAndCompute(InputArray image, InputArray mask,
	//	CV_OUT std::vector<KeyPoint>& keypoints,
	//	OutputArray descriptors,
	//	bool useProvidedKeypoints = false);
	orb_detector->detect(img1, kpts1);
	latch->compute(img1, kpts1, desc1);
	//latch->detectAndCompute();
	orb_detector->detect(img2, kpts2);
	latch->compute(img2, kpts2, desc2);

	BFMatcher matcher(NORM_HAMMING);
	vector< vector<DMatch> > nn_matches;
	matcher.knnMatch(desc1, desc2, nn_matches, 2);

	vector<KeyPoint> matched1, matched2, inliers1, inliers2;
	vector<DMatch> good_matches;
	for (size_t i = 0; i < nn_matches.size(); i++) {
		DMatch first = nn_matches[i][0];
		float dist1 = nn_matches[i][0].distance;
		float dist2 = nn_matches[i][1].distance;

		if (dist1 < nn_match_ratio * dist2) {
			matched1.push_back(kpts1[first.queryIdx]);
			matched2.push_back(kpts2[first.trainIdx]);
		}
	}

	for (unsigned i = 0; i < matched1.size(); i++) {
		Mat col = Mat::ones(3, 1, CV_64F);
		col.at<double>(0) = matched1[i].pt.x;
		col.at<double>(1) = matched1[i].pt.y;

		col = homography * col;
		col /= col.at<double>(2);
		double dist = sqrt(pow(col.at<double>(0) - matched2[i].pt.x, 2) +
			pow(col.at<double>(1) - matched2[i].pt.y, 2));

		if (dist < inlier_threshold) {
			int new_i = static_cast<int>(inliers1.size());
			inliers1.push_back(matched1[i]);
			inliers2.push_back(matched2[i]);
			good_matches.push_back(DMatch(new_i, new_i, 0));
		}
	}

	Mat res;
	drawMatches(img1, inliers1, img2, inliers2, good_matches, res);
	imwrite("latch_result.png", res);

	double inlier_ratio = inliers1.size() * 1.0 / matched1.size();
	cout << "LATCH Matching Results" << endl;
	cout << "*******************************" << endl;
	cout << "# Keypoints 1:                        \t" << kpts1.size() << endl;
	cout << "# Keypoints 2:                        \t" << kpts2.size() << endl;
	cout << "# Matches:                            \t" << matched1.size() << endl;
	cout << "# Inliers:                            \t" << inliers1.size() << endl;
	cout << "# Inliers Ratio:                      \t" << inlier_ratio << endl;
	cout << endl;

	imshow("result", res);
	waitKey(0);

	return 0;
}
#else
int main()
{
	std::cerr << "OpenCV was built without xfeatures2d module" << std::endl;
	return 0;
}
#endif

LATCH示例匹配结果
ORB+LATCH描述符匹配性能:
Oxford数据集描述子比较:LATCH性能要优于二值化描述子(ORB、BRISK......),弱于非二值化描述子(SIFT、SURF......)
参考文献

https://arxiv.org/pdf/1501.03719.pdf
https://gilscvblog.wordpress.com/
https://docs.opencv.org/4.0.0-beta/

  • 0
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
基于描述符的模板匹配可以通过以下步骤实现: 1. 读取模板图片和待匹配图片,并将图片转换为灰度图像。 2. 使用ORB、SIFT、SURF等算法提取模板图片和待匹配图片的特征点和对应的描述符。 3. 使用描述符匹配算法(如FLANN、Brute-Force等)计算模板图片和待匹配图片的描述符匹配结果。 4. 根据匹配结果,获取最佳匹配点的坐标。可以使用OpenCV中的`cv2.minMaxLoc()`函数来实现。 5. 将匹配点的坐标绘制在待匹配图片上,并显示匹配结果。 下面是一个基于ORB算法的示例代码: ```python import cv2 # 读取模板图片和待匹配图片 template = cv2.imread('template.png', 0) img = cv2.imread('img.png', 0) # 初始化ORB特征检测器和描述符提取器 orb = cv2.ORB_create() # 提取模板图片和待匹配图片的特征点和对应的描述符 kp1, des1 = orb.detectAndCompute(template, None) kp2, des2 = orb.detectAndCompute(img, None) # 使用描述符匹配算法计算匹配结果 bf = cv2.BFMatcher(cv2.NORM_HAMMING, crossCheck=True) matches = bf.match(des1, des2) # 获取最佳匹配点的坐标 min_dist = 100 for m in matches: if m.distance < min_dist: min_dist = m.distance best_match = m pt1 = kp1[best_match.queryIdx].pt pt2 = kp2[best_match.trainIdx].pt # 在待匹配图片上绘制匹配结果 img_match = cv2.drawMatches(template, kp1, img, kp2, [best_match], None, flags=2) # 显示匹配结果 cv2.imshow('Matching result', img_match) cv2.waitKey(0) cv2.destroyAllWindows() ``` 其中,`cv2.BFMatcher()`函数用于初始化描述符匹配算法,`cv2.drawMatches()`函数用于绘制匹配结果。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值