用到的库文件
#include
#include
using namespace std;
//opencv 特征检测模块
#include
#include
#include
提取图像中的特征 关键点 与 关键点的描述子,分别用到了opencv库中的cv::FeatureDetector和cv::DescriptorExtractor来计算
1、读取需要处理的两幅RGBD图像:
cv::Mat rgb1,rgb2,depth1,depth2;
rgb1 = cv::imread("./data3/rgb1.png");
rgb2 = cv::imread("./data3/rgb2.png");
depth1 = cv::imread("./data3/depth1.png",-1);
depth2 = cv::imread("./data3/depth2.png",-1);
2、随后声明 特征提取起以及描述子提取器
cv::Ptr
cv::Ptr
3、声明了两个提取器之后,开始构建,在构建之前还需要在初始化nonfree模块(以SIFT特征点为例):
cv::initModule_nonfree();
_detector = cv::FeatureDetector::create("GridSIFT");
_descriptor = cv::DescriptorExtractor::create("SIFT");
4、提取两幅图像的关键点:
vector
_detector->detect(rgb1,kp1); //提取关键点
_detector->detect(rgb2,kp2);
cout<<"Key points of two images:"< 5、显示刚才计算得到的关键点: cv::Mat imgShow; 6、计算描述子: cv::Mat desp1,desp2; 由上述过程计算得到的关键点和描述子之后,需要对两帧图像进行匹配: 1、匹配描述子 vector 2、显示匹配的特征 cv::Mat imgMatches; 3、误匹配处理,处理准则:去掉大于最小距离四倍的匹配 vector for(size_t i = 0; i < matches.size(); i++) 4、显示处理过后的匹配结果 cout<<"good match = "<
cv::drawKeypoints(rgb1,kp1,imgShow,cv::Scalar::all(-1),cv::DrawMatchesFlags::DRAW_RICH_KEYPOINTS);
cv::imshow("keypoints",imgShow);
cv::imwrite("./data3/keypoints.png",imgShow);
cv::waitKey(0); //暂停等待一个按键
_descriptor->compute(rgb1,kp1,desp1);
_descriptor->compute(rgb2,kp2,desp2);二、特征匹配
cv::FlannBasedMatcher matcher;
matcher.match(desp1,desp2,matches);
cout<<"Find total "<
cv::drawMatches(rgb1,kp1,rgb2,kp2,matches,imgMatches);
cv::imshow("matches",imgMatches);
cv::imwrite("./data3/matches.png",imgMatches);
cv::waitKey(0);
double minDis = 9999;
for(size_t i = 0; i < matches.size(); i++)
{
if( matches[i].distance < minDis)
minDis = matches[i].distance;
}
{
if( matches[i].distance < 4*minDis)
goodMatches.push_back( matches[i] );
}
cv::imshow("good matches",imgMatches);
cv::imwrite("./data3/good_matches.png",imgMatches);
cv::waitKey(0);