BRISK算法在OpenCV3.0中的使用
在OpenCV3.0中一些局部特征相关的接口还是有一些改动的,因为最近在测试BRISK部分内容,所以将OpenCV3.0中BRISK的使用贴出来,其他局部特征提取、匹配的算法类似。
【注意】BRISK算法descriptors是二进制串,因此最后进行特征点匹配时是使用的汉明距离,调用的radiusMatch进行匹配**
代码
#include <opencv2/core/core.hpp>
#include <opencv2/features2d/features2d.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <iostream>
#include <Windows.h>
using namespace cv;
int main()
{
//Load Image
Mat im1 = imread("scene.pgm", CV_LOAD_IMAGE_GRAYSCALE);
Mat im2 = imread("basmati.pgm", CV_LOAD_IMAGE_GRAYSCALE);
if (!im1.data || !im2.data)
{
std::cout << "Error reading images " << std::endl;
return -1;
}
//构建BRISK对象,参数都与BRISK论文保持一致
Ptr<BRISK> brisk = BRISK::create(70, 4, 1.0f);
std::vector<KeyPoint> kp1, kp2; //im1,im2 keypoints
Mat des1, des2;//im1,im2 descriptors
double start = GetTickCount();//记录起始时间
brisk->detectAndCompute(im1, Mat(), kp1, des1, false);
brisk->detectAndCompute(im2, Mat(), kp2, des2, false);
BFMatcher matcher(NORM_HAMMING,true);
std::vector<std::vector<DMatch>> matches;
//此处参数很重要,90.0f为判定为相似特征点的汉明距离阈值
matcher.radiusMatch(des1, des2, matches, 90.0f, Mat(), true);
double end = GetTickCount();
std::cout << "Brisk耗时:" << (end - start) << "ms" << std::endl;
std::cout << "Img1特征点数: " << kp1.size() << std::endl;
std::cout << "Img2特征点数: " << kp2.size() << std::endl;
Mat img_match;
drawMatches(im1, kp1, im2, kp2, matches, img_match, Scalar::all(-1), Scalar::all(-1), std::vector<std::vector<char> >(), DrawMatchesFlags::NOT_DRAW_SINGLE_POINTS);
int matchNum = 0;//总的匹配点数
for (int i = 0; i < matches.size() && matches[i].size() > 0; i++)
{
++matchNum;
if (matches[i].size() < 1)
break;
}
std::cout << "Brisk匹配点数: " << matchNum << std::endl;
imshow("Brisk", img_match);
waitKey(0);
return 0;
}