BRISK算法是2011年ICCV上《BRISK:Binary Robust Invariant Scalable Keypoints》文章中,提出来的一种特征提取算法,也是一种二进制的特征描述算子。
它具有较好的旋转不变性、尺度不变性,较好的鲁棒性等。
在图像配准应用中,速度比较:SIFT<SURF<BRISK<FREAK<ORB,
在对有较大模糊的图像配准时,BRISK算法在其中表现最为出色。
代码示例:
#include <opencv2/opencv.hpp>
#include <iostream>
using namespace cv;
using namespace std;
int main(int argc, char** argv)
{
Mat img1 = imread("D:/cv400/data/box.png", 0);
Mat img2 = imread("D:/cv400/data/box_in_scene.png", 0);
if (img1.empty() || img2.empty())
{
cout << "Load image error..." << endl;
return -1;
}
imshow("object image", img1);
imshow("object in scene", img2);
// surf featurs extraction
double t1 = (double)getTickCount();
//int minHessian = 400;
Ptr<Feature2D> detector = BRISK::create();
vector<KeyPoint> keypoints_obj;
vector<KeyPoint> keypoints_scene;
Mat descriptor_obj, descriptor_scene;
detector->detectAndCompute(img1, Mat(), keypoints_obj, descriptor_obj);
detector->detectAndCompute(img2, Mat(), keypoints_scene, descriptor_scene);
// matching
BFMatcher matcher(NORM_L2);
vector<DMatch> matches;
matcher.match(descriptor_obj, descriptor_scene, matches);
double t2 = (double)getTickCount();
double t = (t2 - t1) / getTickFrequency();
cout << "spend time : " << t << "s" << endl;
//求匹配点最近距离
double minDist = 1000;
for (int i = 0; i < descriptor_obj.rows; i++)
{
double dist = matches[i].distance;
if (dist < minDist)
minDist = dist;
}
cout<<"min distance : "<< minDist<<endl;
//距离较近即匹配较好的点
vector<DMatch> goodMatches;
for (int i = 0; i < descriptor_obj.rows; i++)
{
double dist = matches[i].distance;
if (dist < max(2* minDist, 0.02))
goodMatches.push_back(matches[i]);
}
//寻找匹配上的关键点的变换
vector<Point2f> obj; //目标特征点
vector<Point2f> objInScene; //场景中目标特征点
for (size_t t = 0; t < goodMatches.size(); t++)
{
obj.push_back(keypoints_obj[goodMatches[t].queryIdx].pt);
objInScene.push_back(keypoints_scene[goodMatches[t].trainIdx].pt);
}
Mat imgBH = findHomography(obj, objInScene, RANSAC);
//映射点
vector<Point2f> obj_corners(4);
vector<Point2f> scene_corners(4);
obj_corners[0] = Point(0, 0);
obj_corners[1] = Point(img1.cols, 0);
obj_corners[2] = Point(img1.cols, img1.rows);
obj_corners[3] = Point(0, img1.rows);
perspectiveTransform(obj_corners, scene_corners, imgBH);
//四个点之间画线
Mat dst;
cvtColor(img2, dst, COLOR_GRAY2BGR);
for(int i=0;i<4;i++)
line(dst, scene_corners[i%4], scene_corners[(i+1)%4], Scalar(0, 0, 255), 2, 8, 0);
imshow("find object in sence", dst);
waitKey(0);
return 0;
}
结果: