編程環(huán)境:
VS + OpenCV + C++
完整代碼已經(jīng)更新至GitHub,歡迎fork~GitHub鏈接
聲明:創(chuàng)作不易潜索,未經(jīng)授權(quán)不得復(fù)制轉(zhuǎn)載
statement:No reprinting without authorization
內(nèi)容:
? 了解OpenCV中實現(xiàn)的SIFT, SURF, ORB等特征檢測器的用法,并進(jìn)行實驗梢睛。將檢測到的特征點(diǎn)用不同大小的圓表示脾还,比較不同方法的效率伴箩、效果等。
? 了解OpenCV的特征匹配方法鄙漏,并進(jìn)行實驗嗤谚。
一棺蛛、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);
//步驟三:特征點(diǎn)匹配
//code:
cv::BFMatcher matcher;
std::vector<DMatch> matches;
matcher.match(descriptorMat1, descriptorMat2, matches);
//步驟四:獲取優(yōu)秀匹配點(diǎn)
//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中。
二椅野、測試結(jié)果及對比展示
1彤恶、原圖1(340*256)的特征檢測結(jié)果:(按ORB->SURF->SIFT順序)
2、原圖2(320*426)的特征檢測結(jié)果:(按ORB->SURF->SIFT順序)
3鳄橘、源圖1和2的特征匹配結(jié)果(篩選后):(按ORB->SURF->SIFT順序)
對ORB的結(jié)果不進(jìn)行matchs的篩選結(jié)果如下: