opencv-107-Brisk特征提取与描述子匹配

知识点

BRISK(Binary robust invariant scalable keypoints)是一种基于尺度空间不变性类似ORB特征描述子的特征提取算法。BRISK主要步骤可以分为如下两步:

  1. 构建尺度空间金字塔实现关键点定位
  2. 根据关键点生成描述子

代码(c++,python)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <opencv2/opencv.hpp>
#include <iostream>

using namespace cv;
using namespace std;

int main(int argc, char** argv) {
Mat box = imread("D:/images/box.png");
Mat box_in_sence = imread("D:/images/box_in_scene.png");

// 创建BRISK
auto brisk_detector = BRISK::create();
vector<KeyPoint> kpts_01, kpts_02;
Mat descriptors1, descriptors2;
brisk_detector->detectAndCompute(box, Mat(), kpts_01, descriptors1);
brisk_detector->detectAndCompute(box_in_sence, Mat(), kpts_02, descriptors2);

// 定义描述子匹配 - 暴力匹配
Ptr<DescriptorMatcher> matcher = DescriptorMatcher::create(DescriptorMatcher::BRUTEFORCE);
std::vector< DMatch > matches;
matcher->match(descriptors1, descriptors2, matches);

// 绘制匹配
Mat img_matches;
drawMatches(box, kpts_01, box_in_sence, kpts_02, matches, img_matches);
imshow("AKAZE-Matches", img_matches);
imwrite("D:/result.png", img_matches);

waitKey(0);
return 0;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
import cv2 as cv

box = cv.imread("D:/images/box.png");
box_in_sence = cv.imread("D:/images/box_in_scene.png");
cv.imshow("box", box)
cv.imshow("box_in_sence", box_in_sence)

# 创建BRISK特征检测器
brisk = cv.BRISK_create()
kp1, des1 = brisk.detectAndCompute(box,None)
kp2, des2 = brisk.detectAndCompute(box_in_sence,None)

# 暴力匹配
bf = cv.BFMatcher(cv.NORM_HAMMING, crossCheck=True)
matches = bf.match(des1,des2)

# 绘制匹配
result = cv.drawMatches(box, kp1, box_in_sence, kp2, matches, None)
cv.imshow("orb-match", result)
cv.waitKey(0)
cv.destroyAllWindows()

结果

代码地址

github