opencv特征点匹配(暴力匹配与优化)
1.相关函数
1.knnMatch() 方法,而k=1,返回一对匹配子,这个方法只返回最接近的距离的一对匹配子,当有足够多的匹配子的时候,这种方法通常能够产生最好的结果和最小的误差。
2.Dmatch类
struct CV_EXPORTS_W_SIMPLE DMatch
{
CV_WRAP DMatch() : queryIdx(-1), trainIdx(-1), imgIdx(-1), distance(FLT_MAX) {
}//1
CV_WRAP DMatch( int _queryIdx, int _trainIdx, float _distance ) :
queryIdx(_queryIdx), trainIdx(_trainIdx), imgIdx(-1), distance(_distance) {
}//2
CV_WRAP DMatch( int _queryIdx, int _trainIdx, int _imgIdx, float _distance ) :
queryIdx(_queryIdx), trainIdx(_trainIdx), imgIdx(_imgIdx), distance(_distance) {
}//3
CV_PROP_RW int queryIdx; // query descriptor index
CV_PROP_RW int trainIdx; // train descriptor index
CV_PROP_RW int imgIdx; // train image index
CV_PROP_RW float distance;
// less is better
bool operator<( const DMatch &m ) const
{
return distance < m.distance;
}
};
1、2、3不用说,是三个构造函数。
接着, int queryIdx –>是测试图像的特征点描述符(descriptor)的下标,同时也是描述符对应特征点(keypoint)的下标。
int trainIdx –> 是样本图像的特征点描述符的下标,同样也是相应的特征点的下标。
int imgIdx –>当样本是多张图像的话有用。
float distance –>代表这一对匹配的特征点描述符(本质是向量)的欧氏距离,数值越小也就说明两个特征点越相像。
最后, 也就是一个小于操作符的重载,用于比较和排序。 比较的是上述的distance,当然是越小越好。
2.Code
vector<KeyPoint> kp1, kp2;
Ptr<ORB> mORB = ORB::create(500);;
Mat descriptors_1, descriptors_2;
int t1 = GetTickCount();
mORB->detect(mImage1, kp1);
mORB->compute(mImage1, kp1, descriptors_1);
std::cout << "kp1:" << kp1.size() << "--time for image1:" << GetTickCount() - t1 << "ms" << endl;
int t2 = GetTickCount();
mORB->detect(mImage2, kp2);
mORB->compute(mImage2, kp2, descriptors_2);
std::cout << "kp2:" << kp2.size() << "--time for image2:" << GetTickCount() - t2 << "ms" << endl;
BFMatcher matcher(NORM_HAMMING);
Mat img_matches;
vector<vector<DMatch>> matches;
vector<DMatch> bestMatches;
matcher.knnMatch(descriptors_1, descriptors_2, matches, 2);
std::cout << "matches size:" << (int)matches.size() << endl;
for (int i = 0; i < (int)matches.size(); i++)
{
if (matches[i][0].distance < 0.5 * matches[i][1].distance)
{
bestMatches.push_back(matches[i][0]);
}
}
drawMatches(mImage1, kp1, mImage2, kp2, bestMatches, img_matches, Scalar::all(-1), Scalar::all(-1),
vector<char>(), DrawMatchesFlags::NOT_DRAW_SINGLE_POINTS);
imshow("Matches", img_matches);
imwrite("match.jpg", img_matches);
std::cout << "the size of bestmatches:" << bestMatches.size() << endl;