编程环境:
VS + OpenCV + C++
完整代码已经更新至GitHub,欢迎fork~GitHub链接
声明:创作不易,未经授权不得复制转载
statement:No reprinting without authorization
内容:
• 了解OpenCV中实现的SIFT, SURF, ORB等特征检测器的用法,并进行实验。将检测到的特征点用不同大小的圆表示,比较不同方法的效率、效果等。
• 了解OpenCV的特征匹配方法,并进行实验。
一、opencv特征检测和匹配的通用步骤及Code
//步骤一:读取图片并将图片灰度化
//code:
Mat src1, src2;
src1 = imread("图片路径");
src2 = imread("图片路径");
Mat graySrc1, graySrc2;
cvtColor(src1, graySrc1, CV_BGR2GRAY);
cvtColor(src2, graySrc2, CV_BGR2GRAY);
//步骤二:提取特征并描述
//code:
vector<KeyPoint> keys1;
vector<KeyPoint> keys2;
Ptr<xfeatures2d::SURF> detector = xfeatures2d::SURF::create(1500);
Mat descriptorMat1, descriptorMat2;
detector->detectAndCompute(src1, Mat(), keys1, descriptorMat1);
detector->detectAndCompute(src2, Mat(), keys2, descriptorMat2);
//步骤三:特征点匹配
//code:
cv::BFMatcher matcher;
std::vector<DMatch> matches;
matcher.match(descriptorMat1, descriptorMat2, matches);
//步骤四:获取优秀匹配点
//code:
double max_dist = 0; double min_dist = 100;
for (int i=0; i<descriptorMat1.rows; i++)
{
double dist = matches[i].distance;
if (dist < min_dist) min_dist = dist;
if (dist > max_dist) max_dist = dist;
}
cout<<"-- Max dist :"<< max_dist<<endl;
cout<<"-- Min dist :"<< min_dist<<endl;
vector< DMatch > good_matches;
for (int i=0; i<descriptorMat1.rows; i++)
{
if (matches[i].distance < 0.2*max_dist)
{
good_matches.push_back(matches[i]);
}
}
//步骤五:绘制特征匹配图
//code:
Mat img_matches;
drawMatches(src1, keys1, src2, keys2,good_matches, img_matches,
Scalar::all(-1), Scalar::all(-1),vector<char>(),
DrawMatchesFlags::NOT_DRAW_SINGLE_POINTS);
注:需要添加#include<opencv2/xfeatures2d.hpp>,#include<opencv2/features2d.hpp>,其中SIFT和SURF在xfeatures2d中,ORB在feature2d中。
二、测试结果及对比展示
1、原图1(340*256)的特征检测结果:(按ORB->SURF->SIFT顺序)
image.png image.pngimage.png
2、原图2(320*426)的特征检测结果:(按ORB->SURF->SIFT顺序)
image.png image.pngimage.png
3、源图1和2的特征匹配结果(筛选后):(按ORB->SURF->SIFT顺序)
image.png对ORB的结果不进行matchs的筛选结果如下:
image.png
image.png
image.png
4、运行时间如下:
1.png1.png
网友评论